DEV Community

Cover image for Deployments and Replica Sets in Kubernetes
Jensen Jose
Jensen Jose

Posted on

Deployments and Replica Sets in Kubernetes

Hello everyone! Welcome back to the eighth instalment in the CK 2024 series. Today we'll be delving into deployments and replica sets. For anyone working with Kubernetes or preparing for CK, this is one of the most important concepts to grasp. Hosting your application as a container on a pod, which is then backed by a replica set, stateful set, or deployment, is fundamental to Kubernetes.

Deployments and Replica Sets

In our last blog, we explored how a pod works by spinning up a pod on a Kubernetes node and performing some operations with it. Now, let’s take it a step further. Imagine a user accessing this particular pod. If something happens and the pod crashes, the user won’t get any response from the endpoint.

This is a significant drawback of running a standalone Docker container. In Kubernetes, we need a mechanism to ensure the user doesn't get an empty response even if the application crashes. That’s where a replication controller comes into play. It can automatically create a new pod if the existing one crashes, ensuring high availability by running multiple replicas of the pods.

Image description

Replication Controller

A replication controller ensures that a specified number of pod replicas are running at any given time. If a pod crashes, the replication controller will spin up a new one. This is crucial for maintaining high availability and load balancing. The replication controller is managed by the Kubernetes controller manager, which monitors resources and ensures they are running as expected.

Here’s a basic example of how a replication controller works:

  • High Availability: It ensures that multiple replicas of a pod are running, so if one pod fails, the others continue to serve traffic.
  • Load Balancing: Traffic is not directed to a single pod but distributed across multiple replicas.

Replica Set

While the replication controller is a legacy feature, the replica set is its modern counterpart. It offers more flexibility, allowing us to manage existing pods that weren’t originally part of the replica set. This is done through label selectors, enabling us to match the labels of running pods and include them in the replica set.

Here’s an example of how to define a replica set in YAML:

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

Enter fullscreen mode Exit fullscreen mode

Deployment

Deployments provide additional functionality over replica sets, such as rolling updates and rollbacks. A deployment manages a replica set, which in turn manages the pods. With deployments, we can update the application seamlessly without downtime.

For example, if we need to update the version of an application from 1.1 to 1.2, a deployment will handle this in a rolling update fashion, updating one pod at a time while the others continue to serve traffic.

Here’s how to define a deployment in YAML:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.1
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

To update the deployment, you can use the following command:

kubectl set image deployment/nginx-deploy nginx=nginx:1.2
Enter fullscreen mode Exit fullscreen mode

This command updates the image used by the deployment, triggering a rolling update.

Summary

In this blog, we've covered the importance of deployments and replica sets in Kubernetes. We looked at how replication controllers and replica sets ensure high availability and load balancing by managing multiple pod replicas. We also explored how deployments add value by enabling seamless updates and rollbacks.

In the upcoming instalments, we’ll dive deeper into services and how they work in Kubernetes. Happy learning!

For further reference, check out the detailed YouTube video here:

Top comments (0)