k8s namespace(名称空间)操作

查询namespace

命令kubectl get ns或者kubectl get namespaces
案例:

[root@123 ~]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   3h40m
kube-node-lease   Active   3h40m
kube-public       Active   3h40m
kube-system       Active   3h40m

创建namespace

命令: kubectl create -f $namespace.yamlkubectl create namespace $namespace
yaml文件:

[root@123 ~]# cat test-namespace.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: test-ns

创建案例:

#使用yaml创建名称空间
[root@123 ~]# kubectl create -f test-namespace.yaml
test-ns created
[root@123 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   3h45m
kube-node-lease   Active   3h45m
kube-public       Active   3h45m
kube-system       Active   3h45m
test-ns           Active   18s

# 使用命令创建名称空间
[root@123 ~]# kubectl create namespace custom-namespace
custom-namespace created
[root@123 ~]# kubectl get ns
NAME               STATUS   AGE
custom-namespace   Active   11s
default            Active   3h49m
kube-node-lease    Active   3h49m
kube-public        Active   3h49m
kube-system        Active   3h49m
test-ns            Active   4m1s
[root@123 ~]# kubectl describe ns custom-namespace
Name:         custom-namespace
Labels:       kubernetes.io/metadata.name=custom-namespace
Annotations:  <none>
Status:       Active
No resource quota.
No LimitRange resource.

注意:命名空间名称不允许包含点(.)

删除namespace

命令:kubectl delete ns $namespace
案例:

[root@123 ~]# kubectl get ns
NAME               STATUS   AGE
custom-namespace   Active   11s
default            Active   3h49m
kube-node-lease    Active   3h49m
kube-public        Active   3h49m
kube-system        Active   3h49m
test-ns            Active   4m1s

[root@123 ~]# kubectl delete ns custom-namespacenamespace
 "custom-namespace" deleted
 
[root@123 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   3h55m
kube-node-lease   Active   3h55m
kube-public       Active   3h55m
kube-system       Active   3h55m
test-ns           Active   10m

注意:因为pod等资源是在namespace下创建的,如果namespace删除,那么其中的pod、service等资源也将被同时清除,删除时要慎重

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐