3.7.1 了解对命名空间的需求

将资源分配为生产, 开发和 QA 环境.

3.7.2 发现其他命名空间及其 pod

列出集群中所有命名空间:

  1. $ kubectl get ns
  2. NAME STATUS AGE
  3. default Active 46h
  4. kube-node-lease Active 46h
  5. kube-public Active 46h
  6. kube-system Active 46h
  7. kubernetes-dashboard Active 24h

列出指定 ns 中的 pod:

  • 也可以使用 -n
  1. $ kubectl get po --namespace kube-system
  2. NAME READY STATUS RESTARTS AGE
  3. coredns-74ff55c5b-hxsrg 1/1 Running 2 46h
  4. etcd-minikube 1/1 Running 2 46h
  5. kube-apiserver-minikube 1/1 Running 2 46h
  6. kube-controller-manager-minikube 1/1 Running 2 46h
  7. kube-proxy-zctp7 1/1 Running 2 46h
  8. kube-scheduler-minikube 1/1 Running 2 46h
  9. metrics-server-56c4f8c9d6-mxtqq 0/1 ImagePullBackOff 0 23h
  10. storage-provisioner 1/1 Running 4 46h

3.7.3 创建一个命名空间

从 YAML 文件创建命名空间

image.png

模拟:

  1. apiVersion: v1
  2. kind: Namespace
  3. metadata:
  4. name: custom-namespace

创建:

$ kubectl create -f custom-namespace.yaml 
namespace/custom-namespace created

使用 kubernetes create namespace 命名创建命名空间

$ kubectl create namespace custom-namespace-v2
namespace/custom-namespace-v2 created

注意命名规则: RFC1035

3.7.4 管理其他命名空间中的对象

在指定的命名空间中创建资源, 可以在 yaml 中 metadata 下添加 namespace: custom-namespace 属性, 也可以使用命名:

$ kubectl create -f main-manual.yaml -n custom-namespace
pod/hello-pod created

kubectl 上下文:

  • kubectl config

3.7.5 命名空间提供的隔离

命名空间的隔离不是开箱即用的, 只是逻辑上隔离, 物理上 (比如网络) 不是隔离的.