多集群的切换是K8s运维中比不可少的问题,常见的基于多个集群进行切换的方法有三种:git
咱们先看看放在.kube
下默认的config集群:github
$ kubectl config view apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: name: kubernetes contexts: - context: cluster: kubernetes user: "2xxxxxxxxx7" name: 2xxxxxxxxxxxxxxxx8 current-context: 2xxxxxxxxxxxx8 kind: Config preferences: {} users: - name: "2xxxxxxxxxxx27" user: client-certificate-data: client-key-data:
经过export
更改默认集群配置:npm
$ export KUBECONFIG=$HOME/.kube/rancher-config
查看:api
$ kubectl config view apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: name: stage contexts: - context: cluster: stage user: stage name: stage current-context: stage kind: Config preferences: {} users: - name: stage user: token:
这种方法就是特别麻烦,每次切换都要经过export设置,很是麻烦,同时也不方便。bash
把要切换的全部config文件添加到KUBECONFIG
环境变量中运维
$ export KUBECONFIG=$HOME/.kube/config:$HOME/.kube/rancher-config
或者能够将命令直接写到.bashrc
文件中,这样就不须要每次手动设置。spa
经过kubectl config view
能够看到这两个文件已经合并到一块儿了:code
$ kubectl config view apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: name: kubernetes contexts: - context: cluster: kubernetes user: "2xxxxxxxxx7" name: 2xxxxxxxxxxxxxxxx8 current-context: 2xxxxxxxxxxxx8 kind: Config preferences: {} users: - name: "2xxxxxxxxxxx27" user: client-certificate-data: client-key-data:
合并一块儿后就能够经过use-context
切换上下文来切换集群:component
# 切换stage集群 $ kubectl config use-context stage Switched to context "stage".
切换到第二个集群:orm
$ kubectl config use-context 2xxxxxxxxxxx8 Switched to context "21xxxxxxxxxxxxx9648".
但这种方法不方便的地方就在若是context不少的时候,不方便查看。
基础环境
mac
k8s 1.16.9
# wget https://github.com.cnpmjs.org/sunny0826/kubecm/releases/download/v0.10.3/kubecm_0.10.3_Darwin_x86_64.tar.gz # tar xf kubecm_0.10.3_Darwin_x86_64.tar.gz # mv kubecm /usr/local/bin # kubecm --help KubeConfig Manager _ _ | | ___ _| |__ ___ ___ _ __ ___ | |/ / | | | '_ \ / _ \/ __| '_ \ _ \ | <| |_| | |_) | __/ (__| | | | | | |_|\_\\__,_|_.__/ \___|\___|_| |_| |_| Find more information at: https://github.com/sunny0826/kubecm Usage: kubecm [command] Available Commands: add Merge configuration file with $HOME/.kube/config alias Generate alias for all contexts completion Generates bash/zsh completion scripts delete Delete the specified context from the kubeconfig help Help about any command ls List kubeconfig merge Merge the kubeconfig files in the specified directory namespace Switch or change namespace interactively rename Rename the contexts of kubeconfig switch Switch Kube Context interactively version Print version info Flags: --config string path of kubeconfig (default "/var/root/.kube/config") -h, --help help for kubecm Use "kubecm [command] --help" for more information about a command.
bash
# ource <(kubecm completion bash) # echo "source <(kubecm completion bash)" >> ~/.bashrc # source ~/.bashrc
zsh
# source <(kubecm completion zsh) # echo "source <(kubecm completion zsh)" >> ~/.zshrc # source ~/.zshrc
查看
# kubecm ls +------------+-----------------------------------+---------------+-----------------------+--------------------------------+--------------+ | CURRENT | NAME | CLUSTER | USER | SERVER | Namespace | +============+===================================+===============+=======================+================================+==============+ | * | 283387751775489594-c02e82b6ada | kubernetes | 283387751775489594 | https://3.3.3.3:6443 | default | | | f044bd94d251fea1279648 | | | | | +------------+-----------------------------------+---------------+-----------------------+--------------------------------+--------------+ Cluster check succeeded! Kubernetes master is running at https://3.3.3.3:6443 Contains components: [scheduler controller-manager etcd-5 etcd-0 etcd-4 etcd-1 etcd-3 etcd-2]
添加
# ll -rw-r--r-- 1 root wheel 5.9K 8 26 15:39 config.shengchan -rw-r--r-- 1 root wheel 5.9K 11 18 15:59 config.yufa # kubecm add -f config.yufa -c -n stage # kubecm add -f config.shengchan -c -n production # kubecm ls +------------+---------------+-----------------------+--------------------+--------------------------------+--------------+ | CURRENT | NAME | CLUSTER | USER | SERVER | Namespace | +============+===============+=======================+====================+================================+==============+ | * | production | cluster-kg7h48f8tb | user-kg7h48f8tb | https://3.3.3.3:6443 | default | +------------+---------------+-----------------------+--------------------+--------------------------------+--------------+ | | stage | cluster-kfc6fbfcdt | user-kfc6fbfcdt | https://4.4.4.4:6443 | default | +------------+---------------+-----------------------+--------------------+--------------------------------+--------------+
删除
# kubecm delete 😸 Select:production 👻 True Context Delete:「production」 「/var/root/.kube/config」 write successful! +------------+---------------+-----------------------+--------------------+--------------------------------+--------------+ | CURRENT | NAME | CLUSTER | USER | SERVER | Namespace | +============+===============+=======================+====================+================================+==============+ | * | stage | cluster-kfc6fbfcdt | user-kfc6fbfcdt | https://4.4.4.4:6443 | default | +------------+---------------+-----------------------+--------------------+--------------------------------+--------------+
切换
# kubecm switch 😸 Select:production 「/var/root/.kube/config」 write successful! +------------+---------------+-----------------------+--------------------+--------------------------------+--------------+ | CURRENT | NAME | CLUSTER | USER | SERVER | Namespace | +============+===============+=======================+====================+================================+==============+ | * | production | cluster-kg7h48f8tb | user-kg7h48f8tb | https://3.3.3.3:6443 | default | +------------+---------------+-----------------------+--------------------+--------------------------------+--------------+ | | stage | cluster-kfc6fbfcdt | user-kfc6fbfcdt | https://4.4.4.4:6443 | default | +------------+---------------+-----------------------+--------------------+--------------------------------+--------------+ Switched to context 「production」