Lab 3.3 - Deployments and Rolling Updates
Objectives
By the end of this lab, you will be able to:
- Create and manage Deployments
- Perform rolling updates
- Manage rollbacks
- Scale Deployments
Exercise 1: Create a Deployment
Step 1: Create the YAML file
Create deployment.yaml:
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
Step 2: Apply
kubectl apply -f deployment.yaml
Step 3: Verify
# View the Deployment
kubectl get deployment
# View the created ReplicaSets
kubectl get replicasets
# View the Pods
kubectl get pods -l app=web
Exercise 2: Rolling Update
Step 1: Update the image
# Update to nginx:1.21
kubectl set image deployment/web-deployment nginx=nginx:1.21
# Observe the process
kubectl rollout status deployment/web-deployment
Step 2: Observe the ReplicaSets
# View the ReplicaSets
kubectl get replicasets -w
# In another terminal, view the Pods
kubectl get pods -l app=web -w
Observation: A new ReplicaSet is created, Pods are replaced progressively.
Exercise 3: Rollback
Step 1: View the history
# Rollout history
kubectl rollout history deployment/web-deployment
# Details of a revision
kubectl rollout history deployment/web-deployment --revision=2
Step 2: Perform a rollback
# Rollback to the previous version
kubectl rollout undo deployment/web-deployment
# Verify
kubectl rollout status deployment/web-deployment
Step 3: Rollback to a specific revision
# Rollback to revision 1
kubectl rollout undo deployment/web-deployment --to-revision=1
Exercise 4: Scaling
Step 1: Manual scaling
# Increase to 5 replicas
kubectl scale deployment web-deployment --replicas=5
# Verify
kubectl get pods -l app=web
Step 2: Scaling via editing
# Edit the Deployment
kubectl edit deployment web-deployment
# Change replicas: 5 to replicas: 2
# Save and quit
Exercise 5: Pause and Resume
# Pause the rollout
kubectl rollout pause deployment/web-deployment
# Make modifications
kubectl set image deployment/web-deployment nginx=nginx:1.22
# Verify (nothing changes)
kubectl get pods -l app=web
# Resume the rollout
kubectl rollout resume deployment/web-deployment
# Observe
kubectl rollout status deployment/web-deployment
Cleanup
kubectl delete deployment web-deployment
Reflection Questions
- What is the difference between a Deployment and a ReplicaSet?
- How does a rolling update work?
- Why use a rollback?
- When to use pause/resume?
Next Steps
Lab 3.4: Configuring Health Checks
Chapter 3.8: Health Checks
Lab created: December 2024