How to create a Replicaset in Kubernetes

A replica set makes sure the specified replicas of pods are always running. It can be considered as a replacement for the replication controller. The main difference between the two is that ReplicaSets allow us to use something called “Label Selector”. Replicaset is one of the Kubernetes controllers. It is used to make sure that we have a specified number of pod replicas that are always up and running. 

The template of the replicaset looks as follows.

apiVersion: apps/v1 
kind: ReplicaSet   
Metadata: 
  name: some-name
  labels:
    app: some-App
    tier: some-Tier
Spec: 
  replicas: 3 # Here we tell k8s how many replicas we want
  Selector: # A label selector field. 
    matchLabels:
      tier: some-Tier
    matchExpressions:
      - {key: tier, operator: In, values: [some-Tier]} #set-based operators
  template:
    metadata:
      labels:
        app: some-App
        tier: someTier
    Spec: 
      Containers:

In this article, we will see how to create a replica set and how it identifies pods with labels.

Pre-requisites

Kubernetes Cluster with at least 1 worker node.
If you want to learn to create a Kubernetes Cluster, click here. This guide will help you create a Kubernetes cluster with 1 Master and 2 Nodes on AWS Ubuntu 18.04 EC2 Instances. 

What we will do

  • Create a Replicaset

Create a Replicaset

Create a new file and add the following replica set definition into it.

vim my-replica-set.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-proxy
  labels:
    app: nginx-proxy
    tier: frontend
spec:
  # modify replicas according to your case
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx

my-replicaset

The above object definition will create three replicas of the pod.

First, check the existing replica set using the following command.

kubectl get replicaset

To create a replica said execute the following command.

kubectl create -f my-replica-set.yml
kubectl get replicaset
kubectl get pods

create-replicaset

In the above screenshot, you can see the details of the ponds created after creating a replica set.

Now create an individual pod with the same labels we previously specified in the definition of the replica set.

vim my-test-pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: pod1
  labels:
    tier: frontend
spec:
  containers:
  - name: nginx
    image: nginx

my-test-pod

Now if we create this, It will not get created.

Execute the following commands to create the previous pods and create a new pod.

kubectl get pods
kubectl create -f my-test-pod.yml
kubectl get pods

create-pod-matching-label

In the above screenshot, you can see that even after creating a new pod it is in terminating state. The reason for this is we already have a replica set with replicas as 3 And we are creating a new pod separately with the same labels. Since the labels and selectors match the replica set, it deletes the pod to maintain the specified replica count we specified in the replica set definition.

You can get the details of the replica set using the following commands.

kubectl get replicaset
kubectl describe replicaset nginx-proxy

describe-replicaset

In the above screenshot, you can see that a replica set deletes the pod to maintain the number of replicas.

Now let's create a pod with no label using the following pod definition.

vim my-test-pod-no-label.yml
apiVersion: v1
kind: Pod
metadata:
  name: pod1
spec:
  containers:
  - name: nginx
    image: nginx

my-test-pod-without-label

To create a new pod without any label execute the following command.

kubectl get pods
kubectl create -f my-test-pod-no-label.yml
kubectl get pods

create-pod-with-no-label

In the above screenshot, you can see that a new pod has been created and the replica set did not delete the new pod this time.

The reason for this is the new port does not have any label hence a replica set does not match with it and does not consider it to be a part of replicas set.

Now let's try to create a port that belongs to the replica set.

Execute the following commands to delete a pod.

kubectl get pods
kubectl delete pod nginx-proxy-6gc46
kubectl get pods

delete-pod-from-replicaset

In the above screenshot, you can see that even after deleting a pod that belongs to the replica set a new pod got created. A new pod got created to maintain the number of replicas that we specified in the replica set definition.

Now, when you no longer need your replica set it can be deleted using the following commands.

kubectl get replicaset
kubectl delete replicaset nginx-proxy
kubectl get replicaset
kubectl get pods

delete-replicaset

Since the pod we created separately is not a part of the replica set we need to delete it separately.

Use the commands. To delete the pod which does not belong to the replica set.

kubectl get pods
kubectl delete pod pod1
kubectl get pods

delete-pod-with-no-label

Conclusion

In this article, we saw how to create a simple replica set. We also saw how a pod becomes part of a replica set if the label matches to the selector available in the replica set. 

Share this page:

0 Comment(s)