kubectx-切换集群工具

kubectx是一个用于在不同Kubernetes集群间切换的实用工具。通过安装Bash脚本并配置KUBECONFIG环境变量,用户可以方便地在多个配置文件之间切换上下文。该工具简化了在AWS和Fyre等不同环境中管理Kubernetes集群的过程。
摘要由CSDN通过智能技术生成

kubectx 帮助您在集群之间来回切换

install

由于 kubectx/kubens 是用 Bash 编写的,因此您应该能够将它们安装到任何安装了 Bash 的 POSIX 环境中。

git clone https://github.com/ahmetb/kubectx /opt/kubectx

确保 kubectx 脚本是可执行的

[root@myk8s1 ~] ls /opt/kubectx/ -ltr
total 40
-rw-r--r-- 1 root root 11357 Feb 17 21:25 LICENSE
-rw-r--r-- 1 root root   968 Feb 17 21:25 CONTRIBUTING.md
-rw-r--r-- 1 root root  7784 Feb 17 21:25 README.md
drwxr-xr-x 2 root root   121 Feb 17 21:25 completion
drwxr-xr-x 2 root root    84 Feb 17 21:25 img
drwxr-xr-x 3 root root   100 Feb 17 21:25 test
-rwxr-xr-x 1 root root  5273 Feb 17 21:25 kubens
-rwxr-xr-x 1 root root  5218 Feb 17 21:25 kubectx

从 PATH 中的某个位置创建指向 kubectx/kubens 的符号链接,例如 /usr/local/bin

ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx

usage

you should first understand how to switch among different clusters by using configuration files. please investigate this link, actually kubectx is built on top of it.

for example, I have one cluster on AWS and one cluster on Fyre, in each cluster there is a ~/.kube/config file, rename it as config.aws and config.fyre and put them to another client machine ~/.kube/ folder with kubectl installed.

[root@centctl1 .kube]# ls -ltr
total 16
drwxr-xr-x 3 root root   23 Nov 26 16:38 cache
-rw-r--r-- 1 root root 2214 Dec  6 10:16 config.aws
drwxr-xr-x 2 root root   73 Dec  6 10:16 kubens
drwxr-xr-x 3 root root 4096 Feb 17 22:05 http-cache
-rw------- 1 root root 5474 Feb 17 22:22 config.fyre

append config files to environment variable KUBECONFIG, you can add export to .bashrc file.

export KUBECONFIG=$KUBECONFIG:$HOME/.kube/config.aws:$HOME/.kube/config.fyre

now if you run kubectx you will see there are 2 contexts

[root@centctl1 .kube] kubectx
arn:aws:eks:us-west-2:296744932886:cluster/IIS-Test-Cluster
kubernetes-admin@kubernetes

switch to kubernetes-admin@kubernetes

[root@centctl1 .kube] kubectx kubernetes-admin@kubernetes
Switched to context "kubernetes-admin@kubernetes".

switch back

[root@centctl1 .kube] kubectx -
Switched to context "arn:aws:eks:us-west-2:296744932886:cluster/IIS-Test-Cluster".

actually the effect is the same as

kubectl config view
kubectl config --kubeconfig=config.fyre use-context kubernetes-admin@kubernetes

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

CN-FuWei

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

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

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

打赏作者

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

抵扣说明:

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

余额充值