šŸ” Deep Dive into Kubernetes: Understanding Namespaces and Pods

Komal PatraKomal Patra
4 min read

Kubernetes is like the operating system for your containerized applications. While it's a powerful beast that can scale, heal, and orchestrate your apps like a pro, it all starts with understanding its core components.

In this post, we’ll zoom in on two foundational elements of Kubernetes that every DevOps engineer or cloud developer should master: Namespaces and Pods. Let’s simplify the complex and get hands-on with practical commands and YAML examples!


🧱 What is a Kubernetes Namespace?

Imagine you’re working at a company where multiple teams share a single Kubernetes cluster. Chaos? Nope—Namespaces to the rescue!

Namespaces help you logically partition a single Kubernetes cluster. Think of them as isolated environments for your different projects, teams, or environments like dev, test, and prod.

šŸ” Key Features of Namespaces:

  • Resource Isolation: Keep dev traffic from interfering with production workloads.

  • Access Control: Attach policies and RBAC rules specific to a namespace.

  • Tenant Separation: Perfect for multi-tenant architectures.

  • Scoped Names: Same object name can exist in different namespaces.


šŸ“¦ Create a Namespace: Hands-on YAML

Let’s create a development namespace:

apiVersion: v1
kind: Namespace
metadata:
  name: nginx-dev-ns
  labels:
    name: development

Save this as nginx-dev-ns.yml, and apply it with:

kubectl apply -f nginx-dev-ns.yml

āœ… Done! You’ve just spun up your dev space.

Want to see all namespaces?

kubectl get ns

Users interacting with one namespace do not see the content in another namespace.

Now, let’s do the same for a production namespace:

apiVersion: v1
kind: Namespace
metadata:
  name: nginx-prod-ns
  labels:
    name: production

Apply with:

kubectl apply -f nginx-prod-ns.yml

🐳 Understanding Kubernetes Pods

Okay, so what’s a Pod? Think of a pod as a wrapper around one or more containers. It's the smallest deployable unit in Kubernetes.

While containers in Docker run standalone, in Kubernetes, containers live inside Pods.

🧠 Quick Facts:

  • Pods share network and storage.

  • They are ephemeral – designed to be temporary.

  • Typically, one container per pod, but multi-container pods are supported.

  • Kubernetes communicates with Pods—not individual containers.

we can create this pods in 2 way:

1. imperative ( run command)

2. Declarative (Manifest file)

File is created using below 4 parameters

\> apiVersion

\> kind

\> metadata

\> spec

apiVersion: API version used for the deployment object. apps/v1 is the stable API version for managing deployments. apiVersion refers to code library.

Kind: refers to k8's object that we want to create

metadata: additional information such as names & labels of kubernetes object.

specs: contains information regarding container information such as:

  • Docker image

  • environment variables

  • port mapping

  • Number of pods (Replica)

  • about container and that should run on which image

  • labelling the entire deployment


šŸš€ Creating Pods: The Imperative Way

You can launch a pod with a simple command:

kubectl run nginx --image=nginx:latest --namespace=nginx-dev-ns

This spins up an Nginx pod in the nginx-dev-ns namespace.

Didn’t specify a namespace? No worries—it goes to default.

To see the pods for all namespaces, use the commands:

kubectl get pods --all-namespaces

get more details about the pods using

kubectl describe pod nginx -n nginx-dev-ns

šŸ“œ Declarative Pod Creation with YAML

Want more control? Use a YAML manifest:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80

Save this as nginx-prod-pod.yml and deploy it like this:

kubectl apply -f nginx-prod-pod.yml -n nginx-prod-ns

šŸ” Common Pod Commands You’ll Use Daily

ActionCommand
List Podskubectl get pods -n <namespace>
View all Pods (all namespaces)kubectl get pods --all-namespaces
Describe a Podkubectl describe pod <pod-name> -n <namespace>
View logskubectl logs <pod-name> -n <namespace>
Access Pod terminalkubectl exec <pod-name> -i -t -- bash -il
Delete a Podkubectl delete pod <pod-name> -n <namespace>

Need wide output (including IPs, nodes, etc.)?

kubectl get pods -n nginx-dev-ns -o wide

🌐 Bonus: Exposing Pods with Services

Let’s say you want to make your Nginx pod accessible outside the cluster. Expose it using a service:

kubectl expose pod nginx --type=NodePort --port=80 --name=nginx-service

This creates a NodePort service, allowing access via a port on the cluster node’s IP.


šŸŽÆ Wrapping It Up

So far, we’ve:

āœ… Created Namespaces for dev and prod
āœ… Deployed Pods using imperative and declarative approaches
āœ… Learned essential kubectl commands to interact with and manage those pods

Whether you're just getting started or brushing up on your K8s basics, understanding these two core components lays the groundwork for working confidently in any Kubernetes environment.

Up next? We’ll explore Deployments and Services—stay tuned for part 2!


šŸ’¬ What would you like to learn next? Drop a comment or let me know if you'd like a deep dive into Deployments, Config Maps, or Services!

0
Subscribe to my newsletter

Read articles from Komal Patra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Komal Patra
Komal Patra