Creating ReplicaSets
Learn how to create and retrieve a replicaSet
We'll cover the following
Looking into the definition#
Let’s take a look at a ReplicaSet based on the Pod we created in the previous chapter go-demo-2.yml
.
The
apiVersion
,kind
, andmetadata
fields are mandatory with all Kubernetes objects. ReplicaSet is no exception, i.e., it is also a Kubernetes object.
-
Line 1: We specified that the
apiVersion
isapps/v1
. -
Line 2-3: The
kind
isReplicaSet
andmetadata
has thename
key set togo-demo-2
. We could have extended ReplicaSetmetadata
with labels. However, we skipped that part since they would serve only for informational purposes. They do not affect the behavior of the ReplicaSet.
You should be familiar with the above three fields since we already explored them when we worked with Pods. In addition to them, the spec
section is mandatory as well.
-
Line 5-6: The first field we defined in the
spec
section isreplicas
. It sets the desired number of replicas of the Pod. In this case, the ReplicaSet should ensure that two Pods should run concurrently. If we did not specify the value of thereplicas
, it would default to1
. -
Line 7: The next
spec
section is theselector
. We use it to select which pods should be included in the ReplicaSet. It does not distinguish between the Pods created by a ReplicaSet or some other process. In other words, ReplicaSets and Pods are decoupled. If Pods that match theselector
exist, ReplicaSet will do nothing. If they don’t, it will create as many Pods to match the value of thereplicas
field. Not only that ReplicaSet creates the Pods that are missing, but it also monitors the cluster and ensures that the desired number ofreplicas
is (almost) always running. In case there are already more running Pods with the matchingselector
, some will be terminated to match the number set inreplicas
. -
Line 8-10: We used
spec.selector.matchLabels
to specify a few labels. They must match the labels defined in thespec.template
. In our case, ReplicaSet will look for Pods withtype
set tobackend
andservice
set togo-demo-2
. If Pods with those labels do not already exist, it’ll create them using thespec.template
section. -
Line 11-17: The last section of the
spec
field is thetemplate
. It is the only required field in thespec
, and it has the same schema as a Pod specification. At a minimum, the labels of thespec.template.metadata.labels
section must match those specified in thespec.selector.matchLabels
. We can set additional labels that will serve informational purposes only. ReplicaSet will make sure that the number of replicas matches the number of Pods with the same labels. In our case, we settype
andservice
to the same values and added two additional ones (db
andlanguage
). It might sound confusing that thespec.template.spec.containers
field is mandatory. ReplicaSet will look for Pods with the matching labels created by other means. If we already created a Pod with labelstype: backend
andservice: go-demo-2
, this ReplicaSet would find them and would not create a Pod defined inspec.template
. The main purpose of that field is to ensure that the desired number ofreplicas
is running. If they are created by other means, ReplicaSet will do nothing. Otherwise, it’ll create them using the information inspec.template
. -
Line 18-23: Finally, the
spec.template.spec
section contains the samecontainers
definition we used in the previous chapter. It defines a Pod with two containers (db
andapi
).
In the previous chapter, we claimed that those two containers should not belong to the same Pod. The same is true for the containers in Pods managed by the ReplicaSet. However, we did not yet have the opportunity to explore ways to allow containers running in different Pods to communicate with each other. So, for now, we’ll continue using the same flawed Pods definition.
Creating the replicaSet#
Let’s create the ReplicaSet and experience its advantages first hand.
We got the response that the replicaset "go-demo-2"
was created
. We can confirm that by listing all the ReplicaSets in the cluster.
The output is as follows.
We can see that the desired number of replicas is 2
and that it matches the current value. The value of the ready
field is still 0
but, after the images are pulled, and the containers are running, it’ll change to 2
.
Instead of retrieving all the replicas in the cluster, we can retrieve those specified in the go-demo-2.yml
file.
The output should be the same since, in both cases, there is only one ReplicaSet running inside the cluster.
All the other kubectl get
arguments we explored in the previous chapter also apply to ReplicaSets or, to be more precise, to all Kubernetes objects. The same is true for kubectl describe
command.
The last lines of the output are as follows.
Judging by the events, we can see that ReplicaSet created two Pods while trying to match the desired state with the actual state.
Finally, if you are not yet convinced that the ReplicaSet created the missing Pods, we can list all those running in the cluster and confirm it.
To be on the safe side, we used the --show-labels
argument so that we can verify that the Pods in the cluster match those created by the ReplicaSet.
The output is as follows.
Try it yourself#
The above illustration shows a ReplicaSet with two replicas of a Pod.
For your ease, all the instructions in the lessons have been given below:
You can practice the commands in the following code playground by pressing the Run button and waiting for the cluster to set up:
/
- go-demo-2.yml