Skip to main content

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 Pod states
  • Understand conditions
  • Manage restarts

Pod Phases​

A Pod goes through several phases during its lifecycle:


Detailed States​

1. Pending​

The Pod has been accepted by Kubernetes but the containers have not yet been created.

Possible reasons:

  • Image being downloaded
  • No available node
  • Insufficient resources

2. Running​

The Pod is bound to a node and all containers have been created. At least one container is running.

3. Succeeded​

All containers in the Pod have terminated successfully and will not be restarted.

4. Failed​

At least one container has terminated with a failure.

5. Unknown​

The Pod state 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"

Condition types:

  • 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 Pod state
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 from 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 the 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: December 2024