Replicasets
- Controllers are the brain behind Kubernetes.
- Replication Controller is the focus here.
- What is a replica and why do we need a replication controller?
- What if an application crashes and the pod fails?
- The users are then no longer able to access the application.
- We need more than one instance of a pod running at the same time.
- The Replication Controller helps us run multiple instances of a pod in a Kubernetes cluster. This provides High Availability.

- A Replication Controller is useful even for a single pod, as it can bring up a new pod if the existing one fails.

- The Replication Controller ensures the specified number of pods are running at all times.
- Another use of the Replication Controller is Load Balancing and Scaling.
- If a number of issues increases, we can increase the pod count to balance the load.

- If a number of issues increases, we can increase the pod count to balance the load.
- If demand continues to increase and the resources of the first node are exhausted, can then deploy to another node in the cluster.

- There are two similar terms:
Replication ControllerandReplica Set Replication Controlleris the older technology, that is being replaced byReplica Set. What was discussed previously is applicable to both of those technologies.- How to create a
Replication Controller- Create a
Replication Controllerdefinition file. - 4 sections:
- apiVersion: v1 - Replication Controllers are supported in v1.
- kind: ReplicationController
- metadata: myapp-rc
- labels: * app: myapp * type: front-end
- spec: * template:
- Create a
- For any Kubernetes Definition File, the
spec:defines what is inside the object. - The
Replication Controllercreates multiple instances of a pod. - The
template:section is used to create replicas. - How do we define the
podtemplate:?- Can reuse the contents of a
poddefinition file in thetemplatesection. - Can move all of the lines, aside from
apiVersionandkind.

- Can reuse the contents of a
- The
Replication Controlleris the parent in the file and thePod Definitionbeing the child. - We need to mention how many replicas are required in the
Replication Controller. - Add the property called
replicas:to the file. template:andreplicas:are children of thespecsection.
- Once complete, run
kubectl create -f rc-definition.yaml. It then creates theReplication Controller. - To view the
Replication Controllers, run thekubectl get replicationcontroller
- Will observe the amount of replicas in the output.
- Then to check the pods that were created by the
Replication Controller, run thekubectl get podscommand.
- All of the pods will start with the name of the
Replication Controller - To view the created
Replication Controllers, runkubectl get replicationcontroller
- Now we look at
replicaset. - Very similar to the
replicationcontroller. - We have
apiVersion,kind,metadataandspec apiversionhowever is set toapp/v1- If you input that incorrectly, you will likely receive an error that looks like this:

kindisReplicaSet- Add
nameandlabelsin themetadatasection. specis the same asReplicationController- The main difference is that
ReplicaSetrequires aselectordefinition. - The
selectorpart helps theReplicaSetidentify which pods fall under it. - Why do you need to specify the pods that are underneath it though?
- A
ReplicaSetcan also manage pods, which were not part of theReplicaSetcreation.- For example, pods created before the
ReplicaSet, which matched labels.- The
ReplicaSetwill also take those pods into consideration, when creating the replicas.
- The
- For example, pods created before the
- A
- The
selectoris one of the major differences betweenReplicationControllerandReplicaSet. - The
selectoris not a required field for theReplicationController.- When skipped, the
ReplicationControllerassumes it is the same as the labels provided in thepoddefinition file.
- When skipped, the
- In the case of
ReplicaSet, a user input is required for this property.- That is done using
matchLabels matchLabelsspecifies everything under it, to the labels on the pods.
- That is done using
- The
ReplicaSetalso provides other options for matching labels, that are not available in theReplicationController. - To create a
ReplicaSet, run thekubectl createcommand, providing theReplicaSetas input.
- To view the created replicas, run the
kubectl get replicasetcommand.
- Of course for pods, run
kubectl get pods
- Regarding
LabelsandSelectors- why do we label objectes in Kubernetes? - For example, the front-end web app is deployed as 3 pods.
- If they are not created, the replicaset will create them for you.
- The role of the replicaset is that if any of the pods fail, it creates new ones.
- The replicaset is a process that monitors the pods.
- How does the replicaset know which pods to monitor.
- Labelling the pods during creation is necessary.
- The labels provided are a filter for the replicaset.

- The same concept of labels and connectors is used throughout Kubernetes.
- In the
replicaset-definition.yml, there are 3 sections there.
- In the above example, need to create a Replicaset to ensure the 3 pods are up at all times.
- When a new ReplicaSet instance is created, it is not going to automatically create a new POD pod, because there are already 3 available.
- In that case, we always need a
templatesection, in case a pod fails in the future.
- In that case, we always need a
- How to Scale a ReplicaSet - going from 3 –> 6 as an example.

- To do this, update the number of replicas in the definition file to
6
- Then use
kubectl replaceto update the replica set.kubectl replace -f replicaset-definition.yml
- A second way to perform the same task, is via the
kubectl scalecommand and provide the number of replicas to scale to.kubectl scale --replicas=6 -f replicaset-definition.yml - Can either put the definition file or the replicaset name in the
Type/Nameformat.
- Using the filename as input, will not automatically update all replicas in the file. The
replicaset-definition.ymlwill still be 3. - There are options to scale the replicas based on load as well.
kubectl create -f replicaset-definition.ymlcreates the replicaset.kubectl get replicasetto show the replicasets created.kubectl delete replicaset myapp-replicasetwill get rid of all of the replicasets. It also deletes all of the underlying pods as well.kubectl replace -f replicaset-definition.ymlto increase the amount of replicas via modifying the file.kubectl scale -replicas=6 -f replicaset-definition.ymlwill scale the replicas, without modifying the yml file.