Sling Academy
Home/DevOps/Kubernetes: How to assign a namespace to a specific pod

Kubernetes: How to assign a namespace to a specific pod

Last updated: February 01, 2024

Introduction

In Kubernetes, namespaces provide a mechanism for isolating groups of resources within a single cluster. Namespaces are intended for use in environments with many users spread across multiple teams, or projects. In this guide, we are going to look at the way we can assign a specific namespace to a pod in Kubernetes.

Understanding Kubernetes Namespaces

Before diving into pods and namespaces, it’s important to have a basic understanding of what namespaces are and how they function. Namespaces are a way to divide cluster resources between multiple users. They provide a scope for names, and you use them to group resources that belong to similar projects, teams, or applications.

Default Namespaces

When you install Kubernetes, it creates three initial namespaces:

  • default for objects with no other namespace
  • kube-system for objects created by the Kubernetes system
  • kube-public for resources that should be publicly accessible to all users

Creating a Namespace

If you want to create a new namespace, you can do so using the kubectl create namespace command. Here’s a simple example:

kubectl create namespace my-namespace

Creating a Pod in a Specific Namespace

To create a pod in a specific namespace, you can specify the namespace in the YAML configuration file used to create the pod.

Example Pod Config File

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  namespace: my-namespace
spec:
  containers:
  - name: my-container
    image: my-image

When you apply this configuration using kubectl apply -f <filename>, the pod my-pod will be created in the my-namespace namespace.

Assigning Existing Pod to a New Namespace

Moving an existing pod from one namespace to another is not directly possible because the namespace of a pod is an immutable attribute. The common practice is to delete the existing pod and create a new instance of it in a different namespace.

Example

Below are the steps involved in deleting the existing pod and recreating it in another namespace:

1. Delete the existing Pod:

kubectl delete pod my-pod --namespace=default

2. Create a new instance of the Pod in the desired namespace:

kubectl apply -f <filename-with-namespace-set>

Using Labels and Selectors to Organize Pods within Namespaces

Another method to organize your pods within namespaces is to use labels and selectors. Labels are key/value pairs that are attached to objects, such as pods. You can define and use them to categorize and select subsets of objects.

Example:

apiVersion: v1
kind: Pod
metadata:
  name: my-labeled-pod
  namespace: my-namespace
  labels:
    app: my-web-app
spec:
  containers:
  - name: my-web-app-container
    image: nginx

When you create this pod, it will have the label app: my-web-app. You can use selectors to filter pods with this label across your namespaces.

For more advanced configurations, understanding of how to configure resource quotas, limit ranges, network policies, and role-based access control (RBAC) tied with namespaces will be necessary.

Final Words

Using namespaces is a core concept in Kubernetes that allows the partitioning of cluster resources. Always remember that when you are working with Kubernetes resources and you do not specify the namespace, it defaults to the default namespace. Keep this guide in mind any time you need to assign a namespace to a pod in your Kubernetes cluster.

Next Article: Fixing Kubernetes Error: ImagePullBackOff or ErrImagePull

Previous Article: Kubernetes error: Service account ‘default’ is forbidden

Series: Kubernetes Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide