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.
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.
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: 3We apply that Replica Controller on this terminal below.
1➜ kubectl create -f code/replicasets/rc-definition.yaml
2replicationcontroller/myapp-rc createdWe can see our Replica was running or not, you tried to apply command below.
1➜ kubectl get rc
2NAME DESIRED CURRENT READY AGE
3myapp-rc 3 3 3 28sOn this showing terminal we can se 3 PDss was running and to make sure PODs already runngin we can tried this command below.
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 2m52smyapp-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.
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-endApply ReplicaSet to this file with the command
1➜ kubectl create -f code/replicasets/replicaset-definition.yaml
2replicaset.apps/myapp-replicaset createdthe we can see what will running or not with this command.
1➜ kubectl get replicaset
2NAME DESIRED CURRENT READY AGE
3myapp-replicaset 3 3 3 41sand you can see PODs already running well too on this command.
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 80sIf we want to Scale that ReplicaSet so, we have 2 options:
Scale with edit YAML file for example you want ot scale to be 6 so, we change it like this
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-endthen please run this command
1➜ kubectl replace -f code/replicasets/replicaset-definition.yaml 2replicaset.apps/myapp-replicaset replacedWe can see ReplicaSet and Pods with this command
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 5m55sSo, the PODs above already changes to 6.
Direcly Scale with the command in the terminal
1➜ kubectl scale --replicas=8 -f code/replicasets/replicaset-definition.yaml 2replicaset.apps/myapp-replicaset scaledSo, we will se the
replicaseand PODs will be changed to 81➜ 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 22sor you can set to alernative command on this terminal like this
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.
1➜ kubectl delete pod myapp-replicaset-2cglx myapp-replicaset-h8w9d
2pod "myapp-replicaset-2cglx" deleted
3pod "myapp-replicaset-h8w9d" deletedSo, PODs will be recreated new PODs automatically, so we can see with this command
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 17mContainerCreating status that new PODs will be created.