kubernetes-replicaset-tutorial-for-beginners

Kubernetes Replicaset : introduction

Kubernetes replicaset monitors a set of pods : if a pod crashes, the replicaset replaces it and guarantees that we have the required number of replicas (pods).

This is the pod definition file that we created in the kubernetes pod tutorial :

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    name: nginx
    tier: frontend
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
     - containerPort: 80

In order to create the replicaset definition file, we will reuse the pod definition file : we will use all the sections of the pod definition file except the apiVersion and the kind sections.

We paste the pod definition file in the template section of the replicaset definition file.

Also, we add two sections :

  • Replicas : the number of pods that the replicaset will manage
  • Selector : designs the pods that will be managed by the replicaset : those pods will have the label tier with the value frontend. If we check the pod definition part, we see that the pod has a label tier with the value frontend.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  template:
    metadata:
      name: nginx
      labels:
        name: nginx
        tier: frontend
    spec:
      containers:
        - name: nginx
          image: nginx
          ports:
           - containerPort: 80
  replicas: 3
  selector:
    matchLabels:
     tier: frontend

Kubernetes replicaset creation

We create a replicaset based on the definition file using this command :

Kubectl create -f replicaset-definition-file.yaml

kubectl-create-replicaset

We check our replicaset using this command that lists the available replicasets :

kubectl get replicasets

kubectl-get-replicaset

Here we see that we have a unique replicaset which name is frontend.
The replicaset manages three pods. Those pods are in the ready state.

Scale Kubernetes pods

Now, we have 3 pods declared in the replicaset definition file.

We aim to scale up to have 6 pods. We can do it using three methods :

1)     Method 1

  1.       Change the replicas in the replicaset definition file
  2.       Run kubectl replace -f replicaset-definition-file.yml

2)     Method 2

  •       Kubectl scale –replicas=6 -f replicaset-definition-file.yml

3)     Method 3 

  •       Kubectl scale –replicas=6 replicaset frontend ( frontend : the name of replicaset in the replicaset definition file)

We will use the first method to scale our replicaset :

kubectl-scale-replicaset

We check the number of pods created using the command kubectl get pods :

kubectl-get-pods

We notice that we have six kubernetes pods running.

If we delete a pod using the command : kubectl delete pod <pod_name>, we will notice that the replicaset will replace the deleted pod :

kubectl-delete-pod

Delete kubernetes replicaset

We use the following command in order to delete a replicaset :

kubectl delete replicaset <replicaset-name> 

kubectl-delete-replicaset

When we delete a replicaset, the pods that it manages will be deleted as well.

If we check the presence of these pods, we will find nothing:

kubectl-get-pods

This kubernetes replicaset tutorial for beginners arrives at its end.

To check our other tutorials, you can like our how to program facebook page.


Post a Comment

Previous Post Next Post