Namespace(命名空間)是kubernetes系統中的一個非常重要的概念,Namespace在很多情況下用於實現多租戶的資源隔離。Namespace通過將集群內部的資源對象“分配”到不同的Namespace中,形成邏輯上分組的不同項目、小組或用戶組,便於不同的分組在共享使用整個集群的資源的同時還能被分別管理。
kubernetes集群在啟動后,會默認創建一個名為“default”的Namespace。同一個namespace下面不允許出現兩個service叫相同的名字。
1.查看命名空間
[root@kub_master ~]# kubectl get namespaces -o wide NAME STATUS AGE default Active 7d kube-system Active 7d
可以看到有兩個個namespace
如果在創建過程中不指名Namespace,則用戶創建的pod,RC,Service都將被系統創建到這個默認名為default的Namespace中。
2. 創建命名空間
[root@kub_master ~]# kubectl create namespace test namespace "test" created [root@kub_master ~]# kubectl get namespaces -o wide NAME STATUS AGE default Active 7d kube-system Active 7d test Active 3s
3. 刪除命名空間
注:特別危險!會刪除namespace下所有的k8s資源
[root@kub_master ~]# kubectl delete namespace test namespace "test" deleted [root@kub_master ~]# kubectl get namespaces -o wide NAME STATUS AGE default Active 7d kube-system Active 7d test Terminating 1m [root@kub_master ~]# kubectl get namespaces -o wide NAME STATUS AGE default Active 7d kube-system Active 7d
4. Namespace的定義很簡單,如下所示的yaml定義了名為develop的Namespace
[root@kub_master ~]# mkdir k8s/namespace [root@kub_master ~]# cd k8s/namespace/
[root@kub_master namespace]# vim develop-namespace.yaml [root@kub_master namespace]# cat develop-namespace.yaml apiVersion: v1 kind: Namespace metadata: name: develop
[root@kub_master namespace]# kubectl create -f develop-namespace.yaml namespace "develop" created [root@kub_master namespace]# kubectl get namespaces -o wide NAME STATUS AGE default Active 7d develop Active 15s kube-system Active 7d
5.創建屬於develop命名空間的pod資源
一旦創建了Namespace,在創建資源對象時,就可以指定這個資源對象屬於哪個Namespace。
[root@kub_master namespace]# vim pod-busybox.yaml [root@kub_master namespace]# cat pod-busybox.yaml apiVersion: v1 kind: Pod metadata: name: busybox namespace: develop spec: containers: - name: busybox image: docker.io/busybox:latest imagePullPolicy: IfNotPresent command: - sleep - "3600" [root@kub_master namespace]# kubectl create -f pod-busybox.yaml pod "busybox" created [root@kub_master namespace]# kubectl get pods --namespace=develop NAME READY STATUS RESTARTS AGE busybox 1/1 Running 0 21s
在kubectl命令中加入--namespace參數來查看某個命名空間中的對象。
6. 創建namespace=develop的RC資源
[root@kub_master namespace]# vim nginx-rc.yaml [root@kub_master namespace]# cat nginx-rc.yaml apiVersion: v1 kind: ReplicationController metadata: name: myweb namespace: develop spec: replicas: 2 selector: app: myweb template: metadata: labels: app: myweb spec: containers: - name: myweb image: 192.168.0.212:5000/nginx:1.13 ports: - containerPort: 80 [root@kub_master namespace]# kubectl create -f nginx-rc.yaml replicationcontroller "myweb" created [root@kub_master namespace]# kubectl get rc --namespace=develop NAME DESIRED CURRENT READY AGE myweb 2 2 2 20s [root@kub_master namespace]# kubectl get pods --namespace=develop NAME READY STATUS RESTARTS AGE busybox 1/1 Running 9 9h myweb-1fd25 1/1 Running 0 34s myweb-z1524 1/1 Running 0 34s
7. 創建namespace=develop的Service資源
[root@kub_master namespace]# vim nginx-svc.yaml [root@kub_master namespace]# cat nginx-svc.yaml apiVersion: v1 kind: Service metadata: name: myweb namespace: develop spec: type: NodePort ports: - port: 80 nodePort: 30008 targetPort: 80 selector: app: myweb [root@kub_master namespace]# kubectl create -f nginx-svc.yaml service "myweb" created [root@kub_master namespace]# kubectl get svc --namespace=develop NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE myweb 192.168.72.198 <nodes> 80:30008/TCP 14s
當給每個租戶創建一個Namespace來實現多租戶資源隔離時,還能結合Kubernetes的資源配額管理,限定不同租戶能占用的資源。