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

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

【51CTO.com快译】

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

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

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

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

前提条件

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

如果您想学习创建Kubernetes集群,请点击此处(https://www.howtoforge.com/setup-a-kubernetes-cluster-on-aws-ec2-instance-ubuntu-using-kubeadm/)。本指南将帮助您在AWS Ubuntu 18.04 EC2实例上创建有1个Master和2个节点的Kubernetes集群。

我们要做什么?

  • 创建命名空间

创建命名空间

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

  1. kubectl get namespace #Get all namespace in the cluster 

图1

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

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

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

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

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

  1. kubectl create namespace test-env #Create a namespace  
  2. kubectl get namespace #Get a list of namespaces  

图2

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

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

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

如果您试图在未指定命名空间的情况下创建pod,就无法获得pod的详细信息。 

  1. kubectl get pods #Get a list of pods 

想获得属于“test-env”命名空间的pod的详细信息,使用以下命令。 

  1. kubectl get pods --namespace=test-env #Get a list of pods in the specified namespace 

图3

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

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

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

  1. kubectl get pods #Get a list of pods from the default namespace 

图4

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

  1. kubectl config set-context --current --namespace=default #Check the namespace to default  
  2. kubectl get pods #Get a list of pods  

图5

想检查哪个是默认命名空间,请使用以下命令。 

  1. kubectl config view --minify | grep namespace: #Extract the namespace from the kubernetes config file.  
  2. kubectl config set-context --current --namespace=test-env #Set default namespace in the config file.  
  3. kubectl config view --minify | grep namespace:  

图6

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

  1. kubectl api-resources --namespaced=true #Get Kubernetes objects which can be in a namespaces 

图7

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

  1. kubectl api-resources --namespaced=false #Get a list of Kubernetes objects that can never be in a namespace 

图8

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

  1. kubectl get namespaces #Get a list of namespaces.  
  2. kubectl describe namespace test-env #Get details of a namespace.  

图9

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

  1. vim namespace-using-file.yml #Create a namespace definition file 

图10

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

  1. kubectl create -f namespace-using-file.yml #Create a namespace using a .yml file  
  2. kubectl get namespaces #Get a list of namespaces  

图11

您不再需要命名空间时,只需使用以下命令即可删除它。 

  1. kubectl get namespaces #Get a list of namespaces  
  2. kubectl delete namespaces env-prod test-env #Delete a namespace  
  3. kubectl get namespaces #Get a list of namespaces  

图12

结论

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

原文标题:How to create Namespaces in Kubernetes

【51CTO译稿,合作站点转载请注明原文译者和出处为51CTO.com】

 

责任编辑:庞桂玉 来源: 51CTO
相关推荐

2019-07-30 10:33:01

2020-07-20 07:00:00

KubernetesHostPath

2022-11-18 14:58:34

JavaScript语言TypeScript

2019-07-29 10:00:10

Windows 10节省空间Windows

2022-10-19 21:24:24

临时表空间Oracle

2023-02-06 17:49:35

Linux符号链接

2011-05-04 09:11:30

RPM包Linux

2019-05-14 11:00:07

LinuxSSH别名

2015-04-09 11:05:55

openstack公有云openstack镜像

2023-07-29 11:54:44

KuberneteNFS

2022-06-27 05:48:24

Kubernetes容器

2009-11-23 20:05:29

ibmdwLotus

2022-03-15 07:55:09

JavaScript线性仪表图开发

2018-01-26 09:02:30

LinuxPDF创建视频

2023-09-27 23:24:50

C++链表

2022-11-11 09:01:08

SwiftUI条形图子视图

2021-11-11 09:01:01

Helm Chart Kubernetes

2019-07-12 16:28:32

MacKubernetes

2020-07-09 13:10:42

GIMP曲线文本应用

2021-02-18 17:00:52

Linux归档文件
点赞
收藏

51CTO技术栈公众号