Chapter 3.4 - Pod Lifecycle
Learning Objectives
By the end of this chapter, you will be able to:
- Understand the phases of a Pod
- Identify the states of a Pod
- Understand the conditions
- Manage restarts
Phases of a Pod
A Pod goes through several phases during its lifecycle:
Detailed States
1. Pending
The Pod has been accepted by Kubernetes but the containers are not yet created.
Possible reasons:
- Image being downloaded
- No available node
- Insufficient resources
2. Running
The Pod is bound to a node and all containers are created. At least one container is running.
3. Succeeded
All containers in the Pod have successfully completed and will not be restarted.
4. Failed
At least one container has terminated in failure.
5. Unknown
The state of the Pod cannot be obtained (communication issue with the node).
Pod Conditions
Conditions provide more details about the state:
conditions:
- type: PodScheduled
status: "True"
reason: ""
- type: Initialized
status: "True"
- type: ContainersReady
status: "True"
- type: Ready
status: "True"
Types of conditions:
- PodScheduled: The Pod has been assigned to a node
- Initialized: All init containers have completed
- ContainersReady: All containers are ready
- Ready: The Pod can serve traffic
Restart Policy
Determines when to restart containers:
spec:
restartPolicy: Always # Always restart
# restartPolicy: OnFailure # Restart only on failure
# restartPolicy: Never # Never restart
Init Containers
Init containers run before the main containers:
spec:
initContainers:
- name: init-db
image: busybox
command: ['sh', '-c', 'until nslookup mydb; do sleep 2; done']
containers:
- name: app
image: my-app:1.0
Use cases:
- Wait for a dependency to be ready
- Initialize the database
- Download configuration files
Useful Commands
# View the state of a Pod
kubectl get pod my-pod
# Full details with conditions
kubectl describe pod my-pod
# View events
kubectl get events --field-selector involvedObject.name=my-pod
# Logs of a crashed container
kubectl logs my-pod --previous
Summary
In this chapter, you learned:
Phases: Pending → Running → Succeeded/Failed
Conditions: PodScheduled, Initialized, ContainersReady, Ready
Restart Policy: Always, OnFailure, Never
Init Containers: Run before main containers
Diagnostics: kubectl describe, get events, logs --previous
Next Steps
Now that you understand the lifecycle:
Chapter 3.5: ReplicaSets - Managing Replication
Lab 3.1: Creating and Managing Pods
Chapter created on: December 2024