Pod Lifecycle Management
Understanding Pod Lifecycle
Kubernetes pods undergo a complex lifecycle with multiple phases from creation to termination. Understanding these phases is crucial for effective container management and deployment strategies.
stateDiagram-v2
[*] --> Pending
Pending --> Running
Running --> Succeeded
Running --> Failed
Succeeded --> [*]
Failed --> [*]
Pod Phases
Phase |
Description |
Typical Scenario |
Pending |
Pod accepted but not running |
Resource allocation |
Running |
Pod scheduled and containers started |
Active workload |
Succeeded |
All containers completed successfully |
Batch jobs |
Failed |
At least one container failed |
Error conditions |
Pod Creation and Configuration
apiVersion: v1
kind: Pod
metadata:
name: lifecycle-demo
spec:
containers:
- name: nginx
image: nginx:latest
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Container started"]
preStop:
exec:
command: ["/bin/sh", "-c", "nginx -s quit"]
Managing Pod Restart Policies
spec:
restartPolicy: Always ## Default strategy
## Options: Always, OnFailure, Never
Deployment Strategies
## Create deployment
kubectl create deployment nginx-deployment --image=nginx:latest --replicas=3
## Scale deployment
kubectl scale deployment nginx-deployment --replicas=5
## Update deployment
kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
Pod Health Monitoring
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 10
periodSeconds: 5
readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 15
Resource Management
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"