Pages

Tuesday, August 7, 2018

Kubernetes - ReplicaSet

A Replica Set is a term for API objects in K8 that refer to pod replicas. The basic idea of Replica Set is to control a set of Pod behavior. The Replica Set makes sure that a specified number of pods will always be running. If somehow one of the pod is crashed, another one will start automatically.

Replication Controller vs Replica Set
Both Replica Set and Replication Controller does the same thing. From version 1.2, Replica set does the job as Replication Controller. K8 even now allows to create replication controller now but it is advised to use Replica Set since they are up to date and have finer granularity of configuration

[root@manja17-I13330 kubenetes-config]# cat basic-replicaSet.yml
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  name: testing-service
  labels:
    version: 0.0.1
spec:
  replicas: 3
  template:
    metadata:
      labels:
        role: frontend
        env: dev
    spec:
      containers:
        - name: test-ser
          image: "docker.io/jagadesh1982/testing-service"
          ports:
           - containerPort: 9876

The important element in the above configuration is the “replicas: 3”. This  tells that at least 3 replicas of the container should be running all the time

Create the replica Set using,
[root@manja17-I13330 kubenetes-config]# kubectl create -f basic-replicaSet.yml
replicaset.extensions "testing-service" created

Now if we see pod list, we see that there are 3 pods running
[root@manja17-I13330 kubenetes-config]# kubectl get pods
NAME                                  READY     STATUS    RESTARTS   AGE
pod/testing-service-4gbkf     1/1          Running    0               10s
pod/testing-service-649jc     1/1          Running    0               10s
pod/testing-service-pcxwx    1/1          Running    0               10s

Check the replica set using,
[root@manja17-I13330 kubenetes-config]# kubectl get rs
NAME                                      DESIRED   CURRENT   READY     AGE
replicaset.apps/testing-service   3              3                3            10s

We can edit the replica set using,
[root@manja17-I13330 kubenetes-config]# kubectl edit rs testing-service
replicaset.extensions "testing-service" edited

I changed the replicas from 3 to 4. Once you edit the replica set, a new pod will be created automatically,
[root@manja17-I13330 kubenetes-config]# kubectl get pods
NAME                        READY       STATUS                     RESTARTS   AGE
testing-service-4gbkf   1/1           Running                     0                9m
testing-service-649jc   1/1           Running                     0                9m
testing-service-lh4l5    0/1           ContainerCreating       0                3s
testing-service-pcxwx  1/1           Running                     0                9m

No comments :

Post a Comment