Pemrograman

Understanding Kubernetes Replica Controller dan Replicaset

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.

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.

apiVersion: v1
kind: ReplicationController
metadata:
  name: myapp-rc
  labels:
    app: myapp
    type: front-end
spec:
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        type: front-end
    spec:
      containers:
        - name: nginx-container
          image: nginx
  replicas: 3

We apply that Replica Controller on this terminal below.

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

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

➜ kubectl get rc
NAME       DESIRED   CURRENT   READY   AGE
myapp-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.

➜ kubectl get pods
NAME             READY   STATUS    RESTARTS   AGE
myapp-rc-j5zdv   1/1     Running   0          2m52s
myapp-rc-pxqcb   1/1     Running   0          2m52s
myapp-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.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
    app: myapp
    type: front-end
spec:
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        type: front-end
    spec:
      containers:
        - name: nginx-container
          image: nginx
  replicas: 3
  selector:
    matchLabels:
      type: front-end

Apply ReplicaSet to this file with the command

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

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

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

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

➜ kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
myapp-replicaset-2cglx   1/1     Running   0          80s
myapp-replicaset-mb4z8   1/1     Running   0          80s
myapp-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

    apiVersion: apps/v1
    kind: ReplicaSet
    metadata:
    name: myapp-replicaset
    labels:
        app: myapp
        type: front-end
    spec:
    template:
        metadata:
        name: myapp-pod
        labels:
            app: myapp
            type: front-end
        spec:
        containers:
            - name: nginx-container
            image: nginx
    replicas: 6
    selector:
        matchLabels:
        type: front-end
    

    then please run this command

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

    We can see ReplicaSet and Pods with this command

    ➜ kubectl get replicaset,pod
    NAME                               DESIRED   CURRENT   READY   AGE
    replicaset.apps/myapp-replicaset   6         6         6       5m55s
    
    NAME                         READY   STATUS    RESTARTS   AGE
    pod/myapp-replicaset-2cglx   1/1     Running   0          5m55s
    pod/myapp-replicaset-h8w9d   1/1     Running   0          101s
    pod/myapp-replicaset-mb4z8   1/1     Running   0          5m55s
    pod/myapp-replicaset-ppr52   1/1     Running   0          101s
    pod/myapp-replicaset-rbhlp   1/1     Running   0          101s
    pod/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

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

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

    ➜ kubectl get replicaset,pod                                            
    NAME                               DESIRED   CURRENT   READY   AGE
    replicaset.apps/myapp-replicaset   8         8         8       8m40s
    
    NAME                         READY   STATUS    RESTARTS   AGE
    pod/myapp-replicaset-2cglx   1/1     Running   0          8m40s
    pod/myapp-replicaset-h8w9d   1/1     Running   0          4m26s
    pod/myapp-replicaset-lxfrf   1/1     Running   0          22s
    pod/myapp-replicaset-mb4z8   1/1     Running   0          8m40s
    pod/myapp-replicaset-ppr52   1/1     Running   0          4m26s
    pod/myapp-replicaset-rbhlp   1/1     Running   0          4m26s
    pod/myapp-replicaset-rrqc2   1/1     Running   0          8m40s
    pod/myapp-replicaset-znr7n   1/1     Running   0          22s
    

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

    ➜  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.

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

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

➜  kubectl get replicaset,pod                   
NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/myapp-replicaset   8         8         7       25m

NAME                         READY   STATUS              RESTARTS   AGE
pod/myapp-replicaset-l568n   1/1     Running             0          33s
pod/myapp-replicaset-lxfrf   1/1     Running             0          17m
pod/myapp-replicaset-mb4z8   1/1     Running             0          25m
pod/myapp-replicaset-ppr52   1/1     Running             0          21m
pod/myapp-replicaset-rbhlp   1/1     Running             0          21m
pod/myapp-replicaset-rrqc2   1/1     Running             0          25m
pod/myapp-replicaset-t9pg5   0/1     ContainerCreating   0          2s
pod/myapp-replicaset-znr7n   1/1     Running             0          17m

We can see there are PODs will have ContainerCreating status that new PODs will be created.

comments powered by Disqus