Kubernetes Replica set Example-Replica set Tutorial

Kubernetes Replica set Example-Replica set Tutorial

What is Kubernetes Replicaset

Replica Sets is a kubernetes object that ensures a certain number of pods are always running. If a pod  crashes the Replication Controller recreate the pods. so using replicaset wec can make sure that that number of pods always exists. Replication Controllers also provide feature like scaleup or scale down the number of pods, and update or delete multiple pods. A pod could die for a different kinds of reasons such as a node failed, ran out of resources, or because any other reason pod will die. But when we are working in production environment we need 100% availability of pods all the time. To ensure this we use replicates. Replicates ensures a certain number of pods are always running, by recreating the new pods in the place of older terminated pods.

How to Create Replicaset

we can create the replicase with yml manifest file

cat myrs.yml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  # modify replicas according to your case
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google_samples/gb-frontend:v3

Create replicaset with kubectl apply command.

master $ kubectl apply -f myrs.yml

replicaset.apps/frontend created

Here the replicaset name is frontend. And this replicaset will take care of the pods which has labels with 'tier: frontend'. In above yml file you can see selector section, using this selector section replicaset will identify the pods. In selector we mentioned matchlabels as tier: frontend. In the template section we defined pod definition like pods labels and what is image the pod should use and the container name. Basically in the the template section we will define the pod.

List the kubernetes replicasets

we can list the pods with kubectl get rs or kubectl get replicaset commands.

master $ kubectl get rs

NAME       DESIRED   CURRENT   READY   AGE
frontend   3         3         3       6m25s

master $ kubectl get replicaset

NAME       DESIRED   CURRENT   READY   AGE
frontend   3         3         3       6m46s

list the pods

list the pods with kubectl command

master $ kubectl get pods
NAME             READY   STATUS    RESTARTS   AGE
frontend-k66rj   1/1     Running   0          13s
frontend-lrjjm   1/1     Running   0          13s
frontend-m8gbg   1/1     Running   0          13s

here you can see 3 pods are running if you deleted the pod. again the pod will be created

master $ kubectl delete pods frontend-k66rj

pod "frontend-k66rj" deleted

master $ kubectl get pods

NAME             READY   STATUS    RESTARTS   AGE
frontend-2mht4   1/1     Running   0          9s
frontend-lrjjm   1/1     Running   0          3m30s
frontend-m8gbg   1/1     Running   0          3m30s

Here you can see age of 'frontend-2mht4' is 9 seconds. So the new pod is created automatically by replicaset.

  • kubernetes replica set example
  • kubernetes replica set history
  • kubernetes replica set logs
  • kubernetes replica set name
  • kubernetes replica set scale

Leave a Reply

Your email address will not be published. Required fields are marked *