In this lesson, you’ll learn how to create and manage a ReplicaSet using a pre-existing Pod definition file. Previously, you created a Pod using YAML; now, we’ll extend that knowledge by grouping Pods into a ReplicaSet to ensure that a consistent number of identical Pods is running at all times.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 the ReplicaSet YAML
Start by navigating to your project directory. You should already have a directory namedpods containing your Pod definition files. Next, create a new directory for ReplicaSets and add a file called replicaset.yaml inside it.
Open replicaset.yaml and begin by specifying the API version and kind. For ReplicaSets, use apps/v1 as the API version and ReplicaSet as the kind. Then add metadata including the ReplicaSet’s name and labels. In this example, we use the label app: myapp.
Under the spec section:
- Define the
selectorthat matches the labels on the Pods managed by this ReplicaSet. - Set the number of replicas (e.g., 3).
- Provide the Pod template. You can copy the template from your existing Pod definition, but ensure that the indentation is correct after pasting.
Ensure that the labels under the
selector and in the Pod template exactly match, as these are the only labels that affect the ReplicaSet’s operation. The label assigned to the ReplicaSet itself in the metadata is not used for matching.Validating the ReplicaSet Configuration
After saving the file, verify that your directory structure is correct. Your project root should now contain a new directory (for example,replicatesets) with the replicaset.yaml file. For instance:
myapp-replicaset, indicating its association with the ReplicaSet.
Testing the ReplicaSet Self-Healing
ReplicaSets continuously ensure that the defined number of Pods is running. To test this self-healing mechanism:-
List Your Pods:
Identify a Pod to delete (e.g., one ending with8nxxl):Sample output: -
Delete the Selected Pod:
You should see confirmation similar to:
Preventing Extra Pods from Running
A core feature of ReplicaSets is to ensure that only the specified number of Pods are active. If you attempt to create an additional Pod with a label matching the ReplicaSet selector, the ReplicaSet controller will automatically delete the extra Pod. For instance, update your existingnginx.yaml file so that the Pod uses the same label as defined in the ReplicaSet selector:
Updating the ReplicaSet
There are scenarios where you may need to adjust the number of replicas in your ReplicaSet. There are two methods to achieve this:Method 1: Edit the Running Configuration
Usekubectl edit to modify the live configuration of the ReplicaSet. This command opens the configuration in your default text editor (such as Vim):
spec.replicas field (for example, change it from 3 to 4), then save and exit the editor. Kubernetes will immediately update the ReplicaSet and create new Pods as necessary. Verify the update by listing your Pods: