Learn to create and manage ReplicaSets using a YAML file based on a pre-existing Pod definition in Kubernetes.
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”.
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.
To verify that the ReplicaSet maintains the desired number of pods, delete one of them. For example:
Copy
Ask AI
kubectl delete pod myapp-replicaset-8nxxl
After a few seconds, list the pods again:
Copy
Ask AI
kubectl get pods
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:
Copy
Ask AI
kubectl describe replicaset myapp-replicaset
This command displays the ReplicaSet’s configuration, selectors, pod template details, and a history of events including the creation of replacement pods.
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”:
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:
To update your ReplicaSet—for instance, changing the number of replicas from 3 to 4—run:
Copy
Ask AI
kubectl edit replicaset myapp-replicaset
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:
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:
Once the terminating pods are removed, only two pods should remain. Confirm the final state with:
Copy
Ask AI
kubectl get pods
Expected final output:
Copy
Ask AI
NAME READY STATUS RESTARTS AGEmyapp-replicaset-jlgr2 1/1 Running 0 6m50smyapp-replicaset-pm4rl 1/1 Running 0 6m50s
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.