Book source: "CKA/CKAD Test Guide: From Docker to Kubernetes Complete Raiders"
Organize the reading notes while studying, and share them with everyone. If the copyright is violated, it will be deleted. Thank you for your support!
Attach a summary post: Kubernetes certification exam self-study series | Summary_COCOgsta's Blog-CSDN Blog
Step 1: Check how many namespaces you currently have.
[root@vms10 ~]# kubectl get ns
NAME STATUS AGE
default Active 27m
kube-node-lease Active 27m
kube-public Active 27m
kube-system Active 27m
[root@vms10 ~]#
复制代码
Step 2: There is a better tool for switching namespaces.
Execute the command wget ftp://ftp.hce.cc/cka-tool/kubens -P /bin/ on vms10 to download kubens to the /bin directory.
[root@vms10 ~]# chmod +x /bin/kubens
[root@vms10 ~]#
复制代码
Step 3: View the current namespace.
[root@vms10 ~]# kubens
default
kube-node-lease
kube-public
kube-system
[root@vms10 ~]#
复制代码
Step 4: Create a new namespace ns1.
[root@vms10 ~]# kubectl create ns ns1
namespace/ns1 created
[root@vms10 ~]#
[root@vms10 ~]# kubectl get ns
NAME STATUS AGE
default Active 29m
kube-node-lease Active 29m
kube-public Active 29m
kube-system Active 29m
ns1 Active 8s
[root@vms10 ~]#
复制代码
Step 5: Switch to the ns1 namespace.
[root@vms10 ~]# kubens ns1
Context "kubernetes-admin@kubernetes" modified.
Active namespace is "ns1".
[root@vms10 ~]#
[root@vms10 ~]# kubens
default
kube-public
kube-node-lease
kube-system
ns1
[root@vms10 ~]#
复制代码
Step 6: Switch to the default namespace.
[root@vms10 ~]# kubens default
Context "kubernetes-admin@kubernetes" modified.
Active namespace is "default".
[root@vms10 ~]#
复制代码
Step 7: Delete namespace ns1.
[root@vms10 ~]# kubectl delete ns ns1
namespace "ns1" deleted
[root@vms10 ~]#
复制代码
You can also use the following commands to switch namespaces.
kubectl config set-context 集群名 --namespace=命名空间
复制代码
If the cluster does not switch, but only switches the namespace, you can also use the following command.
kubectl config set-context --current --namespace=命名空间