获取名称空间

bash
1
2
3
4
5
6
7
root@k8s-master01:~# kubectl get ns
NAME STATUS AGE
default Active 24h
kube-node-lease Active 24h
kube-public Active 24h
kube-system Active 24h
longhorn-system Active 8h

查看名称空间的资源清单

bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
root@k8s-master01:~# kubectl get ns default -o yaml
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: "2021-06-28T10:53:44Z"
labels:
kubernetes.io/metadata.name: default
name: default
resourceVersion: "209"
uid: 2bef0068-5658-4f23-a2a2-c3f712cdaa7b
spec:
finalizers:
- kubernetes
status:
phase: Active

创建名称空间

使用资源清单创建名称空间

1.创建资源清单

bash
1
2
3
4
apiVersion: v1
kind: Namespace
metadata:
name: dev

2.应用资源清单

bash
1
2
3
4
5
6
7
8
9
10
root@k8s-master01:~/yaml/chapter01# kubectl apply -f namespace.yaml
namespace/dev created
root@k8s-master01:~/yaml/chapter01# kubectl get ns
NAME STATUS AGE
default Active 24h
dev Active 4s # dev名称空间已经创建
kube-node-lease Active 24h
kube-public Active 24h
kube-system Active 24h
longhorn-system Active 9h

命令创建名称空间

使用命令创建出namespace。

bash
1
2
3
4
5
6
7
8
9
10
11
12
root@k8s-master01:~/yaml/chapter01# kubectl create ns test
namespace/test created

root@k8s-master01:~/yaml/chapter01# kubectl get ns
NAME STATUS AGE
default Active 24h
dev Active 76s
kube-node-lease Active 24h
kube-public Active 24h
kube-system Active 24h
longhorn-system Active 9h
test Active 4s