Skip to main content
In this lesson, you will learn how to create and manage ReplicaSets using a YAML file based on a pre-existing Pod definition. Previously, we created Pods using YAML inside the “Pods” directory. Now, we will create a new directory called “ReplicaSets,” where you will define your ReplicaSet in a file named “replica-set.yaml”.

Creating the ReplicaSet YAML File

Start by creating the “ReplicaSets” directory and then create a file named “replica-set.yaml” inside it. Begin your file by specifying the appropriate API version for ReplicaSets, which is apps/v1, followed by setting the kind as ReplicaSet. Below is a sample YAML snippet for the ReplicaSet. The metadata includes the ReplicaSet’s name (myapp-replicaset) and a label (app: myapp). Within the spec section, Visual Studio Code’s YAML extension may automatically add a selector field. Here, we are using the matchLabels option so that it matches the label in our previously created Pods. For instance, if your Pod’s definition uses a label such as env: production, include the same label in the ReplicaSet selector. After defining the selector, specify the number of desired replicas (three in this example) and include the Pod template, which is copied from the original Pod YAML file. When pasting, ensure the indentation is correct; in Visual Studio Code, you can select the pasted block (except the first line) and press Tab twice to adjust the indentation. Below is the sample YAML for our ReplicaSet, followed by the original Pod definition for reference:
The label defined in the ReplicaSet’s metadata (e.g., app: myapp) is not used for matching. Ensure that the label in the Pod template and the selector (here, env: production) match exactly to allow the ReplicaSet to manage the pods properly.

Deploying the ReplicaSet

  1. Save the “replica-set.yaml” file.
  2. Open your terminal, navigate to the project root where the “ReplicaSets” directory is located, and verify the file exists:
  3. Use cat to inspect the file content and confirm its correctness:
  4. Create the ReplicaSet with the following command:
  5. Check the status of the ReplicaSet:
    Expected output:
  6. List the pods to further validate:
    Expected output:
Each pod name begins with the ReplicaSet’s name (myapp-replicaset), making it easy to identify managed pods.

Pod Deletion and Automatic Replacement

To verify that the ReplicaSet maintains the desired number of pods, delete one of them. For example:
After a few seconds, list the pods again:
You will observe that although one pod is deleted, the ReplicaSet automatically creates a new one to maintain three replicas. For more details about this process, run:
This command displays the ReplicaSet’s configuration, selectors, pod template details, and a history of events including the creation of replacement pods.

Creating a Pod with Matching Labels

Now, consider what happens if you manually create a Pod that uses a label matching the ReplicaSet’s selector. Use the following Pod definition saved as “nginx.yaml”:
If you create this pod using:
the new Pod might initially show a “terminating” status. This is because the ReplicaSet controller, which is responsible for maintaining the desired replica count, will delete any pod that does not fit its management criteria. You can confirm this behavior by inspecting the ReplicaSet events with:

Updating a ReplicaSet

To update your ReplicaSet—for instance, changing the number of replicas from 3 to 4—run:
This command opens the ReplicaSet’s configuration in your default text editor. Note that the displayed file is temporary and includes fields managed by Kubernetes. Locate the spec section and update the replica count:
Save and exit the editor. Kubernetes will apply the changes immediately by creating new pods to meet the updated replica count. Verify the changes with:

Scaling with kubectl scale

Alternatively, you can scale the ReplicaSet without editing the configuration file by using the kubectl scale command. To scale down the ReplicaSet to two replicas, run:
After a short period, check the pods to confirm scaling:
You might see an output like:
Once the terminating pods are removed, only two pods should remain. Confirm the final state with:
Expected final output:
This lesson demonstrated how to create, manage, update, and scale a ReplicaSet in Kubernetes. Using ReplicaSets ensures that the desired number of pod replicas are consistently maintained, promoting high availability in your deployments.
Happy clustering! For further reading, check out the Kubernetes Documentation.

Watch Video

Practice Lab