Mastering Kubernetes Deployments: ReplicaSets, Updates, and Rollbacks


Introduction
Kubernetes is a powerful container orchestration platform that automates application deployment, scaling, and management. In this blog, I will walk through an exercise covering ReplicaSets, Deployments, updating pods, rollout history, and troubleshooting issues in Kubernetes. These concepts are crucial for maintaining high availability and reliability in production environments.
Step 1: Creating and Updating a ReplicaSet
A ReplicaSet ensures that a specified number of pod replicas are running at any given time.
Creating a ReplicaSet with 3 Replicas
To create a ReplicaSet with 3 replicas of nginx, apply the following YAML:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
Apply the file:
kubectl apply -f nginx-replicaset.yaml
Updating the Replicas
Update replicas to 4 from the YAML file:
Modify the replicas
field to 4 and apply the change:
kubectl apply -f nginx-replicaset.yaml
Update replicas to 6 from the command line:
kubectl scale rs nginx-replicaset --replicas=6
Verify the changes:
kubectl get rs
Step 2: Creating and Managing a Deployment
A Deployment provides declarative updates for Pods and ReplicaSets. It helps in rolling updates and rollbacks, ensuring zero downtime.
Creating a Deployment with 3 Replicas
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
tier: backend
spec:
replicas: 3
selector:
matchLabels:
app: v1
template:
metadata:
labels:
app: v1
spec:
containers:
- name: nginx
image: nginx:1.23.0
ports:
- containerPort: 80
Apply the Deployment:
kubectl apply -f nginx-deployment.yaml
Verify that the correct number of replicas are running:
kubectl get deployments
Updating the Image Version
We now update the image to nginx:1.23.4
:
kubectl set image deployment/nginx-deployment nginx=nginx:1.23.4
Confirm that all replicas have updated successfully:
kubectl rollout status deployment/nginx-deployment
Assign a change cause:
kubectl annotate deployment nginx-deployment kubernetes.io/change-cause="Pick up patch version"
Scaling the Deployment to 5 Replicas
kubectl scale deployment nginx-deployment --replicas=5
Checking Deployment Rollout History
kubectl rollout history deployment/nginx-deployment
Rolling Back to Revision 1
kubectl rollout undo deployment/nginx-deployment --to-revision=1
Ensure the rollback by verifying the image version:
kubectl describe deployment nginx-deployment | grep Image
Step 3: Troubleshooting Deployment Issues
Issue 1: Missing selector
Field
Problem YAML:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
labels:
env: demo
spec:
template:
metadata:
labels:
env: demo
name: nginx
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
replicas: 3
selector:
matchLabels:
env: demo
Fix: The selector
field must match the labels used in the template:
selector:
matchLabels:
env: demo
Issue 2: Mismatched Labels
Problem YAML:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
labels:
env: demo
spec:
template:
metadata:
labels:
env: demo
name: nginx
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
replicas: 3
selector:
matchLabels:
env: dev
Fix: The labels inside the selector
must match the ones in template.metadata.labels
:
selector:
matchLabels:
env: demo
Key Takeaways
ReplicaSets maintain a stable set of pods.
Deployments allow declarative updates and rollbacks.
Scaling can be done via YAML or command line.
Rollouts ensure smooth updates.
Rollbacks help in reverting faulty updates.
Selectors and Labels must always match to avoid issues.
Subscribe to my newsletter
Read articles from ADITYA KALIDAS directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
