Learn to create and manage a ReplicaSet using a Pod definition file to maintain a consistent number of identical Pods.
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.
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.
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:
Copy
Ask AI
admin@ubuntu-server kubernetes-for-beginners # lspods replicatesadmin@ubuntu-server kubernetes-for-beginners # cd replicatesets/
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 with 8nxxl):
Copy
Ask AI
kubectl get pods
Sample output:
Copy
Ask AI
NAME READY STATUS RESTARTS AGEmyapp-replicaset-8nxxl 1/1 Running 0 45smyapp-replicaset-jlgr2 1/1 Running 0 45smyapp-replicaset-pm4rl 1/1 Running 0 45s
Delete the Selected Pod:
Copy
Ask AI
kubectl delete pod myapp-replicaset-8nxxl
You should see confirmation similar to:
Copy
Ask AI
pod "myapp-replicaset-8nxxl" deleted
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:
Copy
Ask AI
kubectl describe replicaset myapp-replicaset
Scroll through the details to verify that a new Pod was created after deletion.
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:
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:
Use kubectl edit to modify the live configuration of the ReplicaSet. This command opens the configuration in your default text editor (such as Vim):
Copy
Ask AI
kubectl edit replicaset myapp-replicaset
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:
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!