Working with Kubernetes Namespaces

Ramesh Babu Chayapathi
4 min readJul 7, 2023

--

In this short article, we will take a look at namespaces in Kubernetes, explain what they are and how to create them on the command line and in a configuration file, with some quick reference command examples using kubectl.

What are Kubernetes Namespaces?

Namespaces in Kubernetes allow objects to be grouped. Namespaces can be used for logical groupings, for example, to denote multiple environments like dev, qa, or prod, or for grouping objects that make up an application, like this blog!

Commonly a combination of both the above examples is used. For example, if an application is called blog, then it could be segmented using namespaces blog-dev, blog-qa, and blog-prod.

Namespaces can be created using the command line tool kubectl or using a YAML configuration file.

The Kubernetes documentation describes namespaces:

In Kubernetes, namespaces provides a mechanism for isolating groups of resources within a single cluster. Names of resources need to be unique within a namespace, but not across namespaces. Namespace-based scoping is applicable only for namespaced objects (e.g. Deployments, Services, etc) and not for cluster-wide objects (e.g. StorageClass, Nodes, PersistentVolumes, etc).

If a resource is namespaced, that means it cannot be created without a namespace.

Working with Kubernetes Namespaces Using kubectl

Note that on the command line, if a namespace is not specified for a given kubectl command, then objects from the default namespace are shown.

View existing namespaces.

To see objects within a specific namespace, the name of the namespace must be specified. It is considered bad practice to create objects in the default namespace as it makes it harder to implement NetworkPolicies, use RBAC, and segregate objects.

# View existing namespaces
kubectl get namespaces

Here I have a namespace listed called ingress-nginx.

Note that I also see the four Kubernetes namespaces that come out of the box:

  • default — The default namespace set by the system.
  • kube-system — This namespace is assigned to resources that are created by the Kubernetes system.
  • kube-public — This namespace is created by the system and is visible to all users, even users that aren’t authenticated.

kube-node-lease — This namespace holds lease objects associated with each node. These leases allow the kubelet to send heartbeats so that you can determine node availability.

2. List the pods contained in a namespace.

To view the pods within the namespace:

# List the pods contained in a namespace
kubectl get pods --namespace ingress-nginx

# Note the short format for namespace can be used (-n)
kubectl get pods -n ingress-nginx

3. List pods in the default namespace.

To view pods in the default namespace (no namespace specified):

# List pods in the default namespace
kubectl get pods

Note that my three pods in the ingress-nginx namespace are not displayed here.

4. Create a new namespace.

To create a new namespace:

# Create a new namespace called jacks-blog
kubectl create namespace jacks-blog

The namespace’s name can be created with a maximum length of 63 characters using only alphanumeric characters and hyphens. Names cannot start with a hyphen, and the alpha characters can only be lowercase.

5. Delete a namespace.

And to delete a namespace:

# Delete a namespace called jacks-blog
kubectl delete namespace jacks-blog

6. See details about a namespace.

To see details about a namespace:

# Describe a namespace
kubectl describe namespace nginx-ingress

7. Show resource usage of pods in a namespace.

To see the resource usage (CPU / Memory) of pods in a particular namespace:

# Show resource usage of pods in ingress-nginx namespace
kubectl top pod --namespace=ingress-nginx

Creating a Namespace Using a YAML Configuration File

Creating a namespace in a YAML configuration is defined as the example below:

# namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: jacks-blog
# Apply the namespace.yaml file
kubectl apply -f namespace.yml

Key Points

Namespaces are easy to work with in Kubernetes and can be manipulated using the kubectl command line tool, or declared in YAML configuration files. They allow the separation and logical organization of groups of resources. Effective use of namespaces can make cluster management more streamlined.

--

--

No responses yet