Mastering Kubernetes ReplicaSets and Deployments
In this hands-on guide, we’ll explore Kubernetes ReplicaSets and Deployments, two crucial concepts for managing containerized applications at scale. We’ll go through practical exercises and troubleshooting scenarios to deepen our understanding of these Kubernetes objects.
ReplicaSet Exercises
Let’s start with some exercises involving ReplicaSets.
1. Create a ReplicaSet
First, we’ll create a ReplicaSet based on the nginx image with 3 replicas:
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
Save this as nginx-replicaset.yaml
and apply it:
kubectl apply -f nginx-replicaset.yaml
2. Update Replicas in YAML
To update the replicas to 4, modify the replicas
field in the YAML file:
spec:
replicas: 4
Apply the changes:
kubectl apply -f nginx-replicaset.yaml
3. Update Replicas via Command Line
To scale the ReplicaSet to 6 replicas using the command line:
kubectl scale replicaset nginx-replicaset --replicas=6
Deployment Exercises
Now, let’s move on to Deployments, which provide declarative updates for Pods and ReplicaSets.
1. Create a Deployment
Create a Deployment named nginx
with 3 replicas:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
tier: backend
spec:
replicas: 3
selector:
matchLabels:
app: v1
template:
metadata:
labels:
app: v1
spec:
containers:
- name: nginx
image: nginx:1.23.0
Save this as nginx-deployment.yaml
and apply it:
kubectl apply -f nginx-deployment.yaml
2. List the Deployment
Verify the Deployment:
kubectl get deployments
3. Update the Image
Update the image to nginx:1.23.4
:
kubectl set image deployment/nginx nginx=nginx:1.23.4
4. Verify the Rollout
Check the rollout status:
kubectl rollout status deployment/nginx
5. Assign Change Cause
Record the change cause:
kubectl annotate deployment/nginx kubernetes.io/change-cause="Pick up patch version"
6. Scale the Deployment
Scale to 5 replicas:
kubectl scale deployment/nginx --replicas=5
7. View Rollout History
Check the rollout history:
kubectl rollout history deployment/nginx
8. Rollback to Revision 1
Rollback to the first revision:
kubectl rollout undo deployment/nginx --to-revision=1
9. Verify Image Version
Check the current image version:
kubectl describe deployment nginx | grep Image:
Troubleshooting Exercises
Let’s troubleshoot some common issues with Deployments.
Issue 1: Invalid Kind
Apply the following YAML:
apiVersion: 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
You’ll encounter an error because the apiVersion
and kind
don’t match. To fix this:
- Change
apiVersion: v1
toapiVersion: apps/v1
- Apply the corrected YAML
Issue 2: Mismatched Labels
Apply the following YAML:
apiVersion: 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
This will create a Deployment, but it won’t be able to manage any Pods due to mismatched labels. To fix this:
- Change the
selector.matchLabels.env
fromdev
todemo
to match the Pod template labels - Change
apiVersion: v1
toapiVersion: apps/v1
- Apply the corrected YAML
Conclusion
Through these exercises, we’ve explored the creation and management of ReplicaSets and Deployments in Kubernetes. We’ve also practiced troubleshooting common issues that can arise when working with these objects. Remember, proper label management and correct API versions are crucial for the smooth operation of your Kubernetes resources.