How to create namespaces in Kubernetes?

Namespaces are helpful when multiple teams are using the same cluster, used in case of name collisions, and can be a virtual wall between multiple clusters. For example, we cannot have pods with the same name in a Kubernetes cluster, but using namespaces we can divide the cluster virtually and have pods with the same name.

Some important features of namespaces are as follows.

  1. Useful for pod-to-pod communication using the same namespace.
  2. Acts as a virtual cluster that can sit on top of the same physical cluster.
  3. Provides a logical separation between teams and their environments.

In this article, we will create a namespace and create a pod in the newly created namespace, we will also see how to set the namespace as the default namespace.

prerequisites

  1. A Kubernetes cluster with at least 1 worker node.

This article will help you create a Kubernetes cluster with 1 master and 2 nodes on AWS Ubuntu 18.04 EC2 instance.

what are we going to do

  1. create namespace

create namespace

To list all namespaces available in the Kubernetes cluster, execute the following command. You can have multiple namespaces, and namespaces can have namespace metadata.

kubectl get namespace #获取集群中的所有命名空间

Now let's try to create a pod in a specific namespace that doesn't exist.

To create a pod in the "test-env" namespace, execute the following command.

kubectl run nginx --image=nginx --namespace=test-env #尝试在不存在的命名空间中创建一个pod。

Pods won't be created in a namespace that doesn't exist, so we first need to create one.

To create the namespace "test-env", execute the following command (k8s create namespace).

kubectl create namespace test-env #创建命名空间
kubectl get namespace #获取命名空间列表

Now that we have a namespace, we want to create a pod in it.

To create a pod in the namespace we created, pass the --namespace=test-env option to the command.

kubectl run nginx --image=nginx --namespace=test-env #在命名空间中创建一个pod。

If you try to get a pod without specifying a namespace, you won't be able to get the pod's details.

kubectl get pods #获取pod列表

To get the details of pods belonging to the 'test-env' namespace, use the following command.

kubectl get pods --namespace=test-env #获取指定命名空间中的pod列表

If you want to set the namespace as the default namespace, you don't need to specify the namespace option in the command, use the following command.

kubectl config set-context --current --namespace=test-env #设置默认命名空间

Pod details can now be obtained without specifying a namespace in the command.

kubectl get pods #从默认命名空间中获取 pod 列表 

To switch to the default namespace, use the following command.

kubectl config set-context --current --namespace=default #检查命名空间为默认值
kubectl get pods #获取pod列表

To check which is the default namespace, use the following command.

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:

To check which Kubernetes resources are namespaces, execute the following command.

kubectl api-resources --namespaced=true #获取可以在命名空间中的Kubernetes对象

To see which Kubernetes resources are not in a namespace, use the following command.

kubectl api-resources --namespaced=false #获取永远不能在命名空间中的Kubernetes对象列表

You can get the namespace details using the command mentioned below.

kubectl get namespaces #获取命名空间列表。
kubectl describe namespace test-env #获取命名空间的详细信息。

Namespaces can also be created using .yml files.

vim namespace-using-file.yml #创建命名空间定义文件

Execute the following command to create the namespace specified in the object definition file.

kubectl create -f namespace-using-file.yml #使用.yml文件创建命名空间
kubectl get namespaces #获取命名空间列表

When you no longer need a namespace, you can delete it with the following command.

kubectl get namespaces #获取命名空间列表
kubectl delete namespaces env-prod test-env #删除一个命名空间
kubectl get namespaces #获取命名空间列表

Use the command "kubectl change namespace" to switch to a different namespace.

in conclusion

In this article, we learned about namespaces, creating namespaces, changing the default namespace, inspecting Kubernetes resources in and out of namespaces, and we also saw how to create Kubernetes objects in a namespace of our choice (here for pods).

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132145214