Skip to main content
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.

Creating the ReplicaSet YAML

Start by navigating to your project directory. You should already have a directory named pods 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 selector that 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.
Below is an example ReplicaSet definition:

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:
Check the file by listing its contents:
Clear your screen and create the ReplicaSet using:
Soon after, you can verify the ReplicaSet status with:
Expected output:
This output confirms that the ReplicaSet has successfully created three Pods. To inspect the created Pods further, run:
You might see output similar to:
Note how each Pod’s name begins with 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:
  1. List Your Pods:
    Identify a Pod to delete (e.g., one ending with 8nxxl):
    Sample output:
  2. Delete the Selected Pod:
    You should see confirmation similar to:
After a few seconds, list the Pods again. The ReplicaSet will have automatically created a new Pod to maintain the desired replica count. To inspect the ReplicaSet details, including its events, run:
Scroll through the details to verify that a new Pod was created after deletion.

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 existing nginx.yaml file so that the Pod uses the same label as defined in the ReplicaSet selector:
Before applying changes, verify the current Pods:
Then, create the Pod:
Shortly after, run:
You’ll notice that the extra Pod is immediately marked for termination. The ReplicaSet controller consistently enforces that only the predefined number of Pods remain active. To see related events, describe the ReplicaSet:

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

Use kubectl edit to modify the live configuration of the ReplicaSet. This command opens the configuration in your default text editor (such as Vim):
Modify the 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:

Method 2: Utilize the Scale Command

Alternatively, you can scale the ReplicaSet directly:
This command scales the ReplicaSet down to two Pods. Verify the change with:
You may see an output similar to:
After termination completes, only two Pods will remain active.

Conclusion

In this lesson, you learned how to create a ReplicaSet from an existing Pod definition file, observed the self-healing behavior when Pods are deleted, and explored two methods to update the number of replicas—editing the running configuration or scaling directly. These features ensure that your cluster consistently maintains the desired number of active Pods. Happy clustering, and see you in the next lesson!

Watch Video