š Deep Dive into Kubernetes: Understanding Namespaces and Pods


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
Action | Command |
List Pods | kubectl get pods -n <namespace> |
View all Pods (all namespaces) | kubectl get pods --all-namespaces |
Describe a Pod | kubectl describe pod <pod-name> -n <namespace> |
View logs | kubectl logs <pod-name> -n <namespace> |
Access Pod terminal | kubectl exec <pod-name> -i -t -- bash -il |
Delete a Pod | kubectl 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!
Subscribe to my newsletter
Read articles from Komal Patra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
