一、命名空间(namespace)作用
Namespace是对一组资源和对象的抽象集合.
常见的 pod, service, deployment 等都是属于某一个namespace的(默认是 default)
不是所有资源都属于namespace,如nodes, persistent volume,namespace 等资源则不属于任何 namespace
二、查看namespace 1 2 3 4 5 6 [root@k8s-master1 ~] NAME STATUS AGE default Active 130m kube-node-lease Active 130m kube-public Active 130m kube-system Active 130m
三、查看namespace里的资源 使用kubectl get all --namespace=命名空间名称
可以查看此命名空间下的所有资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 [root@k8s-master1 ~] NAME READY STATUS RESTARTS AGE pod/calico-kube-controllers-7fdc86d8ff-cskfq 1/1 Running 3 5d1h pod/calico-node-9dpc9 1/1 Running 2 5d1h pod/calico-node-jdmxw 1/1 Running 3 5d1h pod/calico-node-krwps 1/1 Running 2 5d1h pod/calico-node-tttlr 1/1 Running 2 5d1h pod/coredns-65dbdb44db-mm7cr 1/1 Running 2 5d1h pod/dashboard-metrics-scraper-545bbb8767-q66bc 1/1 Running 2 5d1h pod/kubernetes-dashboard-65665f84db-nll6k 1/1 Running 4 5d1h pod/metrics-server-869ffc99cd-8f4jd 1/1 Running 3 5d1h NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/dashboard-metrics-scraper ClusterIP 10.2.246.128 <none> 8000/TCP 5d1h service/kube-dns ClusterIP 10.2.0.2 <none> 53/UDP,53/TCP,9153/TCP 5d1h service/kubernetes-dashboard NodePort 10.2.213.30 <none> 443:21351/TCP 5d1h service/metrics-server ClusterIP 10.2.232.121 <none> 443/TCP 5d1h NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE daemonset.apps/calico-node 4 4 4 4 4 beta.kubernetes.io/os=linux 5d1h NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/calico-kube-controllers 1/1 1 1 5d1h deployment.apps/coredns 1/1 1 1 5d1h deployment.apps/dashboard-metrics-scraper 1/1 1 1 5d1h deployment.apps/kubernetes-dashboard 1/1 1 1 5d1h deployment.apps/metrics-server 1/1 1 1 5d1h NAME DESIRED CURRENT READY AGE replicaset.apps/calico-kube-controllers-7fdc86d8ff 1 1 1 5d1h replicaset.apps/coredns-65dbdb44db 1 1 1 5d1h replicaset.apps/dashboard-metrics-scraper-545bbb8767 1 1 1 5d1h replicaset.apps/kubernetes-dashboard-65665f84db 1 1 1 5d1h replicaset.apps/metrics-server-869ffc99cd 1 1 1 5d1h
使用kubectl get 资源类型 --namespace=命名空间名称
可以查看此命名空间下的对应的资源
1 2 3 4 5 6 7 8 9 10 11 [root@k8s-master1 ~] NAME READY STATUS RESTARTS AGE calico-kube-controllers-7fdc86d8ff-cskfq 1/1 Running 3 5d1h calico-node-9dpc9 1/1 Running 2 5d1h calico-node-jdmxw 1/1 Running 3 5d1h calico-node-krwps 1/1 Running 2 5d1h calico-node-tttlr 1/1 Running 2 5d1h coredns-65dbdb44db-mm7cr 1/1 Running 2 5d1h dashboard-metrics-scraper-545bbb8767-q66bc 1/1 Running 2 5d1h kubernetes-dashboard-65665f84db-nll6k 1/1 Running 4 5d1h metrics-server-869ffc99cd-8f4jd 1/1 Running 3 5d1h
使用kubectl get 资源类型 -A
,查看所有命名空间的资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [root@k8s-master01 kubernetes] NAMESPACE NAME READY STATUS RESTARTS AGE calico-apiserver calico-apiserver-665569779-4wr5l 1/1 Running 0 6h27m calico-apiserver calico-apiserver-665569779-r76t6 1/1 Running 0 6h27m calico-system calico-kube-controllers-78687bb75f-46l2t 1/1 Running 0 6h29m calico-system calico-node-f9gx6 1/1 Running 0 5h58m calico-system calico-node-rsk4w 1/1 Running 0 5h58m calico-system calico-node-xfr2n 1/1 Running 0 6h29m calico-system calico-typha-75444c4b8-4x7pp 1/1 Running 0 5h58m calico-system calico-typha-75444c4b8-nmmj5 1/1 Running 0 6h29m calico-system csi-node-driver-4l2ts 2/2 Running 0 5h56m calico-system csi-node-driver-92q4k 2/2 Running 0 6h28m calico-system csi-node-driver-kcw5b 2/2 Running 0 5h57m kube-system coredns-558bd4d5db-5j4gq 1/1 Running 0 16h kube-system coredns-558bd4d5db-79qsm 1/1 Running 0 16h kube-system etcd-k8s-master01 1/1 Running 0 16h kube-system kube-apiserver-k8s-master01 1/1 Running 0 16h kube-system kube-controller-manager-k8s-master01 1/1 Running 0 16h kube-system kube-proxy-v2ltp 1/1 Running 0 16h kube-system kube-proxy-wzxps 1/1 Running 0 5h58m kube-system kube-proxy-zr77d 1/1 Running 0 5h58m kube-system kube-scheduler-k8s-master01 1/1 Running 0 16h tigera-operator tigera-operator-6f669b6c4f-qmdbc 1/1 Running 0 6h37m
四、创建namespace 4.1 命令创建 1 2 3 4 5 6 7 8 9 10 [root@k8s-master1 ~] namespace/ns1 created [root@k8s-master1 ~] NAME STATUS AGE default Active 5d1h kube-node-lease Active 5d1h kube-public Active 5d1h kube-system Active 5d1h ns1 Active 10s
4.2 YAML文件创建
k8s中几乎所有的资源都可以通这YAML编排来创建
编写创建namespace的YAML文件
1 2 3 4 5 [root@k8s-master1 ~] apiVersion: v1 kind: Namespace metadata: name: ns2
使用kubctl apply -f应用YAML文件
1 2 3 4 5 6 7 8 9 10 11 [root@k8s-master1 ~] namespace/ns2 created [root@k8s-master1 ~] NAME STATUS AGE default Active 5d2h kube-node-lease Active 5d2h kube-public Active 5d2h kube-system Active 5d2h ns1 Active 10m ns2 Active 46s
可以使用 kubectl edit 资源类型 资源名 编辑资源的YAML语法
1 2 [root@k8s-master1 ~] ......
也可使用 kubectl get 资源类型 资源名 -o yaml
来查看
1 2 [root@k8s-master1 ~] ......
还可通过kubectl explain 资源类型
来查看语法文档
1 2 3 [root@k8s-master1 ~] [root@k8s-master1 ~] [root@k8s-master1 ~]
五、删除namespace 注意:
删除一个namespace会自动删除所有属于该namespace的资源(类似mysql中drop库会删除库里的所有表一样,请慎重操作)
default,kube-system,kube-public命名空间不可删除
5.1 命令删除 1 2 [root@k8s-master1 ~] namespace "ns1" deleted
5.2 YAML文件删除 1 2 3 4 5 6 7 8 9 [root@k8s-master1 ~] namespace "ns2" deleted [root@k8s-master1 ~] NAME STATUS AGE default Active 5d2h kube-node-lease Active 5d2h kube-public Active 5d2h kube-system Active 5d2h