Kubernetes ReplicaSet explained

0
1294
kubernetes replicaset explained
kubernetes replicaset explained

On the two last articles we was talking about the Kubernetes Pod, right now we will discuss another important concept in Kubernetes which is  the “Kubernetes ReplicaSet”. We will understand what is a ReplicaSet, when we should use it and how to create it with an example.

Note: This post assumes that you have a running kubernetes cluster and you have a basic understanding of what a “Pod” is and its purpose, kubeadm and kubectl.

So first..

What is a Kubernetes ReplicaSet

It’s the new version of the Replication Controller and we can use it to make sure that we have a specified number of pod replicas running (A controller in Kubernetes is what takes care of tasks to make sure the desired state of the cluster matches the observed state).

The main difference between a ReplicaSet and a Replication Controller right now is the selector support. First, the ReplicaSet supports the new set-based selector requirements as described in the labels user guide and the Replication Controller only supports equality-based selector requirements.

kubernetes replicaset
kubernetes replicaset

Kubernetes ReplicaSet manifest file

apiVersion: apps/v1 # The API version
kind: ReplicaSet   # The kind that we are creating
Metadata: # All Metadata like name, labels
  name: app-name
  labels:
    app: web-App
    tier: app-Tier
Spec: 
  replicas: 3 # Here we tell kubernetes the number of replicas we want
  Selector: # The label selector field. 
    matchLabels:
      tier: app-Tier
    matchExpressions:
      - {key: tier, operator: In, values: [app-Tier]} # we are using the set-based operators
  template:
    metadata:
      labels:
        app: web-App
        tier: appTier
    Spec: # This spec section should look like spec in a pod definition
      Containers: 

The only difference here comes with the Spec section, it look different from other objects. There are two Spec’s in the manifest example above. The first Spec lets you declare what the replicaset should look like and the second spec is for containers. “.spec.template” is the only required field of the first spec section. If you do not specify replicas, it will deploy just one pod. Also we should mention that the “.spec.template.metadata.labels” must match exactly what you have in the “.spec.selector”(You can see this in our manifest above), or Kubernetes will not allow it to be created.

The APIVersion, kind and Metadata look the same comparing to any other object in Kubernetes.

On those two tables you can find the tags used on the manifest file, their type and description:

Tag Type Description
apiVersion String Versioned schema of represented objects
kind String REST resource that the object represents – ReplicaSet in this example
metadata ObjectMeta Details about metadata of object
name metadata It’s a unique name within the namespace. It defines the name of the replica set.
spec ReplicaSetSpec Spec defines the specification of the desired behavior of the ReplicaSet
selector spec It’s a LabelSelector object.

The selector is a label query over pods that should match the replica count. Label keys and values that must match in order to be controlled by this replica set. It must match the pod template’s labels

app spec:selector:matchLabels It’s a key and his value is the app name
replicas spec Replicas is the number of desired replicas
template spec: containers Object of PodTemplateSpec

Template is the object that describes the pod that will be created if insufficient replicas are detected

This section is nothing but the definition of pod.

metadata spec: template Standard list metadata
labels spec: template: metadata It’s a key value pair
spec spec: template Standard list metadata
containers spec: template: spec List of containers belonging to the pod. It’s the array of containers
name spec: template: spec: containers It’s the unique name of image. Name of the container specified as a DNS_LABEL
image spec: template: spec: containers The image that the container is running

ReplicaSet example

After understanding what a replicaset is, let’s create one:

In this example, we will deploy our firstreplica.yaml file which will create a simple frontend nginx app with 3 replicas.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: webapp-replicas
  labels:
    app: webapp
    tier: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
    matchExpressions:
      - {key: tier, operator: In, values: [frontend]}
  template:
    metadata:
      labels:
        app: webapp
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

First we run kubectl create command to apply our manifest

$ kubectl create -f firstreplica.yaml
replicaset.apps "webapp-replicas" created

Next, we make sure it is created

$ kubectl get replicaset
NAME                            DESIRED   CURRENT   READY     AGE
webapp-replicas                  3         3         3         15s

As we can see, there are 3 deployed and 3 ready. You can just check the pods.

$ kubectl get pod
NAME                            READY     STATUS    RESTARTS   AGE
webapp-replicas-hdr3r            1/1       Running   0          33s
webapp-replicas-7sva2            1/1       Running   0          33s
webapp-replicas-s9rd4            1/1       Running   0          33s

We see all 3 running with 0 restarts which mean our application is not crashing. We can also describe the object which will give us more details about our replicas.

$ kubectl describe replicaset webapp-replicas
Name:         webapp-replicas
Namespace:    default
Selector:     tier=frontend,tier in (frontend)
Labels:       app=webapp
              tier=frontend
Annotations:  
Replicas:     3 current / 3 desired
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=webapp
           tier=frontend
  Containers:
   nginx:
    Image:        nginx
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  
    Mounts:       
  Volumes:        
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  12m   replicaset-controller  Created pod: webapp-replicas-8nfd8
  Normal  SuccessfulCreate  12m   replicaset-controller  Created pod: webapp-replicas-tyrkp
  Normal  SuccessfulCreate  12m   replicaset-controller  Created pod: webapp-replicas-ff6sg

Special cases

What if we want to change the number of the replicas?

This is very simple, we have to just change the replica field to the value we want and k8s will scale it to that number. For example “replicas: 1”. If we make the change and re apply, we will only have one replica running.

Can we remove pod from a Kubernetes ReplicaSet?

Yes we can. It is as simple as removing the label from the pod and it will be removed from the Set.

How to delete a Kubernetes ReplicaSet?

If you want to fully delete a replicaset, all you have to do is running the following command:

kubectl delete replicaset webapp-replicas

the replicasets and the pods will be deleted.

You can refer to the official Kubernetes ReplicaSet documentations if you want to read more about it.

This is the end of our article and where i should note that in general, Kubernetes recommend that we use deployment (a higher-level concept that manages ReplicaSets and provides declarative updates to pods along with a lot of other useful features) controller instead of ReplicaSet and which will be the subject of our next article.

see you!

LEAVE A REPLY

Please enter your comment!
Please enter your name here