Pods & Deployments

25 minLesson 2 of 8

Learning Objectives

  • Create and manage Pods
  • Use Deployments for replica management
  • Perform rolling updates and rollbacks
  • Scale applications horizontally

Working with Pods

Creating a Pod

# nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
    environment: dev
spec:
  containers:
  - name: nginx
    image: nginx:1.25
    ports:
    - containerPort: 80
    resources:
      requests:
        memory: "64Mi"
        cpu: "100m"
      limits:
        memory: "128Mi"
        cpu: "200m"
# Create the pod
kubectl apply -f nginx-pod.yaml
 
# Check status
kubectl get pods
kubectl describe pod nginx-pod
 
# View logs
kubectl logs nginx-pod
 
# Execute into the pod
kubectl exec -it nginx-pod -- /bin/bash
 
# Delete the pod
kubectl delete pod nginx-pod

Multi-Container Pod

apiVersion: v1
kind: Pod
metadata:
  name: web-with-sidecar
spec:
  containers:
  - name: web
    image: nginx:1.25
    ports:
    - containerPort: 80
    volumeMounts:
    - name: shared-logs
      mountPath: /var/log/nginx
 
  - name: log-shipper
    image: busybox
    command: ['sh', '-c', 'tail -f /var/log/nginx/access.log']
    volumeMounts:
    - name: shared-logs
      mountPath: /var/log/nginx
 
  volumes:
  - name: shared-logs
    emptyDir: {}

Pod Lifecycle

Pending → Running → Succeeded/Failed
              ↓
          CrashLoopBackOff (restart loop)
PhaseMeaning
PendingScheduled but not yet running
RunningAt least one container is running
SucceededAll containers exited successfully
FailedAt least one container failed
UnknownPod state cannot be determined

Deployments

Deployments manage ReplicaSets, which manage Pods. They provide:

  • Desired state management
  • Rolling updates
  • Rollback capability
  • Scaling

Creating a Deployment

# app-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nextgen-app
  labels:
    app: nextgen-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nextgen-app
  template:
    metadata:
      labels:
        app: nextgen-app
    spec:
      containers:
      - name: app
        image: nginx:1.25
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: "64Mi"
            cpu: "100m"
          limits:
            memory: "128Mi"
            cpu: "200m"
# Create deployment
kubectl apply -f app-deployment.yaml
 
# Check status
kubectl get deployments
kubectl get replicasets
kubectl get pods -l app=nextgen-app
 
# Detailed info
kubectl describe deployment nextgen-app

Scaling

# Scale to 5 replicas
kubectl scale deployment nextgen-app --replicas=5
 
# Verify
kubectl get pods -l app=nextgen-app
# NAME                          READY   STATUS    RESTARTS   AGE
# nextgen-app-7d9f8b6c4-abc12  1/1     Running   0          5m
# nextgen-app-7d9f8b6c4-def34  1/1     Running   0          5m
# nextgen-app-7d9f8b6c4-ghi56  1/1     Running   0          5m
# nextgen-app-7d9f8b6c4-jkl78  1/1     Running   0          10s
# nextgen-app-7d9f8b6c4-mno90  1/1     Running   0          10s
 
# Scale down
kubectl scale deployment nextgen-app --replicas=2

Horizontal Pod Autoscaler

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: nextgen-app-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nextgen-app
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70

Rolling Updates

# Update the image
kubectl set image deployment/nextgen-app app=nginx:1.26
 
# Watch the rollout
kubectl rollout status deployment/nextgen-app
 
# Check rollout history
kubectl rollout history deployment/nextgen-app

Update Strategy in YAML

spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1          # Max pods above desired count during update
      maxUnavailable: 0    # Zero downtime

Rollbacks

# View revision history
kubectl rollout history deployment/nextgen-app
 
# Rollback to previous version
kubectl rollout undo deployment/nextgen-app
 
# Rollback to specific revision
kubectl rollout undo deployment/nextgen-app --to-revision=2
 
# Verify
kubectl get pods -l app=nextgen-app
kubectl describe deployment nextgen-app

Health Checks

spec:
  containers:
  - name: app
    image: nextgen-app:1.0
    ports:
    - containerPort: 3000
 
    # Is the container alive?
    livenessProbe:
      httpGet:
        path: /health
        port: 3000
      initialDelaySeconds: 15
      periodSeconds: 20
      failureThreshold: 3
 
    # Is the container ready to serve traffic?
    readinessProbe:
      httpGet:
        path: /ready
        port: 3000
      initialDelaySeconds: 5
      periodSeconds: 10
 
    # Has the container started?
    startupProbe:
      httpGet:
        path: /health
        port: 3000
      failureThreshold: 30
      periodSeconds: 10
ProbePurposeFailure Action
LivenessIs it alive?Restart container
ReadinessCan it serve traffic?Remove from service
StartupHas it started?Kill and restart

Summary

You've learned:

  • Creating and managing Pods (single and multi-container)
  • Using Deployments for replica management
  • Scaling applications manually and with autoscalers
  • Performing rolling updates and rollbacks
  • Configuring health checks for reliability

Next Steps

Next, we'll expose applications with Services and configure networking.