Kubernetes 简明教程

Kubernetes - Namespace

命名空间为资源名称提供了其他限定。当多个团队使用同一个集群并且存在名称冲突的可能时,这会很有帮助。它可以作为多个集群之间的虚拟墙。

Namespace provides an additional qualification to a resource name. This is helpful when multiple teams are using the same cluster and there is a potential of name collision. It can be as a virtual wall between multiple clusters.

Functionality of Namespace

以下是 Kubernetes 中命名空间的一些重要功能−

Following are some of the important functionalities of a Namespace in Kubernetes −

  1. Namespaces help pod-to-pod communication using the same namespace.

  2. Namespaces are virtual clusters that can sit on top of the same physical cluster.

  3. They provide logical separation between the teams and their environments.

Create a Namespace

以下命令用于创建命名空间。

The following command is used to create a namespace.

apiVersion: v1
kind: Namespce
metadata
   name: elk

Control the Namespace

以下命令用于控制命名空间。

The following command is used to control the namespace.

$ kubectl create –f namespace.yml ---------> 1
$ kubectl get namespace -----------------> 2
$ kubectl get namespace <Namespace name> ------->3
$ kubectl describe namespace <Namespace name> ---->4
$ kubectl delete namespace <Namespace name>

在上述代码中,

In the above code,

  1. We are using the command to create a namespace.

  2. This will list all the available namespace.

  3. This will get a particular namespace whose name is specified in the command.

  4. This will describe the complete details about the service.

  5. This will delete a particular namespace present in the cluster.

Using Namespace in Service - Example

以下是使用 service 中的命名空间的示例文件的示例。

Following is an example of a sample file for using namespace in service.

apiVersion: v1
kind: Service
metadata:
   name: elasticsearch
   namespace: elk
   labels:
      component: elasticsearch
spec:
   type: LoadBalancer
   selector:
      component: elasticsearch
   ports:
   - name: http
      port: 9200
      protocol: TCP
   - name: transport
      port: 9300
      protocol: TCP

在上面的代码中,我们在带有 elk 名称的服务元数据下使用相同的命名空间。

In the above code, we are using the same namespace under service metadata with the name of elk.