In the last article we studied about the smallest object in kubernetes i.e. pods. Now let us move to forward and know about more objects in kubernetes. Here in this article we are going to discuss ReplicaSets.
We already touched upon what kubernetes controllers are. They are brain behind running kubernetes. They are the processes that listens to other kubernetes objects and respond accordingly. In this article we are going to discuss one controller in particular and that is replication controller.
If you remember in the last article we created a pod. In that scenario, there was only a single pod and if that pod crashes, application will not be accessible to the users. We always want our application to be highly available. Here is where replication controller comes in to play as it helps in maintaining a specific number of pods of a particular type(replicas) to be always running. Doesn't matter if you want to have only one pod running, it will still make sure that one pod of an application is always running.
Replication controller helps in maintaining a desired pod count. It brings up a new pod if in case an existing pod fails. No matter whether the number of pods are one or many.
It also helps in load balancing the work load among different pods located on different nodes.
Now there are two ways of doing the same thing , one is Replication controller that we just discussed and the other one is Replica set which is the new recommended way. Replication controller is older concept and replica sets with some more flexibility are the good way to achieve the same thing.
Example (Replication Controller)
Below is the replication controller to deploy nginx with 3 replicas.
[root@node1 kubernetes]# cat replication_controller.yaml apiVersion: v1 kind: ReplicationController metadata: name: nginx spec: replicas: 3 selector: app: nginx template: metadata: name: nginx labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80 [root@node1 kubernetes]# kubectl create -f replication_controller.yaml replicationcontroller/nginx created [root@node1 kubernetes]# kubectl get rc NAME DESIRED CURRENT READY AGE nginx 3 3 1 21s [root@node1 kubernetes]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-26njn 1/1 Running 0 2m3s nginx-4w6dr 1/1 Running 0 2m4s nginx-bnzk8 1/1 Running 0 2m3s nginx-pod 1/1 Running 0 41m
[root@node1 kubernetes]# cat replica_set.yaml apiVersion: apps/v1 kind: ReplicaSet metadata: name: frontend labels: app: guestbook tier: frontend spec: replicas: 3 selector: matchLabels: tier: frontend template: metadata: labels: tier: frontend spec: containers: - name: php-redis image: gcr.io/google_samples/gb-frontend:v3 [root@node1 kubernetes]# kubectl create -f replica_set.yaml replicaset.apps/frontend created [root@node1 kubernetes]# kubectl get rs NAME DESIRED CURRENT READY AGE frontend 3 3 0 14s
Hey , there we have just made the application highly available , we have 3 pods running and now we can sleep peacefully without any worries :)