DEV Community

ustundagsemih
ustundagsemih

Posted on • Originally published at ustundagsemih.com

Kubernetes Deployment & ReplicaSet

What is a ReplicaSet?

A ReplicaSet's purpose is to maintain a stable set of replica Pods running at any given time. A ReplicaSet ensures that a specified number of pod replicas are running at any given time. We often don't need to create a ReplicaSet directly. Instead we use Deployment to manage a ReplicaSet. Let's first take a look at how should we create a ReplicaSet.

We will create a ReplicaSet to ensure there is always 3 instances of a nginx container.

Here .spec.selector is a label selector and it is used to identify potential Pods to acquire by the ReplicaSet. In our case it will look for the Pods which have the label tier: frontend.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-replicaset
  labels:
    tier: frontend
spec:
  replicas: 2
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: my-nginx
        image: nginx

Save this manifest into replicaset.yaml and apply it.

$ kubectl apply -f replicaset.yaml
replicaset.apps/nginx-replicaset created

$ kubectl get replicaset
NAME               DESIRED   CURRENT   READY   AGE
nginx-replicaset   2         2         2       19s

$ kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-replicaset-7db6b   1/1     Running   0          19s
nginx-replicaset-ncz56   1/1     Running   0          19s

We can see that our ReplicaSet has been deployed and there are 2 PODs just as we specified.

To the Deployments!

Now we have seen what is a ReplicaSet and how to create it, it is time to integrate it into a Deployment.

A Deployment provides declarative updates for Pods ReplicaSets.

Let's create a Deployment manifest. Instead of creating the manifest from scratch, we can use kubectl to create a template file for us. With the --dry-run flag, we tell kubectl to not create the resource. This way, we can easily modify the attributes later.

$ kubectl create deployment nginx-deployment --image=nginx --dry-run=client -o yaml > deployment.yaml
 $ kubectl get deploy
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   1/1     1            1           4m9s

$ kubectl get po
NAME                                READY   STATUS        RESTARTS   AGE
nginx-deployment-5969c7f455-j6685   1/1     Running       0          3m39s

After creting the deployment, we can see that only 1 Pod is created. This is because we didn't specify a value for replicas option. We can scale our deployment by editing the yaml file or we can use the imperative way.

$ kubectl scale deploy nginx-deployment --replicas=2
deployment.apps/nginx-deployment scaled

$ kubectl get deploy
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   2/2     2            2           12m

$ kubectl get po
NAME                                READY   STATUS        RESTARTS   AGE
nginx-deployment-5969c7f455-9nrs7   1/1     Running       0          71s
nginx-deployment-5969c7f455-j6685   1/1     Running       0          6m20ss

Top comments (0)