title: kubernetes 切换context 或 namespace
author: susu
# 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 ~]#
WARNING: No any other purpose,keeping reminded! So sorry to offended,if necessary, contact me and I do change what I had done to protect your privileges!