Sunday, May 13, 2018

Kubernetes - Replication Controller

Replication controller manage the life cycle of a POD. A Pod by itself will not have any lifecycle.
This replication controller will ensure that specified number of pods are running at any given
time. They do this by creating or deleting pods as required.

Create a manifest file with replication controller definition as,
[root@manja17-I13330 kubenetes-config]# cat basic-replicaController.yml
apiVersion: v1
kind: ReplicationController
metadata:
 name: testing-service
spec:
 replicas: 1
 template:
   metadata:
     labels:
       app: test-service
       version: "2"
   spec:
     containers:
       - name: test-ser
         image: "docker.io/jagadesh1982/testing-service"
         ports:
          - containerPort: 9876

In the above config file, we are telling to create a replication controller which should have 2
replicas of pods always running.

Create the replication controller using,
[root@manja17-I13330 kubenetes-config]# kubectl create -f basic-replicaController.yml
replicationcontroller "testing-service" created

Once i create the rc and see the pod details, we can see that 2 pods are being created.
[root@manja17-I13330 kubenetes-config]# kubectl get pods
NAME                    READY STATUS       RESTARTS AGE
testing-service-bmtsc   0/1 ContainerCreating   0 4s
testing-service-xkh6k   0/1 ContainerCreating   0 4s

To check the replication controller details we can use the below command as,
[root@manja17-I13330 kubenetes-config]# kubectl get rc
NAME              DESIRED CURRENT  READY AGE
testing-service   1 1  1 6s

If we want to scale the replica we can use,
[root@manja17-I13330 kubetesting]# kubectl scale --replicas=3 rc testing-service
replicationcontroller "my-myapp" scaled

This makes sure that at all time 3 replicas of the pod will be running. Check the replication
controller using,
[root@manja17-I13330 kubenetes-config]# kubectl get rc
NAME              DESIRED CURRENT  READY AGE
testing-service   3 3             3 3m

Now we can see that another pod will be created automatically.
Now if we delete a running pod ,
[root@manja17-I13330 kubenetes-config]# kubectl get pods
kNAME                    READY STATUS RESTARTS   AGE
testing-service-5tqxn   1/1 Running 0        47s
testing-service-bmtsc   1/1 Running 0        3m
testing-service-xkh6k   1/1 Running 0        3m

[root@manja17-I13330 kubenetes-config]# kubectl delete pod testing-service-xkh6k
pod "testing-service-xkh6k" deleted

And check for the pods, we can see that the pod we deleted is being terminating and another
new pod is being created. These creation is now automatically done by the replication Controller

[root@manja17-I13330 kubenetes-config]# kubectl get pods
NAME                     READY STATUS       RESTARTS AGE
testing-service-5tqxn   1/1 Running       0 57s
testing-service-bmtsc   1/1 Running        0 4m
testing-service-vbmkh  0/1 ContainerCreating    0 2s
testing-service-xkh6k   1/1 Terminating       0 4m

ReplicaSets are the next generation Replication controller which will be used and are based on
Set based label selector.

More to come , Happy learning :-)

No comments :

Post a Comment