DEV Community

Cover image for Kubernetes: ReplicaSet
Pratik Jagrut
Pratik Jagrut

Posted on • Originally published at psj.codes

Kubernetes: ReplicaSet

In the last blog, we explored Pods and how they encapsulate containers to run workloads on Kubernetes. While Pods provide useful features for running workloads, they also have inherent issues due to their ephemeral nature—they can be terminated at any time. When this happens, the user application will no longer be available.

To avoid such situations and ensure the user application is always available, Kubernetes uses ReplicaSets (RS). A ReplicaSet creates multiple identical replicas of a pod and ensures a specific number of pods are running at all times—neither fewer nor more.

A ReplicaSet controller continuously monitors the pods to ensure that the number of desired pods equals the number of available pods at all times. If a pod fails, the ReplicaSet automatically creates more pods. Conversely, if new pods with the same label are added and there are more pods than needed, the ReplicaSet will reduce the number by stopping the extra pods.

Configuring a ReplicaSet

Configuring a ReplicaSet involves defining a YAML file that specifies the desired state for the ReplicaSet. This YAML file includes crucial details such as the number of replicas, the selector to identify the pods managed by the ReplicaSet, and the pod template that defines the pods to be created.

Below is a sample YAML configuration for a ReplicaSet using an Nginx container:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-replicaset
  labels:
    app: nginx-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-pods
  template:
    metadata:
      labels:
        app: nginx-pods
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Breakdown of the YAML File

When writing any object in Kubernetes, you need to include certain required fields: apiVersion, kind, metadata, and spec.

  1. apiVersion:

    apiVersion: apps/v1
    

    This field specifies the version of the Kubernetes API that your object adheres to, ensuring compatibility with your Kubernetes cluster. In this case, it uses apps/v1.

  2. kind:

    kind: ReplicaSet
    

    This field defines the type of Kubernetes object being created. In our YAML file, it indicates that we are creating a ReplicaSet.

  3. metadata:

    metadata:
      name: nginx-replicaset
      labels:
        app: nginx-rs
    

    This section provides essential information about the ReplicaSet:

  • name: This uniquely identifies the ReplicaSet within its namespace (nginx-replicaset). This is the only field in metadata that is required.

  • namespace: Assigns a specific namespace for resource isolation (optional).

  • labels: Key-value pairs used to organize and select resources (app: nginx-rs).

  • annotations: These key-value pairs offer additional details about the Pod, useful for documentation, debugging, or monitoring (optional).

  • ownerReferences: Specifies the controller managing the Pod, establishing a relationship hierarchy among Kubernetes resources (optional).

  1. spec:

    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx-pods
      template:
        metadata:
          labels:
            app: nginx-pods
        spec:
          containers:
          - name: nginx
            image: nginx:latest
            ports:
            - containerPort: 80
    

    The spec section defines the desired state of the ReplicaSet, including its pods and their configurations:

  • replicas: Specifies the number of pod replicas that the ReplicaSet should maintain (3 in this case).

  • selector: Defines how the ReplicaSet identifies the pods it manages.

  • matchLabels: A set of key-value pairs used to match the pods (app: nginx-pods). This should be the same as the labels in template.metadata.labels.

    • template: Describes the pod's configuration to be created.

      • metadata: Includes labels to be applied to the pods.
      • spec: Defines the pod's configuration.

        • containers: Lists the containers within the pod.
        • name: Identifies the container (nginx).
        • image: Specifies the Docker image to use (nginx:latest).
        • ports: Indicates which ports should be exposed by the container (containerPort: 80).

      Additional optional fields for advanced configurations within the spec section include:

      • resources: Manages the pod's resource requests and limits.
      • volumeMounts: Specifies volumes to be mounted into the container's filesystem.
      • env: Defines environment variables accessible to the container.
      • volumes: Describes persistent storage volumes available to the pod.

Creating a ReplicaSet

To create a ReplicaSet using the above YAML configuration, save the configuration to a file named nginx-replicaset.yaml and apply it to the Kubernetes cluster using the following command:

kubectl apply -f nginx-replicaset.yaml
Enter fullscreen mode Exit fullscreen mode

Managing ReplicaSet

You can list the replica sets using kubectl get replicaset command.

❯ kubectl get replicasets
NAME               DESIRED   CURRENT   READY   AGE
nginx-replicaset   3         3         3       2m17s
Enter fullscreen mode Exit fullscreen mode

You can use the kubectl describe command to check the state of the replica set.

❯ kubectl describe replicasets.apps/nginx-replicaset
Name:         nginx-replicaset
Namespace:    default
Selector:     app=nginx-pods
Labels:       app=nginx-rs
Annotations:  <none>
Replicas:     3 current / 3 desired
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx-pods
  Containers:
   nginx:
    Image:        nginx:latest
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  13s   replicaset-controller  Created pod: nginx-replicaset-rgxzx
  Normal  SuccessfulCreate  13s   replicaset-controller  Created pod: nginx-replicaset-prddh
  Normal  SuccessfulCreate  13s   replicaset-controller  Created pod: nginx-replicaset-rwvpn
Enter fullscreen mode Exit fullscreen mode

You can list the pods using kubectl get pods command.

❯ kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-replicaset-xqcjm   1/1     Running   0          10s
nginx-replicaset-pzzhh   1/1     Running   0          10s
nginx-replicaset-k4lp4   1/1     Running   0          10s/
Enter fullscreen mode Exit fullscreen mode

Scaling the ReplicaSet

You can scale the ReplicaSet to a different number of replicas using the kubectl scale command:

❯ kubectl scale --replicas=5 replicaset nginx-replicaset
replicaset.apps/nginx-replicaset scaled

❯ kubectl get replicasets
NAME               DESIRED   CURRENT   READY   AGE
nginx-replicaset   5         5         3       17m

❯ kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-replicaset-xqcjm   1/1     Running   0          17m
nginx-replicaset-pzzhh   1/1     Running   0          17m
nginx-replicaset-k4lp4   1/1     Running   0          17m
nginx-replicaset-79d96   1/1     Running   0          16s
nginx-replicaset-sdb5g   1/1     Running   0          16s
Enter fullscreen mode Exit fullscreen mode

Updating the ReplicaSet

To update the ReplicaSet, such as changing the container image, you can modify the YAML file and apply the changes using:

kubectl apply -f nginx-replicaset.yaml
Enter fullscreen mode Exit fullscreen mode

Alternatively, use the kubectl set image command to update the image directly:

kubectl set image replicaset/nginx-replicaset nginx=nginx:1.19
Enter fullscreen mode Exit fullscreen mode

Deleting the ReplicaSet

To delete the ReplicaSet, use the following command:

kubectl delete rs nginx-replicaset
Enter fullscreen mode Exit fullscreen mode

Deleting the ReplicaSet will terminate all the pods it manages. If you want to keep the pods running after deleting the ReplicaSet, use the --cascade=orphan flag:

kubectl delete rs nginx-replicaset --cascade=orphan
Enter fullscreen mode Exit fullscreen mode

Scenarios Where RSs are Particularly Useful

  1. High Availability: ReplicaSets ensure that a specified number of pod replicas are always running, which is crucial for applications requiring high availability.

  2. Load Balancing: By maintaining multiple replicas of a pod, ReplicaSets help distribute the load evenly across all replicas, improving performance and reliability.

  3. Fault Tolerance: If a pod fails, the ReplicaSet automatically replaces it, ensuring continuous availability of the application.

  4. Rolling Updates: ReplicaSets can be used to perform rolling updates to applications, allowing updates without downtime by incrementally replacing old pods with new ones.

  5. Scalability: Easily scale the number of pod replicas up or down based on demand, ensuring efficient use of resources.

Conclusion

In summary, ReplicaSets play a crucial role in maintaining the desired state of your applications by ensuring a specified number of pod replicas are running at all times. This not only enhances the availability and reliability of your applications but also simplifies the management of pods in a Kubernetes environment.

Benefits of Using ReplicaSets:

  • Enhanced Reliability: Ensures continuous application availability by maintaining multiple pod replicas.

  • Improved Uptime: Automatically replaces failed pods to maintain the desired number of running pods.

  • Simplified Scaling: Allows easy scaling of applications by adjusting the number of replicas.

  • Consistent Performance: Distributes the load evenly across replicas, maintaining application performance.

By defining a ReplicaSet through a YAML file, you can easily control the number of replicas, monitor their status, and scale them as needed, ensuring your applications remain resilient and performant.

Thank you for reading this blog; your interest is greatly appreciated. I hope this information helps you on your Kubernetes journey. In the next blog, we'll explore Kubernetes deployments.

Top comments (0)