Skip to main content
In this guide, you’ll learn how to define, deploy, self-heal, and scale a Kubernetes ReplicaSet. A ReplicaSet ensures a specified number of Pod replicas are running at all times, providing high availability and fault tolerance.

Directory Structure

Assuming your project root is named Kubernetes-for-Beginners, the layout might look like this:

1. Defining the ReplicaSet

Create the file replicasets/replicaset.yaml:
The selector.matchLabels field must exactly match the labels under template.metadata.labels. The labels in metadata.labels on the ReplicaSet itself are not used for Pod selection.
For reference, here’s the original Pod definition in pods/nginx.yaml:

2. Deploying the ReplicaSet

From the project root, apply the ReplicaSet manifest:
Verify the ReplicaSet and its Pods:

3. ReplicaSet Self-Healing

To observe self-healing, delete one of the Pods:
After a few seconds, the ReplicaSet controller will recreate a replacement Pod:
You can inspect events to confirm:
Check the Events section for Pod creation and deletion entries.

4. Preventing Extra Pods

If you manually create a Pod matching the ReplicaSet’s selector, the controller will delete it to maintain the desired count.
The ReplicaSet controller enforces the desired replica count by deleting any excess Pods that match its selector.

5. Scaling the ReplicaSet

5.1 Using kubectl edit

Locate the spec.replicas field and adjust it:
Save and exit. Verify new Pod creation:

5.2 Using kubectl scale

Excess Pods beyond the new replica count are gracefully terminated.

Watch Video