DEV Community

Nishant Raj
Nishant Raj

Posted on

Mastering Kubernetes ReplicaSets and Deployments: Step-by-Step Guide with Troubleshooting

Efficiently managing workloads in Kubernetes involves understanding two core concepts: ReplicaSets and Deployments. This guide will walk you through creating, updating, and troubleshooting these resources with real-world examples.


What is a ReplicaSet?

A ReplicaSet is a Kubernetes resource that ensures a specified number of identical Pod replicas are running at all times. It is typically used to maintain high availability and scalability for applications.

Key Features of a ReplicaSet:

  • Ensures a consistent number of Pods are running.
  • Automatically replaces Pods if they fail.
  • Used as a building block for Deployments.

Part 1: Working with ReplicaSets

Task 1: Create a ReplicaSet with 3 Replicas

Objective: Use the nginx image to create a ReplicaSet with 3 replicas.

Step-by-Step Solution:

  1. Create a YAML configuration for the ReplicaSet and save it as replicaset.yml:
   apiVersion: apps/v1
   kind: ReplicaSet
   metadata:
     name: nginx-rs
     labels:
       env: demo
   spec:
     replicas: 3
     selector:
       matchLabels:
         env: demo
     template:
       metadata:
         labels:
           env: demo
       spec:
         containers:
         - name: nginx
           image: nginx
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • replicas: Specifies the desired number of Pods (3 in this case).
  • selector.matchLabels: Ensures the ReplicaSet manages only Pods with the env=demo label.
  • template: Defines the Pod template used to create replicas.
  1. Apply the configuration to create the ReplicaSet:
   kubectl apply -f replicaset.yml
Enter fullscreen mode Exit fullscreen mode
  1. Verify the creation of the ReplicaSet:
   kubectl get rs
Enter fullscreen mode Exit fullscreen mode

Task 2: Scale the ReplicaSet

Objective: Increase the number of replicas from 3 to 6.

Step-by-Step Solution:

  1. Edit the ReplicaSet using the following command:
   kubectl edit rs nginx-rs
Enter fullscreen mode Exit fullscreen mode
  1. Locate the replicas: 3 line and update it to:
   replicas: 6
Enter fullscreen mode Exit fullscreen mode
  1. Save and exit the editor. Kubernetes will automatically scale the ReplicaSet.

  2. Verify the updated ReplicaSet:

   kubectl get rs
   kubectl get pods
Enter fullscreen mode Exit fullscreen mode

What is a Deployment?

A Deployment is a Kubernetes resource that provides declarative updates to applications. It automates rolling updates, rollbacks, scaling, and self-healing for your workloads.

Key Features of a Deployment:

  • Manages ReplicaSets for ensuring desired Pod counts.
  • Simplifies rolling updates for application versions.
  • Tracks rollout history for safe rollbacks.

Part 2: Managing Deployments

Task 1: Create a Deployment with 3 Replicas

Objective: Deploy 3 replicas of the nginx application using the nginx:1.23.0 image.

Step-by-Step Solution:

  1. Create a YAML file named deployment.yml:
   apiVersion: apps/v1
   kind: Deployment
   metadata:
     name: nginx-deploy
     labels:
       tier: backend
   spec:
     replicas: 3
     selector:
       matchLabels:
         app: v1
     template:
       metadata:
         labels:
           app: v1
         name: nginx
       spec:
         containers:
         - name: nginx
           image: nginx:1.23.0
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • replicas: Defines the desired number of Pods.
  • selector.matchLabels: Ensures that the Deployment manages only Pods with the label app=v1.
  • template.metadata.labels: Defines labels for Pods created by this Deployment.
  1. Apply the configuration to create the Deployment:
   kubectl apply -f deployment.yml
Enter fullscreen mode Exit fullscreen mode
  1. Verify the Deployment and Pods:
   kubectl get deploy
   kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Task 2: Update the Deployment

Update the Image to nginx:1.23.4

  1. Use the kubectl set image command to update the Deployment:
   kubectl set image deploy/nginx-deploy nginx=nginx:1.23.4
Enter fullscreen mode Exit fullscreen mode
  1. Verify the updated Pods:
   kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Explanation:

This command updates the image for all Pods managed by the Deployment, rolling out the changes seamlessly.

Scale the Deployment to 5 Replicas

  1. Edit the Deployment:
   kubectl edit deploy nginx-deploy
Enter fullscreen mode Exit fullscreen mode
  1. Find the replicas: 3 line and change it to:
   replicas: 5
Enter fullscreen mode Exit fullscreen mode
  1. Save and exit. Kubernetes will scale the Deployment.

  2. Verify the new replica count:

   kubectl get deploy
   kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Task 3: Rollout History and Rollback

View Deployment Rollout History

kubectl rollout history deploy/nginx-deploy
Enter fullscreen mode Exit fullscreen mode

Revert Deployment to Revision 1

kubectl rollout undo deploy/nginx-deploy --to-revision=1
Enter fullscreen mode Exit fullscreen mode

Part 3: Troubleshooting YAML Issues

Issue 1: Incorrect apiVersion for Deployment

Problem: The YAML uses apiVersion: v1 instead of apps/v1.

Solution:

Update the apiVersion to apps/v1.


Issue 2: Label Mismatch in selector.matchLabels

Problem: The selector.matchLabels does not match the Pod template's labels.

Solution: Update selector.matchLabels to match the template's labels:

selector:
  matchLabels:
    app: v1
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this guide, you learned how to:

  1. Create and scale ReplicaSets.
  2. Deploy and update applications using Deployments.
  3. Troubleshoot common YAML issues.

Pro Tip:

  • Use kubectl describe to debug resource issues.
  • Leverage kubectl rollout commands to track changes and manage rollbacks.

By mastering these concepts, you can confidently manage Kubernetes workloads.

References:


Top comments (0)