Task weight: 1%
You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts.
Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh
, the command should use kubectl.
Finally write a second command doing the same thing into /opt/course/1/context_default_no_kubectl.sh
, but without the use of kubectl.*
alias k=kubectl
export do="--dry-run=client -o yaml"
export now="--force --grace-period 0" # k delete pod x $now
# Vim
# To make vim use 2 spaces for a tab edit ~/.vimrc to contain:
set tabstop=2
set expandtab
set shiftwidth=2
Answer
Maybe the fastest way is just to run:
第一步
k config get-contexts # copy manually
k config get-contexts -o name > /opt/course/1/contexts
Or using jsonpath:
k config view -o yaml # overview``
k config view -o jsonpath=“{.contexts[].name}"k config view -o jsonpath="{.contexts[].name}” | tr " " “\n” # new lines
k config view -o jsonpath=“{.contexts[*].name}” | tr " " “\n” > /opt/course/1/contexts `
The content should then look like:
– # /opt/course/1/contexts
k8s-c1-H
k8s-c2-AC
k8s-c3-CCC
Next create the first command:
vim /opt/course/1/context_default_kubectl.sh
写入
kubectl config current-context
➜ sh /opt/course/1/context_default_kubectl.sh
k8s-c1-H
And the second one:
vim /opt/course/1/context_default_no_kubectl.sh
写入
cat ~/.kube/config | grep current
运行
➜ sh /opt/course/1/context_default_no_kubectl.sh
current-context: k8s-c1-H
In the real exam you might need to filter and find information from bigger lists of resources, hence knowing a little jsonpath and simple bash filtering will be helpful.
The second command could also be improved to:
vim /opt/course/1/context_default_no_kubectl.sh
cat ~/.kube/config | grep current | sed -e "s/current-context: //"