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