如何在Kubernetes中創(chuàng)建命名空間?
譯文【51CTO.com快譯】
多個團隊使用同一個集群時,命名空間很有用。有可能發(fā)生名稱沖突時使用命名空間。它可能是多個集群之間的虛擬墻。比如說,我們在Kubernetes集群中不可能有名稱一樣的pod,但使用命名空間,我們實際上可以劃分集群,擁有名稱一樣的pod。
命名空間的一些重要功能如下。
- •它使用同一個命名空間幫助pod到pod的通信。
 - •它充當駐留在同一個理集群上的虛擬集群。
 - •它在團隊及其環(huán)境之間提供了邏輯隔離。
 
我們在本文中將創(chuàng)建一個命名空間,并在剛創(chuàng)建的命名空間中創(chuàng)建一個pod。我們還將看到如何將命名空間設置為默認命名空間。
前提條件
- 有至少1個worker節(jié)點的Kubernetes集群
 
如果您想學習創(chuàng)建Kubernetes集群,請點擊此處(https://www.howtoforge.com/setup-a-kubernetes-cluster-on-aws-ec2-instance-ubuntu-using-kubeadm/)。本指南將幫助您在AWS Ubuntu 18.04 EC2實例上創(chuàng)建有1個Master和2個節(jié)點的Kubernetes集群。
我們要做什么?
- 創(chuàng)建命名空間
 
創(chuàng)建命名空間
想列出Kubernetes集群中所有可用的命名空間,請執(zhí)行以下命令。
- kubectl get namespace #Get all namespace in the cluster
 
圖1
現(xiàn)在,不妨在并不存在的特定命名空間中創(chuàng)建pod。
想在“test-env”命名空間中創(chuàng)建一個pod,執(zhí)行以下命令。
- kubectl run nginx --image=nginx --namespace=test-env #Try to create a pod in the namespace that does not exist.
 
pod不會在不存在的命名空間中創(chuàng)建,因此我們先要創(chuàng)建一個命名空間。
想創(chuàng)建命名空間“test-env”,執(zhí)行以下命令。
- kubectl create namespace test-env #Create a namespace
 - kubectl get namespace #Get a list of namespaces
 
圖2
現(xiàn)在,我們有了想在其中創(chuàng)建pod的命名空間。
想在我們創(chuàng)建的這個空間中創(chuàng)建pod,將--namespace = test-env選項傳遞給命令。
- kubectl run nginx --image=nginx --namespace=test-env #Create a pod in the namespace.
 
如果您試圖在未指定命名空間的情況下創(chuàng)建pod,就無法獲得pod的詳細信息。
- kubectl get pods #Get a list of pods
 
想獲得屬于“test-env”命名空間的pod的詳細信息,使用以下命令。
- kubectl get pods --namespace=test-env #Get a list of pods in the specified namespace
 
圖3
如果您想把命名空間設置為默認命名空間,從而不需要在命令中指定命名空間選項,請使用以下命令。
- kubectl config set-context --current --namespace=test-env #Set default namespace
 
現(xiàn)在,無需在命令中指定命名空間即可獲得pod的詳細信息。
- kubectl get pods #Get a list of pods from the default namespace
 
圖4
想切換到默認命名空間,請使用以下命令。
- kubectl config set-context --current --namespace=default #Check the namespace to default
 - kubectl get pods #Get a list of pods
 
圖5
想檢查哪個是默認命名空間,請使用以下命令。
- kubectl config view --minify | grep namespace: #Extract the namespace from the kubernetes config file.
 - kubectl config set-context --current --namespace=test-env #Set default namespace in the config file.
 - kubectl config view --minify | grep namespace:
 
圖6
檢查哪些Kubernetes資源是命名空間,執(zhí)行以下命令。
- kubectl api-resources --namespaced=true #Get Kubernetes objects which can be in a namespaces
 
圖7
想查看哪些Kubernetes資源不在命名空間中,請使用以下命令。
- kubectl api-resources --namespaced=false #Get a list of Kubernetes objects that can never be in a namespace
 
圖8
您可以使用下述命令獲得命名空間的詳細信息。
- kubectl get namespaces #Get a list of namespaces.
 - kubectl describe namespace test-env #Get details of a namespace.
 
圖9
還可以使用.yml文件創(chuàng)建命名空間。
- vim namespace-using-file.yml #Create a namespace definition file
 
圖10
執(zhí)行以下命令以創(chuàng)建對象定義文件中指定的命名空間。
- kubectl create -f namespace-using-file.yml #Create a namespace using a .yml file
 - kubectl get namespaces #Get a list of namespaces
 
圖11
您不再需要命名空間時,只需使用以下命令即可刪除它。
- kubectl get namespaces #Get a list of namespaces
 - kubectl delete namespaces env-prod test-env #Delete a namespace
 - kubectl get namespaces #Get a list of namespaces
 
圖12
結論
我們在本文中了解了命名空間、創(chuàng)建命名空間、更改默認命名空間,以及檢查命名空間中存在和不存在的Kubernetes資源。我們還看到了如何在我們選擇的命名空間中創(chuàng)建Kubernetes對象(這里是pod)。
原文標題:How to create Namespaces in Kubernetes
【51CTO譯稿,合作站點轉載請注明原文譯者和出處為51CTO.com】



























 
 
 












 
 
 
 