k8s namespace(名称空间)操作

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等资源也将被同时清除,删除时要慎重

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值