管理多个kubernetes集群的配置:

先使用命令创建出一个模板:

# fat
kubectl config set-cluster fat --server=https://fat-master:6443 --certificate-authority="" --kubeconfig=config
kubectl config set-credentials fat --client-certificate="" --client-key="" --kubeconfig=config
kubectl config set-context fat --cluster=fat --user=fat --kubeconfig=config
kubectl config use-context fat --kubeconfig=config

# prod
kubectl config set-cluster pro --server=https://pro-master6443 --certificate-authority="" --kubeconfig=config
kubectl config set-credentials pro --client-certificate="" --client-key="" --kubeconfig=config
kubectl config set-context pro --cluster=pro --user=pro --kubeconfig=config
kubectl config use-context pro --kubeconfig=config

得到的内容如下:

apiVersion: v1
clusters:
- cluster:
    certificate-authority: .
    server: https://fat-master:6443
  name: fat
- cluster:
    certificate-authority: .
    server: https://pro-master:6443
  name: pro
contexts:
- context:
    cluster: fat
    user: fat
  name: fat
- context:
    cluster: pro
    user: pro
  name: pro
current-context: pro
kind: Config
preferences: {}
users:
- name: fat
  user:
    client-certificate: .
    client-key: .
- name: pro
  user:
    client-certificate: .
    client-key: .

        把 certificate-authority、client-certificate、client-key 替换成 certificate-authority-data、client-certificate-data、client-key-data,并将点“.”替换成对应的秘钥。如果有证书在创建配置的时候指定证书即可。

创建快捷方式:

cat >> .bashrc <<EOF
alias pro='kubectl config use-context pro'
alias fat='kubectl config use-context fat'
EOF

切换集群:

~$ fat
Switched to context "fat".