Kubernetes 切换context和namespace

# kubernetes查看当前context
[root@VM_0_2_linux ~]# kubectl config get-contexts
CURRENT   NAME                            CLUSTER        AUTHINFO           NAMESPACE
*         kubernetes-admin@cls-lp2k78hj   cls-lp2k78hj   kubernetes-admin   kube-system
[root@VM_0_2_linux ~]# kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://kube-shf.cls-lp2k78hj.io:6443
...
output omitted
...

# 使用定义的名称空间创建新的上下文
kubectl config set-context gce-dev --user=cluster-admin --namespace=dev
kubectl config use-context gce-dev

# 仅切换名称空间
kubectl config set-context --current --namespace=<namespace>

$ alias kubens='kubectl config set-context --current --namespace '
$ alias kubectx='kubectl config use-context '

$ kubens kube-system    //Switch to a different namespace
$ kubectx docker        //Switch to separate context
# 利用第三方小工具(二进制命令)来进行切换context或namespace
[root@VM_0_2_linux ~]# git clone https://github.com/ahmetb/kubectx
[root@VM_0_2_linux ~]# which kubectl
/usr/bin/kubectl
[root@VM_0_2_linux ~]# cp kubectx/kube* /usr/bin/
[root@VM_0_2_linux ~]# kubectx
kubernetes-admin@cls-lp2k78hj
[root@VM_0_2_linux ~]# kubens
default
kube-node-lease
kube-public
kube-system
test
[root@VM_0_2_linux ~]# kubens kube-system
Context "kubernetes-admin@cls-lp2k78hj" modified.
Active namespace is "kube-system".
[root@VM_0_2_linux ~]#
posted @ 2021-06-08 10:15  素丶素  阅读(927)  评论(0编辑  收藏  举报