What is a Pod in Kubernetes?
A Pod is the smallest deployable unit in Kubernetes. It represents a single instance of a running process in your cluster and can contain one or more containers.
Understanding the Pod lifecycle is crucial for effectively managing applications in Kubernetes. This guide covers the different states a Pod can be in, how Pods are created, and how they are terminated.
Pod Creation Process
Create Pod
User creates Pod definition
API Server
Validates and processes request
etcd
Stores Pod information
Scheduler
Assigns Pod to a node
Kubelet
Creates containers on node
Detailed Creation Steps:
- Pod Definition: You create a Pod configuration (YAML/JSON) and submit it to the Kubernetes API.
- API Server Validation: The API server validates the Pod specification.
- etcd Storage: The Pod information is written to etcd, Kubernetes' key-value store.
- Scheduler Assignment: The scheduler finds an appropriate node for the Pod based on resource requirements and constraints.
- Kubelet Execution: The kubelet on the assigned node pulls the Pod specification and instructs the container runtime to create the containers.
- Container Runtime: The container runtime (like Docker or containerd) creates the containers.
- Status Update: The kubelet reports the Pod status back to the API server, which updates etcd.
Pod Deletion Process
Delete Pod
User requests deletion
Terminating
Pod marked as terminating
TERM Signal
Graceful shutdown begins
SIGKILL
Forceful termination
Remove from etcd
Pod record deleted
Service Update
Removed from endpoints
Detailed Deletion Steps:
- Delete Request: You send a delete command for a Pod.
- Terminating State: The Pod is marked as "Terminating" but continues to run.
- Grace Period: A default 30-second grace period begins (configurable).
- TERM Signal: The kubelet sends a TERM signal to the main process in each container.
- PreStop Hooks: Any PreStop hooks defined in the Pod specification are executed.
- Graceful Shutdown: Applications should use this time to complete ongoing operations.
- SIGKILL (if needed): If containers haven't terminated after the grace period, they receive a SIGKILL signal.
- Cleanup: The Pod is removed from etcd and from all service endpoints.
Note:
The graceful deletion process allows applications to shut down properly.
If you need to force delete a Pod, you can use the --force --grace-period=0 flags
with kubectl delete.
Pod Lifecycle Best Practices
For Smooth Startup
- Use readiness probes to indicate when a Pod is ready to receive traffic
- Implement liveness probes to detect and restart unhealthy containers
- Define appropriate resource requests and limits to help the scheduler
- Use init containers for setup tasks that must complete before the main container starts
For Graceful Shutdown
- Handle SIGTERM signals in your application to shut down gracefully
- Use preStop hooks for cleanup tasks before termination
- Set appropriate termination grace periods based on your application's needs
- Ensure your application stops accepting new connections when terminating
Pod States
Pending
The Pod has been accepted by the Kubernetes system, but one or more of its containers has not been created.
Running
The Pod has been bound to a node, and all of its containers have been created.
Succeeded
All containers in the Pod have terminated successfully with exit code 0.
Failed
All containers have terminated, and at least one container exited with a non-zero status.
Unknown
The state of the Pod could not be obtained, typically due to communication issues.
CrashLoopBackOff
The Pod started, crashed, started again, and then crashed again.