Skip to content
Santekno.com | Level Up Your Engineering Skills
EN
📖 0%
Kubernetes for Beginners

Understanding Kubernetes Replica Controller dan Replicaset

· 5 min read ·Ihsan Arif

Replica Controller

Replica Controller is a mechanism used to replicate applications and we can handle it when our application has a failure or initializes a failed POD. So with this Replica controller we can adjust the scale of our running application.

For example, when one fails the other running applications will continue to run, regulated by the Replica Controller on the Kubernetes Cluster.

Then there is something called Replica Controller and there is also Replica Set, what’s the difference? Replica Set is a way to perform or create replication configurations.

Danger
The difference between a replica set and a replication controller is that a replica set supports set-based selector requirements whereas a replication controller only supports equality-based selector requirements.

Create a ReplicaSet

Once we know these terms, then we will try to implement them.

First, we create a YAML file rc-definition.yaml then fill the file with the code below.

yml
 1apiVersion: v1
 2kind: ReplicationController
 3metadata:
 4  name: myapp-rc
 5  labels:
 6    app: myapp
 7    type: front-end
 8spec:
 9  template:
10    metadata:
11      name: myapp-pod
12      labels:
13        app: myapp
14        type: front-end
15    spec:
16      containers:
17        - name: nginx-container
18          image: nginx
19  replicas: 3

We apply that Replica Controller on this terminal below.

bash
1➜ kubectl create -f code/replicasets/rc-definition.yaml
2replicationcontroller/myapp-rc created

We can see our Replica was running or not, you tried to apply command below.

bash
1➜ kubectl get rc
2NAME       DESIRED   CURRENT   READY   AGE
3myapp-rc   3         3         3       28s

On this showing terminal we can se 3 PDss was running and to make sure PODs already runngin we can tried this command below.

bash
1➜ kubectl get pods
2NAME             READY   STATUS    RESTARTS   AGE
3myapp-rc-j5zdv   1/1     Running   0          2m52s
4myapp-rc-pxqcb   1/1     Running   0          2m52s
5myapp-rc-rrcrj   1/1     Running   0          2m52s
We ccan see 3 PODs that running well with prefix name myapp-rc using auto generated by system.

Create Replica Set

We can create YAML file replicaset-definition.yaml then let’s fill the file with this code below.

yml
 1apiVersion: apps/v1
 2kind: ReplicaSet
 3metadata:
 4  name: myapp-replicaset
 5  labels:
 6    app: myapp
 7    type: front-end
 8spec:
 9  template:
10    metadata:
11      name: myapp-pod
12      labels:
13        app: myapp
14        type: front-end
15    spec:
16      containers:
17        - name: nginx-container
18          image: nginx
19  replicas: 3
20  selector:
21    matchLabels:
22      type: front-end

Apply ReplicaSet to this file with the command

bash
1➜ kubectl create -f code/replicasets/replicaset-definition.yaml 
2replicaset.apps/myapp-replicaset created

the we can see what will running or not with this command.

bash
1➜ kubectl get replicaset
2NAME               DESIRED   CURRENT   READY   AGE
3myapp-replicaset   3         3         3       41s

and you can see PODs already running well too on this command.

bash
1➜ kubectl get pods
2NAME                     READY   STATUS    RESTARTS   AGE
3myapp-replicaset-2cglx   1/1     Running   0          80s
4myapp-replicaset-mb4z8   1/1     Running   0          80s
5myapp-replicaset-rrqc2   1/1     Running   0          80s

If we want to Scale that ReplicaSet so, we have 2 options:

  1. Scale with edit YAML file for example you want ot scale to be 6 so, we change it like this

    yml
     1apiVersion: apps/v1
     2kind: ReplicaSet
     3metadata:
     4name: myapp-replicaset
     5labels:
     6    app: myapp
     7    type: front-end
     8spec:
     9template:
    10    metadata:
    11    name: myapp-pod
    12    labels:
    13        app: myapp
    14        type: front-end
    15    spec:
    16    containers:
    17        - name: nginx-container
    18        image: nginx
    19replicas: 6
    20selector:
    21    matchLabels:
    22    type: front-end

    then please run this command

    bash
    1➜  kubectl replace -f code/replicasets/replicaset-definition.yaml
    2replicaset.apps/myapp-replicaset replaced

    We can see ReplicaSet and Pods with this command

    bash
     1➜ kubectl get replicaset,pod
     2NAME                               DESIRED   CURRENT   READY   AGE
     3replicaset.apps/myapp-replicaset   6         6         6       5m55s
     4
     5NAME                         READY   STATUS    RESTARTS   AGE
     6pod/myapp-replicaset-2cglx   1/1     Running   0          5m55s
     7pod/myapp-replicaset-h8w9d   1/1     Running   0          101s
     8pod/myapp-replicaset-mb4z8   1/1     Running   0          5m55s
     9pod/myapp-replicaset-ppr52   1/1     Running   0          101s
    10pod/myapp-replicaset-rbhlp   1/1     Running   0          101s
    11pod/myapp-replicaset-rrqc2   1/1     Running   0          5m55s

    So, the PODs above already changes to 6.

  2. Direcly Scale with the command in the terminal

    bash
    1➜  kubectl scale --replicas=8 -f code/replicasets/replicaset-definition.yaml 
    2replicaset.apps/myapp-replicaset scaled

    So, we will se the replicase and PODs will be changed to 8

    bash
     1➜ kubectl get replicaset,pod                                            
     2NAME                               DESIRED   CURRENT   READY   AGE
     3replicaset.apps/myapp-replicaset   8         8         8       8m40s
     4
     5NAME                         READY   STATUS    RESTARTS   AGE
     6pod/myapp-replicaset-2cglx   1/1     Running   0          8m40s
     7pod/myapp-replicaset-h8w9d   1/1     Running   0          4m26s
     8pod/myapp-replicaset-lxfrf   1/1     Running   0          22s
     9pod/myapp-replicaset-mb4z8   1/1     Running   0          8m40s
    10pod/myapp-replicaset-ppr52   1/1     Running   0          4m26s
    11pod/myapp-replicaset-rbhlp   1/1     Running   0          4m26s
    12pod/myapp-replicaset-rrqc2   1/1     Running   0          8m40s
    13pod/myapp-replicaset-znr7n   1/1     Running   0          22s

    or you can set to alernative command on this terminal like this

    bash
    1➜  kubectl scale --replicas=8 replicaset myapp-replicaset

Uniqly, this ReplicaSet if you wan to to remove only one PODs so scale will be restart until the scale already to set, for example we have myapp-replicaset with replicas is 8 PODs and the we tried to delete 2 PODs with this command below.

bash
1➜   kubectl delete pod myapp-replicaset-2cglx myapp-replicaset-h8w9d
2pod "myapp-replicaset-2cglx" deleted
3pod "myapp-replicaset-h8w9d" deleted

So, PODs will be recreated new PODs automatically, so we can see with this command

bash
 1➜  kubectl get replicaset,pod                   
 2NAME                               DESIRED   CURRENT   READY   AGE
 3replicaset.apps/myapp-replicaset   8         8         7       25m
 4
 5NAME                         READY   STATUS              RESTARTS   AGE
 6pod/myapp-replicaset-l568n   1/1     Running             0          33s
 7pod/myapp-replicaset-lxfrf   1/1     Running             0          17m
 8pod/myapp-replicaset-mb4z8   1/1     Running             0          25m
 9pod/myapp-replicaset-ppr52   1/1     Running             0          21m
10pod/myapp-replicaset-rbhlp   1/1     Running             0          21m
11pod/myapp-replicaset-rrqc2   1/1     Running             0          25m
12pod/myapp-replicaset-t9pg5   0/1     ContainerCreating   0          2s
13pod/myapp-replicaset-znr7n   1/1     Running             0          17m
We can see there are PODs will have ContainerCreating status that new PODs will be created.

IH
Ihsan Arif
Backend engineer & penulis di Santekno. Aktif menulis tentang Go, Laravel, dan arsitektur backend modern.
Follow

💬 Comments