Deployment, ReplicaController, and ReplicaSet in K8s - CKA

Deployment, ReplicaController, and ReplicaSet in K8s - CKA

Introduction

Whenever our pods go down, users can't access the application, and the same problem occurs with standalone containers. But Kubernetes solves this problem by using ReplicationController, ReplicaSet, and Deployment. In this blog, we will learn about these essential Kubernetes components. Let's get started

ReplicationController

A ReplicationController (RC) ensures that a specified number of pod replicas are running at any given time. It manages the lifecycle of pods and ensures the desired state is maintained.

Key Features:

  • Ensures that a specified number of replicas are running.

  • If a pod goes down, it automatically creates a new one to replace it.

  • Supports rolling updates but requires manual intervention and scripting.

Example:

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-rc
  labels:
    env: demo
spec:
  template:
    metadata:
      name: nginx
      labels:
        env: demo
    spec:
      containers:
        - name: nginx
          image: nginx

  replicas: 3

Create a file named rc.yaml, paste the above content into the file, and run the command:

kubectl apply -f rc.yaml

This YAML file will create a ReplicationController that maintains three pods with the NGINX image.

To scale up the replicas, you can modify the YAML file and apply the changes with the kubectl apply -f rc.yaml command, or use the command line directly:

kubectl scale --replicas=10 rc/nginx-rc

You can also edit the running ReplicationController using:

kubectl edit rc/nginx-rc

To delete the ReplicationController, use:

kubectl delete rc/nginx-rc

To get the list of ReplicationControllers, use:

kubectl get rc

Replica Set

A Replica Set (RS) is the next-generation Replication Controller. It also ensures that a specified number of pod replicas are running but offers more expressive pod selector capabilities.

Key Features:

  • Ensures a specified number of pod replicas are running.

  • Supports set-based selectors, allowing more complex queries to identify pods.

  • Typically, not used directly; instead, it is managed by Deployments.

Example:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-rs
  labels:
    env: demo
spec:
  template:
    metadata:
      name: nginx
      labels:
        env: demo
    spec:
      containers:
        - name: nginx
          image: nginx

  replicas: 4
  selector:
    matchLabels:
      env: demo

Create a file named rs.yaml, paste the above content, and run the command:

kubectl apply -f rs.yaml

This YAML file will create a ReplicaSet with four pods containing the NGINX image.

Deployment

A Deployment provides declarative updates to applications and manages Replica Sets. It simplifies the process of rolling updates, rollbacks, scaling, and managing application versions.

Key Features:

  • Manages Replica Sets to provide declarative updates.

  • Supports rolling updates and rollbacks.

  • Can pause and resume updates.

  • Provides a mechanism to scale the number of replicas.

Example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy
  labels:
    tier: backend # it could anything in a form of key-value pair
spec:
  template:
    metadata:
      name: nginx
      labels:
        app: v1
    spec:
      containers:
        - name: nginx
          image: nginx:1.23.0

  replicas: 3
  selector:
    matchLabels:
      app: v1

Create a file deploy.yaml and paste the above content in the file. Then run the following command:

kubectl apply -f deploy.yaml

This YAML file will create a Deployment named nginx-deploy with three pods containing the nginx:1.23.0 image.

To get detailed information about your Deployment, use:

kubectl describe deploy/nginx-deploy  # or
kubectl desribe deploy nginx-deploy

To view the rollout history, use:

kubectl rollout history deploy/nginx-deploy

To roll back a Deployment to a previous revision, use:

kubectl rollout undo deploy/nginx-deploy

You can update the image via a command:

kubectl set image deployment/nginx-deploy nginx=nginx:1.23.4

Conclusion

ReplicationController, ReplicaSet, and Deployment are fundamental Kubernetes resources that help ensure high availability and scalability of applications. They provide mechanisms for maintaining desired states, scaling, and updating applications seamlessly.

Remember, you don't have to memorize these commands. Using the Kubernetes cheatsheet and regular hands-on practice will help you remember the most important commands.

Thank you for reading my blog. After reading this, please do some hands-on practice with the tasks provided. Happy learning!