Kubernetes Pod

A Pod is the smallest deployable unit in Kubernetes, wrapping one or more containers that share a network namespace and storage volumes.

A Pod is the atomic unit of deployment in Kubernetes. It wraps one or more containers that always run on the same node, share the same IP address and port space, and can share storage volumes. In practice, the vast majority of Pods run a single container. Multi-container Pods are used for tightly coupled helpers — a sidecar log shipper, a proxy, or an init container — that must co-locate with the main process.

Pod vs Container

Aspect Container Pod
Abstraction level OS process in an isolated namespace Kubernetes scheduling unit
Network Has its own network stack per container in Docker, but in Kubernetes shares the Pod network namespace Pods get one IP; all containers in a Pod share it
Lifecycle Managed by the container runtime Managed by the kubelet via the Kubernetes API
Rescheduling Not a Kubernetes concept Pods are not rescheduled when they fail; a controller (Deployment, ReplicaSet) must recreate them

Pod Lifecycle

A Pod moves through these phases:

Phase Meaning
Pending Pod accepted by the API server; waiting for scheduling or image pull
Running At least one container is running
Succeeded All containers have exited with code 0 (common for Jobs)
Failed At least one container exited with a non-zero code and will not be restarted
Unknown Pod status cannot be obtained (usually a node communication problem)

A Real Pod Manifest

apiVersion: v1
kind: Pod
metadata:
  name: spring-app
  namespace: default
  labels:
    app: spring-app
    version: "1.0"
spec:
  containers:
    - name: spring-app
      image: eclipse-temurin:21-jre
      command: ["java", "-jar", "/app/app.jar"]
      ports:
        - containerPort: 8080
      env:
        - name: SPRING_PROFILES_ACTIVE
          value: prod
      resources:
        requests:
          cpu: "250m"
          memory: "256Mi"
        limits:
          cpu: "500m"
          memory: "512Mi"
      livenessProbe:
        httpGet:
          path: /actuator/health/liveness
          port: 8080
        initialDelaySeconds: 30
        periodSeconds: 10
      readinessProbe:
        httpGet:
          path: /actuator/health/readiness
          port: 8080
        initialDelaySeconds: 10
        periodSeconds: 5
  restartPolicy: Always

Useful kubectl Commands

# Create a Pod from a manifest
kubectl apply -f spring-app.yaml

# List all Pods in the default namespace
kubectl get pods
# NAME         READY   STATUS    RESTARTS   AGE
# spring-app   1/1     Running   0          45s

# Show detailed info including events, probe results, container state
kubectl describe pod spring-app

# Stream logs from the container
kubectl logs -f spring-app

# Open a shell inside the running container
kubectl exec -it spring-app -- /bin/sh

# Delete the Pod
kubectl delete pod spring-app

Init Containers

Init containers run to completion before any regular container starts. They are useful for tasks like waiting for a database to be ready or pre-populating shared volumes.

spec:
  initContainers:
    - name: wait-for-db
      image: busybox:1.36
      command: ['sh', '-c', 'until nc -z postgres-svc 5432; do sleep 2; done']
  containers:
    - name: spring-app
      image: eclipse-temurin:21-jre
      # ... rest of spec

Common Mistakes

  • Creating bare Pods in production. A Pod has no self-healing. If the node it runs on fails, the Pod is gone permanently. Always use a Deployment or another controller.
  • Using the latest image tag. This makes rollbacks unpredictable and can cause inconsistent behavior across nodes. Pin to an explicit version.
  • Omitting resource requests and limits. Without requests, the scheduler cannot make accurate placement decisions. Without limits, one misbehaving container can exhaust a node's resources.
  • Forgetting health probes. Without a readinessProbe, a Service routes traffic to Pods that are not yet ready. Without a livenessProbe, deadlocked containers are never restarted.
  • Running multiple unrelated processes in one container. Each container should have a single responsibility. Use separate containers within the Pod for sidecar patterns.

Best Practices

  • Always specify resources.requests and resources.limits for CPU and memory.
  • Define both livenessProbe and readinessProbe on every long-running container.
  • Use labels consistently; Services and Deployments use labels to find their Pods.
  • Avoid running containers as root; set securityContext.runAsNonRoot: true.
  • Use init containers for startup sequencing rather than sleep commands in the main container.

Interview Questions

1. What is a Pod in Kubernetes?

A Pod is the smallest deployable unit in Kubernetes. It encapsulates one or more containers that share a network namespace (the same IP and port space) and can share storage volumes. Kubernetes schedules Pods onto nodes, and the kubelet on each node is responsible for running the containers inside them.

2. What is the difference between a Pod and a Deployment?

A Pod is a single instance of a running workload with no self-healing. A Deployment is a higher-level controller that manages a ReplicaSet, which in turn ensures a desired number of Pod replicas are always running. When a Pod fails, the Deployment controller creates a replacement. Deployments also manage rolling updates and rollbacks.

3. What are init containers and when would you use them?

Init containers are special containers that run to completion before the main containers start. They run sequentially, and if any init container fails, Kubernetes restarts it until it succeeds. Common uses: waiting for a downstream service to become available, running database migrations, or pre-populating a shared volume with configuration files.

4. What is a readinessProbe and how does it differ from a livenessProbe?

A readinessProbe determines whether a Pod is ready to receive traffic. When it fails, the Pod is removed from the Service's endpoint list but is not restarted. A livenessProbe determines whether a container is alive. When it fails, the kubelet kills and restarts the container. Both are essential: readiness prevents traffic from reaching an unready Pod; liveness recovers a deadlocked process.

5. Can Pods in the same Pod communicate over localhost?

Yes. All containers within a Pod share the same network namespace, so they share the same IP address. Container A can reach container B at localhost:<port>. This is the primary reason for the sidecar and ambassador patterns — the helper container runs alongside the main container and they communicate via localhost.

6. What happens when a Pod's node is lost?

If a Pod is a bare Pod (not managed by a controller), it is not rescheduled and is eventually marked as Unknown and deleted. If the Pod is managed by a Deployment or ReplicaSet, the controller detects that the desired replica count is not met and creates a new Pod on a healthy node.

7. What is restartPolicy and what are its options?

restartPolicy controls whether the kubelet restarts a container after it exits. Options are Always (restart on any exit — default for Deployments), OnFailure (restart only on non-zero exit codes — common for Jobs), and Never (never restart). It applies to the whole Pod, not individual containers.

Summary

  • A Pod wraps one or more containers that share a network and can share storage; it is the atomic unit Kubernetes schedules.
  • Pods are ephemeral — they should always be managed by a controller like Deployment or StatefulSet.
  • Define resources, livenessProbe, and readinessProbe on every production container.
  • Labels on Pods are the primary mechanism by which Services and controllers find and select Pods.
  • Init containers handle startup sequencing before the main application container starts.

Continue Learning

Previous: Kubernetes Architecture | Next: ReplicaSet

Was this guide helpful?

CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.