Skip to main content

Chapter 3.6 - Deployments

Learning Objectives

By the end of this chapter, you will be able to:

  • Understand the role of Deployments
  • Create and manage Deployments
  • Perform rolling updates
  • Manage rollbacks
  • Scale Deployments

What is a Deployment?

A Deployment manages ReplicaSets and provides declarative update, rollback, and scaling capabilities.

Advantages:

  • Version management
  • Rolling updates
  • Automatic rollbacks
  • Easy scaling

YAML Declaration

apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
labels:
app: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.20
ports:
- containerPort: 80
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "512Mi"
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0

Rolling Updates

Process

Strategies

RollingUpdate (default):

  • Progressively replaces Pods
  • No service interruption
  • Configurable (maxSurge, maxUnavailable)

Recreate:

  • Deletes all old Pods
  • Creates new Pods
  • Service interruption

Updating a Deployment

Method 1: Update the Image

# Update the image
kubectl set image deployment/web-deployment nginx=nginx:1.21

# View the update status
kubectl rollout status deployment/web-deployment

Method 2: Edit the YAML

# Edit the Deployment
kubectl edit deployment web-deployment

# Or modify the file and apply
kubectl apply -f deployment.yaml

Method 3: Patch

# Modify a specific part
kubectl patch deployment web-deployment -p '{"spec":{"replicas":5}}'

Rollback

View History

# View rollout history
kubectl rollout history deployment/web-deployment

# Details of a revision
kubectl rollout history deployment/web-deployment --revision=2

Perform a Rollback

# Rollback to the previous version
kubectl rollout undo deployment/web-deployment

# Rollback to a specific revision
kubectl rollout undo deployment/web-deployment --to-revision=2

Scaling

Manual Scaling

# Increase the number of replicas
kubectl scale deployment web-deployment --replicas=5

# Or edit
kubectl edit deployment web-deployment

Automatic Scaling (HPA)

# Create a Horizontal Pod Autoscaler
kubectl autoscale deployment web-deployment --min=2 --max=10 --cpu-percent=80

Useful Commands

# View Deployments
kubectl get deployments

# Details of a Deployment
kubectl describe deployment web-deployment

# View created ReplicaSets
kubectl get replicasets

# Pause/Resume a rollout
kubectl rollout pause deployment/web-deployment
kubectl rollout resume deployment/web-deployment

Summary

In this chapter, you learned:

Deployment: Manages ReplicaSets with advanced features
Rolling Updates: Updates without interruption
Rollback: Return to a previous version
Scaling: Manual or automatic (HPA)
Management: kubectl set image, rollout, scale


Next Steps

Now that you have mastered Deployments:

Chapter 3.7: Deployment Strategies
Chapter 3.8: Health Checks


Troubleshooting

Common Problems with Deployments

Problem 1: Deployment does not create Pods

Symptoms:

kubectl get deployment
# NAME READY UP-TO-DATE AVAILABLE
# my-app 0/3 0 0

Solutions:

  1. Check the events:

    kubectl describe deployment my-app
    kubectl get events --sort-by='.lastTimestamp'
  2. Check the ReplicaSets:

    kubectl get replicasets
    kubectl describe replicaset <rs-name>
  3. Check resource quotas:

    kubectl describe quota

Problem 2: Rolling Update stalls

Symptoms:

  • The deployment remains in "Updating" state
  • New pods do not start

Solutions:

  1. Check the events:

    kubectl describe deployment my-app
  2. Check available resources:

    kubectl top nodes
    kubectl describe nodes
  3. Check health checks:

    • Probes (liveness/readiness) can prevent the deployment
    • Check the pod logs
  4. Force rollback if necessary:

    kubectl rollout undo deployment/my-app

Problem 3: Pods in CrashLoopBackOff

Symptoms:

kubectl get pods
# NAME READY STATUS RESTARTS
# my-app-xxx-123 0/1 CrashLoopBackOff 5

Solutions:

  1. Check the logs:

    kubectl logs <pod-name>
    kubectl logs <pod-name> --previous
  2. Check the configuration:

    kubectl describe pod <pod-name>
  3. Check resources:

    • CPU/Memory limits too low
    • Missing volumes
    • Incorrect Secrets/ConfigMaps

Problem 4: Scaling does not work

Symptoms:

kubectl scale deployment my-app --replicas=5
# But still 3 pods

Solutions:

  1. Check quotas:

    kubectl describe quota
  2. Check node resources:

    kubectl describe nodes
  3. Check constraints:

    kubectl get deployment my-app -o yaml
    # Check nodeSelectors, affinity, etc.

Previous chapter: Chapter 3.5 - ReplicaSets
Next chapter: Chapter 3.7 - Deployment Strategies
Back to module: Module 3 - Pods and Deployments


Chapter created: December 2024