CKA 模拟题(Configure Access to Multiple Clusters)

这篇博客介绍了如何通过kubectl获取并列出多个集群的上下文名称,并将它们保存到文件中。同时,提供了两个命令,一个使用kubectl显示当前上下文,另一个不依赖kubectl通过读取配置文件来获取当前上下文。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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: //"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

30xm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值