Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
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:
1apiVersion: apps/v12kind: ReplicaSet3metadata:4 name: nginx-replicaset5spec:6 replicas: 37 selector:8 matchLabels:9 app: nginx10 template:11 metadata:12 labels:13 app: nginx14 spec:15 containers:16 - name: nginx17 image: nginxSave this as nginx-replicaset.yaml and apply it:
1kubectl apply -f nginx-replicaset.yaml2. Update Replicas in YAML#
To update the replicas to 4, modify the replicas field in the YAML file:
1spec:2 replicas: 4Apply the changes:
1kubectl apply -f nginx-replicaset.yaml3. Update Replicas via Command Line#
To scale the ReplicaSet to 6 replicas using the command line:
1kubectl scale replicaset nginx-replicaset --replicas=6Deployment 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:
1apiVersion: apps/v12kind: Deployment3metadata:4 name: nginx5 labels:6 tier: backend7spec:8 replicas: 39 selector:10 matchLabels:11 app: v112 template:13 metadata:14 labels:15 app: v116 spec:17 containers:18 - name: nginx19 image: nginx:1.23.0Save this as nginx-deployment.yaml and apply it:
1kubectl apply -f nginx-deployment.yaml2. List the Deployment#
Verify the Deployment:
1kubectl get deployments3. Update the Image#
Update the image to nginx:1.23.4:
1kubectl set image deployment/nginx nginx=nginx:1.23.44. Verify the Rollout#
Check the rollout status:
1kubectl rollout status deployment/nginx5. Assign Change Cause#
Record the change cause:
1kubectl annotate deployment/nginx kubernetes.io/change-cause="Pick up patch version"6. Scale the Deployment#
Scale to 5 replicas:
1kubectl scale deployment/nginx --replicas=57. View Rollout History#
Check the rollout history:
1kubectl rollout history deployment/nginx8. Rollback to Revision 1#
Rollback to the first revision:
1kubectl rollout undo deployment/nginx --to-revision=19. Verify Image Version#
Check the current image version:
1kubectl describe deployment nginx | grep Image:Troubleshooting Exercises#
Let’s troubleshoot some common issues with Deployments.
Issue 1: Invalid Kind#
Apply the following YAML:
1apiVersion: v12kind: Deployment3metadata:4 name: nginx-deploy5 labels:6 env: demo7spec:8 template:9 metadata:10 labels:11 env: demo12 name: nginx13 spec:14 containers:15 - image: nginx16 name: nginx17 ports:18 - containerPort: 8019 replicas: 320 selector:21 matchLabels:22 env: demoYou’ll encounter an error because the apiVersion and kind don’t match. To fix this:
- Change
apiVersion: v1toapiVersion: apps/v1 - Apply the corrected YAML
Issue 2: Mismatched Labels#
Apply the following YAML:
1apiVersion: v12kind: Deployment3metadata:4 name: nginx-deploy5 labels:6 env: demo7spec:8 template:9 metadata:10 labels:11 env: demo12 name: nginx13 spec:14 containers:15 - image: nginx16 name: nginx17 ports:18 - containerPort: 8019 replicas: 320 selector:21 matchLabels:22 env: devThis 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.envfromdevtodemoto match the Pod template labels - Change
apiVersion: v1toapiVersion: 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.