Cyberithub

How to Connect ReplicaSet to a Pod in Kubernetes Cluster

Advertisements

In this article, we will look into how to connect Replicaset to a Pod in a Kubernetes Cluster but before that it is important to understand the purpose of Replicaset in Kubernetes. According to Kubernetes official documentation, A ReplicaSet's purpose is to maintain a stable set of replica Pods running at any given time. As such, it is often used to guarantee the availability of a specified number of identical Pods.

What is ReplicaSet

Replicaset are Kubernetes Controllers which are responsible for maintaining a stable set of replica pods running at any given time.

Advertisements

What is Pod

A pod is a set of one or more containers, with shared storage and network resources. It is smallest deployable units in Kubernetes.

What is Label

Label are basically the objects with key-value pairs which are attached to pods, replication controllers and services.

Advertisements

What is Selector

Selector is also known as Label Selector. It is one of the excellent feature in Kubernetes to select resources based on the value of labels and resource fields assign to a group of pods or nodes.

How to Connect ReplicaSet to a Pod in Kubernetes Cluster

How to Connect ReplicaSet to a Pod in Kubernetes Cluster

To begin with, we have a list of pods running with different labels as shown below. You can get the pod status along with its label information using kubectl get pods --show-labels command.

Advertisements
root@cyberithub:~# kubectl get pods --show-labels
NAME            READY STATUS  RESTARTS AGE    LABELS
testapp-1-dnrzq 1/1   Running   0      56s    dept=finance,env=dev,tier=frontend
testapp-2-96p4g 1/1   Running   0      56s    env=prod,tier=frontend
testapp-1-8lsvp 1/1   Running   0      56s    dept=finance,env=dev,tier=frontend
testapp-1-zzxdf 1/1   Running   0      55s    dept=finance,env=prod,tier=frontend
testapp-1-f2rdw 1/1   Running   0      56s    dept=finance,env=dev,tier=frontend
testdb-1-4xc2n  1/1   Running   0      55s    env=dev,tier=db
testdb-1-b5gmw  1/1   Running   0      55s    env=dev,tier=db
testauth        1/1   Running   0      55s    dept=finance,env=prod
testdb-2-9s75x  1/1   Running   0      55s    dept=finance,env=prod,tier=db
testdb-1-7l8b9  1/1   Running   0      55s    env=dev,tier=db
testdb-1-766kd  1/1   Running   0      55s    env=dev,tier=db

You can also check all the pods based on some specific label by using -l option. For example, if you want to check all the pods running with label env=dev then you need to use kubectl get pods -l env=dev command as shown below.

root@cyberithub:~# kubectl get pods -l env=dev
NAME            READY STATUS  RESTARTS AGE
testapp-1-dnrzq 1/1   Running    0     4m20s
testapp-1-8lsvp 1/1   Running    0     4m20s
testapp-1-f2rdw 1/1   Running    0     4m20s
testdb-1-4xc2n  1/1   Running    0     4m19s
testdb-1-b5gmw  1/1   Running    0     4m19s
testdb-1-7l8b9  1/1   Running    0     4m19s
testdb-1-766kd  1/1   Running    0     4m19s

We have an example ReplicaSet definition here which we will use to connect ReplicaSet to a pod. Below given ReplicaSet creates 2 replica pods with the name replicaset-1 when it matches the Label selector tier:frontend and the underlying containers are created using nginx image. However this ReplicaSet definition is having a problem which we will understand when we will try to create it.

Advertisements
root@cyberithub:~# vi replicaset-example.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
    name: replicaset-1
spec:
    replicas: 2
    selector:
       matchLabels:
          tier: frontend
    template:
       metadata:
         labels:
          tier: nginx
      spec:
         containers:
         - name: nginx
            image: nginx

So now if you try to create above ReplicaSet, it will not get created as you can see from below output. It is simply because the selector does not match the labels given in the above definition.

root@cyberithub:~# kubectl create -f replicaset-example.yaml
The ReplicaSet "replicaset-1" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"tier":"nginx"}: `selector` does not match template `labels`

So if we change this label to tier:nginx then it will work because this label matches with the given template label.

root@cyberithub:~# vi replicaset-example.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
   name: replicaset-1
spec:
   replicas: 2
   selector:
     matchLabels:
        tier: nginx
   template:
     metadata:
       labels:
        tier: nginx
     spec:
        containers:
        - name: nginx
          image: nginx

Now if you try to create again, then it should be created like below.

root@cyberithub:~# kubectl create -f replicaset-example.yaml
replicaset.apps/replicaset-1 created

You can verify the ReplicaSet using kubectl get replicaset command as shown below.

root@cyberithub:~# kubectl get replicaset
NAME         DESIRED CURRENT READY AGE
testapp-2    1        1       1    23m
testapp-1    3        3       3    23m
testdb-2     1        1       1    23m
testdb-1     4        4       4    23m
replicaset-1 2        2       2    14m

Now the replicaset is created, you can verify the status of pods by using kubectl get pods command. Here you can see that 2 replicaset-1 pods are created and running fine.

root@cyberithub:~# kubectl get pods
NAME               READY STATUS  RESTARTS AGE
testapp-1-dnrzq    1/1   Running    0     26m
testapp-2-96p4g    1/1   Running    0     26m
testapp-1-8lsvp    1/1   Running    0     26m
testapp-1-zzxdf    1/1   Running    0     26m
testapp-1-f2rdw    1/1   Running    0     26m
testdb-1-4xc2n     1/1   Running    0     26m
testdb-1-b5gmw     1/1   Running    0     26m
auth               1/1   Running    0     26m
testdb-2-9s75x     1/1   Running    0     26m
testdb-1-7l8b9     1/1   Running    0     26m
testdb-1-766kd     1/1   Running    0     26m
replicaset-1-nhvlq 1/1   Running    0     16m
replicaset-1-mrj7v 1/1   Running    0     16m

You can also verify the pods running with the label tier=nginx using kubectl get pods -l tier=nginx command.

root@cyberithub:~# kubectl get pods -l tier=nginx
NAME               READY STATUS   RESTARTS AGE
replicaset-1-nhvlq 1/1   Running     0     16m
replicaset-1-mrj7v 1/1   Running     0     16m

Leave a Comment