KubernetesBeginner

Understanding Kubernetes Pods

Learn about Pods, the smallest deployable units in Kubernetes. Understand their lifecycle, how to create them, and best practices for production workloads.

15 min read

What is a Pod?

A Pod is the smallest deployable unit in Kubernetes. It represents a single instance of a running process in your cluster. Pods contain one or more containers, such as Docker containers, with shared storage and network resources.

Key Concept

Think of a Pod as a logical host for your application. Containers within a Pod share the same IP address, port space, and can communicate via localhost.

Pod Lifecycle

Understanding the Pod lifecycle is crucial for debugging and managing your applications effectively. Here are the main phases:

Pod Phases

PendingPod accepted but containers not created yet
RunningPod bound to node, all containers created
SucceededAll containers terminated successfully
FailedAll containers terminated, at least one failed
UnknownPod state cannot be determined

Creating Pods

While you typically create Pods indirectly through Deployments or StatefulSets, understanding the Pod manifest is essential.

YAML Manifest

nginx-pod.yaml
1apiVersion: v1
2kind: Pod
3metadata:
4 name: nginx-pod
5 labels:
6 app: nginx
7spec:
8 containers:
9 - name: nginx
10 image: nginx:1.25
11 ports:
12 - containerPort: 80
13 resources:
14 limits:
15 memory: "128Mi"
16 cpu: "500m"
17 requests:
18 memory: "64Mi"
19 cpu: "250m"

Apply the manifest:

bash
kubectl apply -f nginx-pod.yaml
kubectl get pods
kubectl describe pod nginx-pod

Best Practices

1Always define resource requests and limits
2Use liveness and readiness probes for health checks
3Avoid running multiple containers unless tightly coupled
4Use Deployments instead of bare Pods for production
5Configure proper labels for organization and selection

Interview Questions

Q: What is the difference between a Pod and a Container?

A: A Pod is a Kubernetes abstraction that can contain one or more containers. Containers share the Pod's network namespace and storage volumes.

Q: When would you use multiple containers in a Pod?

A: Use multiple containers when they are tightly coupled (sidecar pattern), such as a logging agent, proxy, or helper container that needs to share resources with the main application.

Q: How do containers within a Pod communicate?

A: Containers within the same Pod share the network namespace, meaning they can communicate via localhost and the same port space.