Friday 24 July 2020

Kubernetes: ReplicaSet Monitor the Pods

This is a continuation of my previous post. In this post, we are going to see how ReplicaSet starts a new Pod instance when a Pod went down.

 

Step 1: Create ‘employeeServiceReplicationSet.yml’ file.

 

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

As you see the definition, I set Pod replicas to 5.

 

Step 2: Execute the below command to create a replicaset.

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

As you see the output, Replicaset created.

 

Step 3: list all the Pods.

$kubectl get pods
NAME                                 READY   STATUS    RESTARTS   AGE
employee-service-replica-set-5f5jg   1/1     Running   0          30s
employee-service-replica-set-fffwg   1/1     Running   0          30s
employee-service-replica-set-mq259   1/1     Running   0          30s
employee-service-replica-set-qlhvz   1/1     Running   0          30s
employee-service-replica-set-t6mvd   1/1     Running   0          30s

Step 4: let's delete one of the pods. When I delete a Pod, ReplicaSet will bringback new Pod.

$kubectl delete pod employee-service-replica-set-5f5jg
pod "employee-service-replica-set-5f5jg" deleted

Step 5: List all the pods.

$kubectl get pods
NAME                                 READY   STATUS              RESTARTS   AGE
employee-service-replica-set-7z4j8   0/1     ContainerCreating   0          4s
employee-service-replica-set-fffwg   1/1     Running             0          107s
employee-service-replica-set-mq259   1/1     Running             0          107s
employee-service-replica-set-qlhvz   1/1     Running             0          107s
employee-service-replica-set-t6mvd   1/1     Running             0          107s

As you see the output, a new container is getting created.

 

Wait for some time and query again. You can confirm that the new Pod is up and running.

$kubectl get pods
NAME                                 READY   STATUS    RESTARTS   AGE
employee-service-replica-set-7z4j8   1/1     Running   0          25s
employee-service-replica-set-fffwg   1/1     Running   0          2m8s
employee-service-replica-set-mq259   1/1     Running   0          2m8s
employee-service-replica-set-qlhvz   1/1     Running   0          2m8s
employee-service-replica-set-t6mvd   1/1     Running   0          2m8s

Previous                                                    Next                                                    Home

No comments:

Post a Comment