- 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 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 Controller and Replica Set
Replication Controller is the older technology, that is being replaced by Replica Set. What was discussed previously is applicable to both of those technologies.
- How to create a
Replication Controller
- Create a
Replication Controller definition file.
- 4 sections:
- apiVersion: v1 - Replication Controllers are supported in v1.
- kind: ReplicationController
- metadata: myapp-rc
- labels:
* app: myapp
* type: front-end
- spec:
* template:
- For any Kubernetes Definition File, the
spec: defines what is inside the object.
- The
Replication Controller creates multiple instances of a pod.
- The
template: section is used to create replicas.
- How do we define the
pod template:?
- Can reuse the contents of a
pod definition file in the template section.
- Can move all of the lines, aside from
apiVersion and kind.

- The
Replication Controller is the parent in the file and the Pod Definition being the child.
- We need to mention how many replicas are required in the
Replication Controller.
- Add the property called
replicas: to the file.
template: and replicas: are children of the spec section.

- Once complete, run
kubectl create -f rc-definition.yaml. It then creates the Replication Controller.
- To view the
Replication Controllers, run the kubectl get replicationcontroller

- Will observe the amount of replicas in the output.
- Then to check the pods that were created by the
Replication Controller, run the kubectl get pods command.

- All of the pods will start with the name of the
Replication Controller
- To view the created
Replication Controllers, run kubectl get replicationcontroller

- Now we look at
replicaset.
- Very similar to the
replicationcontroller.
- We have
apiVersion, kind, metadataand spec
apiversion however is set to app/v1
- If you input that incorrectly, you will likely receive an error that looks like this:

kind is ReplicaSet
- Add
name and labels in the metadata section.
spec is the same as ReplicationController
- The main difference is that
ReplicaSet requires a selector definition.
- The
selector part helps the ReplicaSet identify which pods fall under it.
- Why do you need to specify the pods that are underneath it though?
- A
ReplicaSet can also manage pods, which were not part of the ReplicaSet creation.
- For example, pods created before the
ReplicaSet, which matched labels.
- The
ReplicaSet will also take those pods into consideration, when creating the replicas.
- The
selector is one of the major differences between ReplicationController and ReplicaSet.
- The
selector is not a required field for the ReplicationController.
- When skipped, the
ReplicationController assumes it is the same as the labels provided in the pod definition file.
- In the case of
ReplicaSet, a user input is required for this property.
- That is done using
matchLabels
matchLabels specifies everything under it, to the labels on the pods.
- The
ReplicaSet also provides other options for matching labels, that are not available in the ReplicationController.
- To create a
ReplicaSet, run the kubectl create command, providing the ReplicaSet as input.

- To view the created replicas, run the
kubectl get replicaset command.

- Of course for pods, run
kubectl get pods

- Regarding
Labels and Selectors - 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
template section, in case a pod fails in the future.
- 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 replace to update the replica set.
kubectl replace -f replicaset-definition.yml

- A second way to perform the same task, is via the
kubectl scale command 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 / Name format.

- Using the filename as input, will not automatically update all replicas in the file. The
replicaset-definition.yml will still be 3.
- There are options to scale the replicas based on load as well.
kubectl create -f replicaset-definition.yml creates the replicaset.
kubectl get replicaset to show the replicasets created.
kubectl delete replicaset myapp-replicaset will get rid of all of the replicasets. It also deletes all of the underlying pods as well.
kubectl replace -f replicaset-definition.yml to increase the amount of replicas via modifying the file.
kubectl scale -replicas=6 -f replicaset-definition.yml will scale the replicas, without modifying the yml file.