Friday 24 July 2020

Kubernetes: ReplicaSet and ReplicationController monitor pods that matches to selector labels

Pods created by ReplicationSet or ReplicationController are not tightly coupled to ReplicationController (or) ReplicationSet. By changing the labels, you can remove the pod from the monitoring eye of ReplicationController (or) ReplicationSet. Similarly, by changing the label, you can add a Pod to the monitoring eye of ReplicationController (or) ReplicationSet.

 

Let’s see this with an example.

 

replicaSetLabels.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: employee-service-replica-set
  labels:
    app: employee-service-replica-set
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: employee-service
      labels:
        app: employee-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: employee-service-container
        image: jboss/wildfly

  replicas: 5
  selector:
    matchLabels:
      app: employee-service

As you see, I mentioned label selector as ‘app: employee-service’

 

Let’s create a Pod definition from above definition file.

$kubectl create -f replicaSetLabels.yml 
replicaset.apps/employee-service-replica-set created

Query for Pods to confirm 5 Pods are created.

$kubectl get pods
NAME                                 READY   STATUS    RESTARTS   AGE
employee-service-replica-set-5xbxl   1/1     Running   0          6m
employee-service-replica-set-b5fkd   1/1     Running   0          6m
employee-service-replica-set-dfsv7   1/1     Running   0          6m
employee-service-replica-set-jbmt2   1/1     Running   0          6m
employee-service-replica-set-v48qm   1/1     Running   0          6m

Let’s change the label of a couple of the pods.

$kubectl label pod employee-service-replica-set-5xbxl app=app1 --overwrite
pod/employee-service-replica-set-5xbxl labeled
$
$kubectl label pod employee-service-replica-set-b5fkd app=app2 --overwrite
pod/employee-service-replica-set-b5fkd labelled

Let’s query Pods again.

$kubectl get pods
NAME                                 READY   STATUS              RESTARTS   AGE
employee-service-replica-set-49sq5   0/1     ContainerCreating   0          5s
employee-service-replica-set-5xbxl   1/1     Running             0          16m
employee-service-replica-set-b5fkd   1/1     Running             0          16m
employee-service-replica-set-dfsv7   1/1     Running             0          16m
employee-service-replica-set-h57v8   0/1     ContainerCreating   0          7s
employee-service-replica-set-jbmt2   1/1     Running             0          16m
employee-service-replica-set-v48qm   1/1     Running             0          16m

You can see that replicaset identify this change and immediately spin up two more Pods to make the replicas to 5.

 

Whatever the Pods, you changed ‘app’ label are no more monitored and managed by replicaset. You can even confirm by deleting the updated Pods.

 

What will happen if you delete the ReplicaSet?

The Pods managed by this replica set get deleted and the Pods that we changed the label ‘app’ still remain in Running state.
$kubectl get rs
NAME                           DESIRED   CURRENT   READY   AGE
employee-service-replica-set   5         5         5       26m
$
$kubectl delete rs employee-service-replica-set
replicaset.apps "employee-service-replica-set" deleted

You can confirm by querying Pods.




Previous                                                    Next                                                    Home

No comments:

Post a Comment