- A ReplicaSet is a process that runs multiple instances of a Pod and keeps the specified number of Pods constant.
- Its purpose is to maintain the specified number of Pod instances running in a cluster at any given time.
If you are new to the replica set you can read the replica set blog here
Let’s understand the replica set the YAML file.
apiVersion
- This refers to the version of Kubernetes.
- API version consists of two things group name and Version.
- There are several versions, and several objects are introduced with each version.
- Some common ones are v1, apps/v1, and extensions/v1beta1.
- On the right-hand side, you can see the different types of values
- You can check out the below command for the list of versions.
$ kubectl api-versions
kind:
- This is the type of Kubernetes object. In this case (the example above), we’re creating a ReplicaSet.
- You can see the other types of objects that we create commonly on the right side here.
- You can check out the below command for the list of kinds.
$ kubectl api-resources | awk '{print $5}'
metadata:
- The metadata houses information that describes the object briefly.
- The information in the metadata usually contains the name you want to give the object (Replicaset in our case), the labels, and the annotation.
- name the object is the only mandatory field and the remaining are optional.
Spec:
In the spec section, we have three fields — replicas, selector, and template
- label selector to be used to select the pods, a number of replicas of the pod to be created.
- In the above example, we are running 3 replicas of
nginx
container. - The selector is used to create pods using matchlabels defined in the template field.
- The template is a pod template that is used by a selector to create a set of pods.
Now, we can use two types of selectors in replicaset (Equality/Set)
Equality based Selectors (matchLabels)
- Equality based selectors allow filtering by label keys and values.
- Three operators used in set based equality based selectors are
=
,==
,!=
. The first two represent equality (and are simply synonyms), while the latter represents inequality. - For example, if we provide the following selectors:
app=nginx
- Here, we’ll select all resources with key equal to
app
and value equal tonginx-app
.
Set-Based Selectors (matchExpressions)
- Unlike Equality based, Set-based label selectors allow filtering keys according to a set of values.
- Three kinds of operators used in set-based selectors are
in
,notin
,exists
(only the key identifier). - Here, the first selector will select all resources with key equal to
app
and value equal tonginx-app.
- The second example selects all resources with key equal to
tier
and values other thanfrontend.