8-Overview-Namespaces

cucytoman發表於2019-09-24

concepts/overview/working-with-objects/namespaces/

kubernetes支援由同一物理叢集支援的多個虛擬叢集。這些虛擬叢集稱為名稱空間(Namespaces, NS)。

When to Use Multiple Namespaces

名稱空間旨在用於具有分佈在多個團隊或專案中的許多使用者的環境中。對於擁有幾個到幾十個使用者的叢集,您根本不需要建立或考慮名稱空間。在需要名稱空間提供的功能時開始使用名稱空間。(簡而言之當團隊人數很多時,使用不同的名稱空間更有意義,否則額外增加維護成本)

Namespaces provide a scope for names. Names of resources need to be unique within a namespace, but not across namespaces. Namespaces can not be nested inside one another and each Kubernetes resource can only be in one namespace.名稱空間提供名稱的作用域。資源的名稱在名稱空間中必須是唯一的,但不能跨名稱空間。名稱空間不能相互巢狀,每個kubernetes資源只能位於一個名稱空間中。

Namespaces are a way to divide cluster resources between multiple users 名稱空間是在多個使用者之間劃分叢集資源的一種方法。(via resource quota).

在kubernetes的未來版本中,預設情況下,相同名稱空間中的物件將具有相同的訪問控制策略。

不必使用多個名稱空間來分隔稍有不同的資源,例如同一軟體的不同版本:使用標籤來區分同一名稱空間中的資源。

Working with Namespaces

名稱空間的建立和刪除在[名稱空間管理指南文件] Admin Guide documentation for namespaces.

檢視名稱空間

可以使用以下命令列出群集中的當前名稱空間:

kubectl get namespace
NAME          STATUS    AGE
default       Active    1d
kube-system   Active    1d
kube-public   Active    1d

Kubernetes 安裝後預設有三個名稱空間:

  • default 沒有其他名稱空間的物件的預設名稱空間
  • kube-system 由kubernetes系統建立的物件的名稱空間(叢集關鍵元件)
  • kube-public此名稱空間是自動建立的,所有使用者(包括未經身份驗證的使用者)都可以讀取。這個名稱空間主要是為叢集使用而保留的,以防某些資源在整個叢集中公開可見和可讀。這個名稱空間的公共方面只是一個約定,而不是一個要求。

設定請求的名稱空間

要為當前請求設定名稱空間,請使用--namespace 引數.

例如:

kubectl run nginx --image=nginx --namespace=<insert-namespace-name-here>
kubectl get pods --namespace=<insert-namespace-name-here>

設定名稱空間首選項(設定預設NS)

可以永久儲存該上下文中所有後續kubectl命令的名稱空間。

kubectl config set-context --current --namespace=<insert-namespace-name-here>
# Validate it
kubectl config view | grep namespace:

名稱空間和DNS

When you create a Service, it creates a corresponding DNS entry. This entry is of the form <service-name>.<namespace-name>.svc.cluster.local, which means that if a container just uses <service-name>, it will resolve to the service which is local to a namespace. This is useful for using the same configuration across multiple namespaces such as Development, Staging and Production. If you want to reach across namespaces, you need to use the fully qualified domain name (FQDN).建立服務時,它會建立相應的dns條目。此條目的格式為..svc.cluster.local,這意味著如果容器僅使用,它將解析為名稱空間的本地服務。這對於跨多個名稱空間(如開發、登臺和生產)使用相同的配置非常有用。如果要跨名稱空間訪問,則需要使用完全限定的域名(fqdn)。

Not All Objects are in a Namespace 並非所有物件都在名稱空間中

Most Kubernetes resources (e.g. pods, services, replication controllers, and others) are in some namespaces. However namespace resources are not themselves in a namespace. And low-level resources, such as nodes and persistentVolumes, are not in any namespace.

大多數kubernetes資源(例如pods、服務、複製控制器和其他)位於某些名稱空間中。但是名稱空間資源本身不在名稱空間中。而低階別資源(如節點和persistentVolumes)不在任何名稱空間中。

要檢視哪些kubernetes資源在名稱空間中,哪些不在名稱空間中,請執行以下操作:

# In a namespace
kubectl api-resources --namespaced=true

# Not in a namespace
kubectl api-resources --namespaced=false

What's next

本作品採用《CC 協議》,轉載必須註明作者和本文連結