Pod Structure
Kubernetes pods are the fundamental building blocks for deploying applications, encapsulating one or more containers, storage resources, and network configurations. A pod is treated as a single entity by the Kubernetes scheduler, ensuring all its components run on the same node. Understanding pod structure and lifecycle is critical for managing application behavior and troubleshooting issues.
Pod Structure¶
A pod consists of three primary components:
1. Containers¶
Each pod contains one or more containers that share the same network namespace and storage volumes. Containers within a pod can communicate via localhost and share state through shared volumes.
Example:
containers:
- name: app-container
image: nginx
- name: sidecar-container
image: busybox
command: ["sh", "-c", "while true; do sleep 30; done"]
2. Volumes¶
Pods can mount persistent or ephemeral storage to share data between containers or persist data across container restarts. Volumes are defined in the pod specification and can be backed by local directories, cloud storage, or network file systems.
Example:
3. Init Containers¶
Init containers run before the main containers and are used for tasks like initializing data, downloading artifacts, or waiting for dependencies. They must complete successfully for the pod to proceed to the Running phase.
Example:
initContainers:
- name: init-myservice
image: busybox
command: ["sh", "-c", "until nslookup myservice; do sleep 1; done"]
Pod Lifecycle¶
A pod transitions through several phases until it is terminated. The lifecycle is managed by Kubernetes controllers (e.g., Deployments, StatefulSets).
1. Pending¶
The pod is waiting for resources (e.g., node availability, image pulls) or scheduling. Common causes:
- Node resources (CPU/memory) are insufficient.
- Image pull failures (e.g., invalid registry credentials).
- Node taints or labels prevent scheduling.
Command to check status:
2. Running¶
All containers are running, and the pod is ready to serve traffic. Kubernetes marks the pod as Running once all init containers complete and main containers start.
Command to inspect details:
3. Terminated¶
The pod is removed from the cluster. This occurs when the controller (e.g., Deployment) scales down the replica set or the pod is manually deleted. Terminated pods are no longer visible in kubectl get pods unless using --all or --field-selector.
Key takeaways¶
- Pods are the smallest deployable units in Kubernetes, grouping containers, volumes, and init containers.
- Lifecycle phases (
Pending,Running,Terminated) reflect the pod’s state and help diagnose deployment issues. - Shared resources (network, storage) within a pod enable collaboration between containers but require careful configuration.
- Use
kubectl describeandkubectl logsto troubleshoot pod status and container output.