实战生产环境:最新K8s NameSpaces详解

本篇文章,所使用的任何镜像我都会发一个网盘链接,供大家下载学习!
yaml 请到我的github上下载学习!

github: https://github.com/heyangguang

有任何问题可以直接联系我的Email:heyangev@cn.ibm.com

namespaces官网

https://kubernetes.io/docs/co…

namespaces介绍

​ Namespaces简单来说就是可以用来放置Pod的组!如果资源不多的话,那么不需要使用namespaces。但如果资源一旦多起来,比如我们有web组件和database组件,那么我们就可以分别创建两个不同的namespaces来放置和管理我们的Pod。当然namespaces不仅仅这么简单!它还有可以通过磁盘配额可以实现多个用户之间划分群集资源的方法。

查看namespaces

1、查看全部namespaces

[root@master01 ~]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   10h
kube-node-lease   Active   10h
kube-public       Active   10h
kube-system       Active   10h

Kubernetes以三个初始名称空间开头:

default
kube-system
kube-public

2、指定namespaces查看

[root@master01 ~]# kubectl get namespaces kube-system
NAME          STATUS   AGE
kube-system   Active   10h

3、指定namespaces查看详情

[root@master01 ~]# kubectl describe namespaces kube-system
Name:         kube-system
Labels:       
Annotations:  
Status:       Active

No resource quota.

No resource limits.

namespaces status有两个状态:

Active
Terminating

创建namespaces

1、使用命令行创建

[root@master01 ~]# kubectl create namespace test
namespace/test created
[root@master01 ~]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   10h
kube-node-lease   Active   10h
kube-public       Active   10h
kube-system       Active   10h
test              Active   10s

2、使用yaml创建

[root@master01 ~]# cat my-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: yaml-test
[root@master01 ~]# kubectl create -f ./my-namespace.yaml
namespace/yaml-test created
[root@master01 ~]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   10h
kube-node-lease   Active   10h
kube-public       Active   10h
kube-system       Active   10h
test              Active   114s
yaml-test         Active   5s

删除namespaces

1、使用命令行删除

[root@master01 ~]# kubectl delete namespaces test
namespace "test" deleted
[root@master01 ~]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   10h
kube-node-lease   Active   10h
kube-public       Active   10h
kube-system       Active   10h
yaml-test         Active   99s

2、使用yaml删除

[root@master01 ~]# kubectl delete -f ./my-namespace.yaml
namespace "yaml-test" deleted
[root@master01 ~]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   11h
kube-node-lease   Active   11h
kube-public       Active   11h
kube-system       Active   11h

​ 此删除是异步的,因此有一段时间将看到 Terminating
状态中的命名空间。此删除会级联删除,如果namespaces下面有运行Pod一样会删除掉。