681 words
3 minutes
Mastering Kubernetes ReplicaSets and Deployments

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:

Terminal window
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:

Terminal window
kubectl apply -f nginx-replicaset.yaml

3. Update Replicas via Command Line#

To scale the ReplicaSet to 6 replicas using the command line:

Terminal window
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:

Terminal window
kubectl apply -f nginx-deployment.yaml

2. List the Deployment#

Verify the Deployment:

Terminal window
kubectl get deployments

3. Update the Image#

Update the image to nginx:1.23.4:

Terminal window
kubectl set image deployment/nginx nginx=nginx:1.23.4

4. Verify the Rollout#

Check the rollout status:

Terminal window
kubectl rollout status deployment/nginx

5. Assign Change Cause#

Record the change cause:

Terminal window
kubectl annotate deployment/nginx kubernetes.io/change-cause="Pick up patch version"

6. Scale the Deployment#

Scale to 5 replicas:

Terminal window
kubectl scale deployment/nginx --replicas=5

7. View Rollout History#

Check the rollout history:

Terminal window
kubectl rollout history deployment/nginx

8. Rollback to Revision 1#

Rollback to the first revision:

Terminal window
kubectl rollout undo deployment/nginx --to-revision=1

9. Verify Image Version#

Check the current image version:

Terminal window
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:

  1. Change apiVersion: v1 to apiVersion: apps/v1
  2. 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:

  1. Change the selector.matchLabels.env from dev to demo to match the Pod template labels
  2. Change apiVersion: v1 to apiVersion: apps/v1
  3. 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.

References#

  1. Kubernetes ReplicaSet Documentation
  2. Kubernetes Deployment Documentation
  3. Kubernetes Cheat Sheet
Deployment ReplicaSet (v1) ReplicaSet (v2) Pod Pod Pod Pod Pod
Mastering Kubernetes ReplicaSets and Deployments
https://mranv.pages.dev/posts/kubernetes-replicasets-deployments-hands-on/
Author
Anubhav Gain
Published at
2024-09-23
License
CC BY-NC-SA 4.0