如何在Kubernetes中创建命名空间?

394 阅读4分钟

多个团队使用同一个集群时,命名空间很有用。有可能发生名称冲突时使用命名空间。它可能是多个集群之间的虚拟墙。比如说,我们在Kubernetes集群中不可能有名称一样的pod,但使用命名空间,我们实际上可以划分集群,拥有名称一样的pod。

命名空间的一些重要功能如下。

  • •它使用同一个命名空间帮助pod到pod的通信。
  • •它充当驻留在同一个理集群上的虚拟集群。
  • •它在团队及其环境之间提供了逻辑隔离。

我们在本文中将创建一个命名空间,并在刚创建的命名空间中创建一个pod。我们还将看到如何将命名空间设置为默认命名空间。

前提条件

  • 有至少1个worker节点的Kubernetes集群

如果您想学习创建Kubernetes集群,请点击此处(www.howtoforge.com/setup-a-kub… Ubuntu 18.04 EC2实例上创建有1个Master和2个节点的Kubernetes集群。

我们要做什么?

  • 创建命名空间

创建命名空间

想列出Kubernetes集群中所有可用的命名空间,请执行以下命令。

kubectl get namespace #Get all namespace in the cluster 
![如何在Kubernetes中创建命名空间?](https://p3-tt.byteimg.com/origin/pgc-image/e2c2e780a42c423abbd23d6e6471eb51?from=pc)

图1

现在,不妨在并不存在的特定命名空间中创建pod。

想在“test-env”命名空间中创建一个pod,执行以下命令。

kubectl run nginx --image=nginx --namespace=test-env #Try to create a pod in the namespace that does not exist. 

pod不会在不存在的命名空间中创建,因此我们先要创建一个命名空间。

想创建命名空间“test-env”,执行以下命令。

kubectl create namespace test-env #Create a namespace  kubectl get namespace #Get a list of namespaces  
![如何在Kubernetes中创建命名空间?](https://p3-tt.byteimg.com/origin/pgc-image/062a0358e9c441849ffcfc82efeca344?from=pc)

图2

现在,我们有了想在其中创建pod的命名空间。

想在我们创建的这个空间中创建pod,将--namespace = test-env选项传递给命令。

kubectl run nginx --image=nginx --namespace=test-env #Create a pod in the namespace. 

如果您试图在未指定命名空间的情况下创建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 
![如何在Kubernetes中创建命名空间?](https://p6-tt.byteimg.com/origin/pgc-image/15addea0ce534d36aba40236fe45c265?from=pc)

图3

如果您想把命名空间设置为默认命名空间,从而不需要在命令中指定命名空间选项,请使用以下命令。

kubectl config set-context --current --namespace=test-env #Set default namespace 

现在,无需在命令中指定命名空间即可获得pod的详细信息。

kubectl get pods #Get a list of pods from the default namespace 
![如何在Kubernetes中创建命名空间?](https://p3-tt.byteimg.com/origin/pgc-image/efd2497130bd4a09b911dc0b4d297093?from=pc)

图4

想切换到默认命名空间,请使用以下命令。

kubectl config set-context --current --namespace=default #Check the namespace to default kubectl get pods #Get a list of pods  
![如何在Kubernetes中创建命名空间?](https://p3-tt.byteimg.com/origin/pgc-image/c5e0a96faaaf432d8fdb2077102a8852?from=pc)

图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:  
![如何在Kubernetes中创建命名空间?](https://p6-tt.byteimg.com/origin/pgc-image/1137aaac27cc4dda8633f3320b826224?from=pc)

图6

检查哪些Kubernetes资源是命名空间,执行以下命令。

kubectl api-resources --namespaced=true #Get Kubernetes objects which can be in a namespaces 
![如何在Kubernetes中创建命名空间?](https://p1-tt.byteimg.com/origin/pgc-image/849b652afbab4b858771f70315c1215f?from=pc)

图7

想查看哪些Kubernetes资源不在命名空间中,请使用以下命令。

kubectl api-resources --namespaced=false #Get a list of Kubernetes objects that can never be in a namespace 
![如何在Kubernetes中创建命名空间?](https://p1-tt.byteimg.com/origin/pgc-image/211d58e5a2694019b7864eff5cc2da9a?from=pc)

图8

您可以使用下述命令获得命名空间的详细信息。

kubectl get namespaces #Get a list of namespaces.  kubectl describe namespace test-env #Get details of a namespace.  
![如何在Kubernetes中创建命名空间?](https://p1-tt.byteimg.com/origin/pgc-image/15be69b8da784d8a90e49573c9f24724?from=pc)

图9

还可以使用.yml文件创建命名空间。

vim namespace-using-file.yml #Create a namespace definition file 
![如何在Kubernetes中创建命名空间?](https://p1-tt.byteimg.com/origin/pgc-image/9c59f85898424351a4f8adcea1af1e18?from=pc)

图10

执行以下命令以创建对象定义文件中指定的命名空间。

kubectl create -f namespace-using-file.yml #Create a namespace using a .yml file  kubectl get namespaces #Get a list of namespaces  
![如何在Kubernetes中创建命名空间?](https://p6-tt.byteimg.com/origin/pgc-image/f729d950bf0e46988724309dfb8c7b69?from=pc)

图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  
![如何在Kubernetes中创建命名空间?](https://p1-tt.byteimg.com/origin/pgc-image/9fcdc748bacf45c1a06904e6f08623cc?from=pc)

图12

结论

我们在本文中了解了命名空间、创建命名空间、更改默认命名空间,以及检查命名空间中存在和不存在的Kubernetes资源。我们还看到了如何在我们选择的命名空间中创建Kubernetes对象(这里是pod)。