In this lesson, we explore the concept of replicas in Kubernetes and the importance of replication controllers for ensuring high availability. Imagine a scenario where your application runs only one pod. If that pod crashes, users lose access to your application. To avoid downtime, it’s critical to run multiple instances (or pods) simultaneously. A replication controller guarantees that the desired number of pods are always running in your cluster, delivering both high availability and load balancing. Even if you intend to run a single pod, the replication controller automatically initiates a new pod if the existing one fails. Whether you need one pod or a hundred, the replication controller maintains that number, distributing load across multiple instances. For example, if your user base grows, additional pods can be deployed. In cases where one node runs out of resources, new pods can automatically be scheduled on other nodes.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.

Creating a ReplicationController
To create a ReplicationController, start by defining a configuration file namedrc-definition.yaml. Like any Kubernetes definition file, it includes the following sections: API version, kind, metadata, and spec.
- API Version: For a ReplicationController, use
v1. - Kind: Set it as
ReplicationController. - Metadata: Provide a unique name (for example,
myapp-rc) along with labels that categorize your application (such asappandtype). - Spec: Define the desired state of the object:
- Specify the number of replicas.
- Include a
templatesection for the pod definition. (Note: do not include the API version and kind from the original pod file; include only the pod’s metadata, labels, and spec, indented as a child of thetemplate.)
myapp-rc-xxxx), indicating their automatic creation.
Introducing ReplicaSets
A ReplicaSet functions similarly to a ReplicationController but comes with key differences:-
API Version and Kind:
- For a ReplicaSet, set the API version to
apps/v1(instead ofv1). - The kind should be
ReplicaSet.
- For a ReplicaSet, set the API version to
-
Selector Requirement:
- A ReplicaSet demands an explicit selector in its configuration. Typically defined under
matchLabels, the selector identifies which pods the ReplicaSet will manage. This feature also enables the ReplicaSet to adopt existing pods that match the specified labels, even if they were not created by it.
- A ReplicaSet demands an explicit selector in its configuration. Typically defined under
replicaset-definition.yml:
Labels, Selectors, and Their Role
Labels are vital for organizing and selecting subsets of objects in Kubernetes. When deploying multiple instances (for instance, three pods for a front-end application), a ReplicaSet uses labels and selectors to manage these pods effectively. Consider the following sample configuration that highlights the relationship between pod labels and the ReplicaSet selector:tier: front-end. This concept of labels and selectors is widely used throughout Kubernetes to maintain order and efficiency.
Scaling ReplicaSets
Scaling ReplicaSets allows your application to adapt to changing demand. Suppose you started with three replicas and later need to scale up to six. There are multiple approaches:-
Edit the Definition File:
Update thereplicasfield in your ReplicaSet definition file to six, then run: -
Use the Scale Command:
Alternatively, use thekubectl scalecommand:Or, if you prefer using the ReplicaSet name:
Remember, if you use the scale command, the changes are updated only in the cluster state. The original definition file will continue to show the previous replica count until it is modified.
Summary of Commands
Below is a quick reference for essential kubectl commands used with ReplicaSets:| Command | Description | Example |
|---|---|---|
| Create a ReplicaSet | Launch a new ReplicaSet from a definition file | kubectl create -f replicaset-definition.yml |
| List ReplicaSets | View all ReplicaSets in your cluster | kubectl get replicaset |
| List Pods | Display all pods including those managed by ReplicaSets | kubectl get pods |
| Delete a ReplicaSet | Remove a specific ReplicaSet | kubectl delete replicaset myapp-replicaset |
| Update a ReplicaSet | Replace an existing ReplicaSet with a new configuration | kubectl replace -f replicaset-definition.yml |
| Scale a ReplicaSet | Adjust the number of replicas using a file | kubectl scale --replicas=6 -f replicaset-definition.yml |

