Friday 24 July 2020

ReplicationController, ReplicationSet automatically creates new Pod

Replication controller allows us to run multiple instances of a single Pod in Kubernetes Cluster.

 

When currently running Pod fails, ReplicationController (or) ReplicationSet automatically spin up other Pod instance.

 

Let’s see this with an example.

 

Step 1: Create a ReplicationSet that spin up 5 pods of jboss/wildfly instance.

 

employeeServiceReplicationSet.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

Step 2: Let’s create ReplicaSet from above definition file.

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

Query replicaset and Pods to confirm Pods are started and running.

$kubectl get rs
NAME                           DESIRED   CURRENT   READY   AGE
employee-service-replica-set   5         5         5       58s
$
$kubectl get pods
NAME                                 READY   STATUS    RESTARTS   AGE
employee-service-replica-set-66p9h   1/1     Running   0          63s
employee-service-replica-set-g5kgm   1/1     Running   0          63s
employee-service-replica-set-gpdzz   1/1     Running   0          63s
employee-service-replica-set-n49mv   1/1     Running   0          63s
employee-service-replica-set-ngfc8   1/1     Running   0          63s

Step 3: Now, let’s delete couple of Pods to confirm that ReplicaSet starts new pods to maintain replication of 5.

$kubectl delete pod employee-service-replica-set-66p9h
pod "employee-service-replica-set-66p9h" deleted
$
$kubectl delete pod employee-service-replica-set-g5kgm
pod "employee-service-replica-set-g5kgm" deleted

Query for the pods again. You can observe that ReplicaSet create new pods to maintain 5 replicas.

$kubectl get pods
NAME                                 READY   STATUS              RESTARTS   AGE
employee-service-replica-set-gpdzz   1/1     Running             0          2m3s
employee-service-replica-set-kqvxw   1/1     Running             0          11s
employee-service-replica-set-n49mv   1/1     Running             0          2m3s
employee-service-replica-set-ngfc8   1/1     Running             0          2m3s
employee-service-replica-set-r67n7   0/1     ContainerCreating   0          4s
$
$kubectl get pods
NAME                                 READY   STATUS    RESTARTS   AGE
employee-service-replica-set-gpdzz   1/1     Running   0          2m10s
employee-service-replica-set-kqvxw   1/1     Running   0          18s
employee-service-replica-set-n49mv   1/1     Running   0          2m10s
employee-service-replica-set-ngfc8   1/1     Running   0          2m10s
employee-service-replica-set-r67n7   1/1     Running   0          11s

You can delete replicaset by executing below command.

kubectl delete rs employee-service-replica-set

$kubectl delete rs employee-service-replica-set
replicaset.apps "employee-service-replica-set" deleted


Previous                                                    Next                                                    Home

No comments:

Post a Comment