Adding namespaces to the cluster

In Kubernetes, namespaces provide a mechanism for isolating groups of resources within a single cluster. Kubernetes namespaces can help different teams to share a Kubernetes cluster. Each team can be granted access to only those resources in their own namespace.

user icon Kubernetes administrator

By default, a Kubernetes cluster creates a default namespace when the cluster is created. To create a cluster by using kubeadmz init, see Initializing a Kubernetes cluster and a control plane.

To view the namespaces currently defined, you can use the following command to see the name, status, and age of a namespace:
kubectl get namespaces
When you have not added a new namespace to the cluster, you have the following output:
NAME                                               STATUS    AGE
default                                            Active    5d2h
kube-flannel                                       Active    5d2h
kube-node-lease                                    Active    5d2h
kube-public                                        Active    5d2h
kube-system                                        Active    5d2h
zoscp-zos-pod-admission-controller                 Active    5d2h
zoscp-zos-security-admission-controller            Active    5d2h

The namespaces kube-system, kube-flannel, zoscp-zos-pod-admission-controller, and zoscp-zos-security-admission-controller are restricted namespaces. This means that users are restricted from creating resources on the namespaces.

Creating a new namespace in a cluster

You can create a new namespace in a cluster by using kubectl create.

  1. On the command line, use the kubectl create namespace command to create a new namespace. For example, to create a namespace that is called development, you would run:
    kubectl create namespace development
    When the command is successful, you see the following output for this example:
    namespace "development" created
  2. To verify that the namespace has been created, you can use kubectl get namespaces. With the example of a namespace that is called development you see the following output:
    
    NAME                                               STATUS    AGE
    default                                            Active    5d2h
    kube-flannel                                       Active    5d2h
    kube-node-lease                                    Active    5d2h
    kube-public                                        Active    5d2h
    kube-system                                        Active    5d2h
    development                                        Active    1m
    zoscp-zos-pod-admission-controller                 Active    5d2h
    zoscp-zos-security-admission-controller            Active    5d2h

Deleting a namespace

If you want to delete a namespace that is no longer needed, you use kubectl delete. For example, to delete a namespace that is called development, you would run:
kubectl delete namespace development
When the command is successful, you see the following output for this example:
namespace "development" deleted

To verify that the namespace is deleted, use kubectl get namespaces.

For more information on the parameters and flags available for kubectl, see kubectl commands.