Skip to main content
In this lab, we explore ReplicaSets in Kubernetes. Follow along as we review lab steps, execute commands, and troubleshoot issues. This guide covers checking current resources, examining ReplicaSet configurations, troubleshooting image pull errors, scaling, and modifying ReplicaSets using definition files.

1. Verify Existing Pods

Begin by checking the current pods in your cluster. Run the following command:
The result is expected to be:
This indicates that there are no pods currently running.

2. Review Existing ReplicaSets

Next, inspect if any ReplicaSets are present:
Initially, no ReplicaSets are available. After making configuration changes, a new ReplicaSet may be created. Check its details using the same command:
You might see an output similar to:

3. Analyze ReplicaSet Details

To obtain a detailed description of the ReplicaSet, execute:
Focus on the pod template section; observe the container configuration:
  • Image: busybox777
  • Command:
Below is an excerpt from the output:
The configuration reveals that the pods are configured to run the image busybox777.

4. Check Pod Readiness

Examine the number of ready pods in the ReplicaSet. As seen from the detailed description:
This indicates that none of the pods are ready at this point.

5. Troubleshoot Pod Readiness Issues

Investigate why the pods are not transitioning to a ready state by describing one of them:
The output shows that the pod is in a waiting state with the reason ImagePullBackOff. This error occurs because the image busybox777 cannot be pulled, likely due to a non-existent repository or missing authorization. An excerpt from the pod events:
Kubernetes is unable to pull the image “busybox777” because it either does not exist or you might need to provide proper credentials.

6. Delete a Pod and Observe ReplicaSet Self-Healing

To demonstrate the self-healing nature of ReplicaSets, delete one of the pods. First, list all pods:
You may see output such as:
Delete one pod:
The system confirms deletion:
After deletion, list the pods again:
A new pod will be created automatically by the ReplicaSet to maintain the desired count. Notice that the new pod has a different name and age compared to the existing ones.

7. Create a ReplicaSet Using a Definition File

Now, learn how to create a ReplicaSet from a YAML definition file. First, confirm the existence of the file:
Assuming you see a file named ReplicaSet definition.yaml, attempt to create the ReplicaSet:
If an error similar to the following is returned:
This indicates that the API version in the file is incorrect. The proper API version for ReplicaSets is apps/v1.
Use the command kubectl explain replicaset to verify the correct API version.
Update the file with the correct API version and create it again.

8. Correct a Second ReplicaSet Definition File

Attempt to create a second ReplicaSet using another definition file:
If you encounter this error:
Open the file for editing:
Examine the spec.selector.matchLabels and template.metadata.labels sections. They must match exactly. For example:
After ensuring both fields match, save the file and run:
Verify the creation of all ReplicaSets:
Expected output:
If necessary, delete the extra ReplicaSets:

9. Update the Original ReplicaSet Image

The original ReplicaSet is still configured to use busybox777. To update the image to busybox, edit the ReplicaSet:
Locate the container section and change:
to
Save the changes and exit the editor. Bear in mind that updating the ReplicaSet does not automatically update the running pods; you must delete the existing pods so the ReplicaSet can recreate them with the correct image. List the pods:
Then, delete the current pods:
After deletion, new pods will be created. Verify that they are transitioning from “ContainerCreating” to “Running”:

10. Scaling the ReplicaSet

Scale Up to 5 Pods

Increase the ReplicaSet to five pods with the scale command:
Verify the scale-up by listing the pods again:

Scale Down to 2 Pods

To reduce the number of pods to two, edit the ReplicaSet:
Update the spec.replicas field to 2 and save the file. The ReplicaSet will automatically adjust the pod count accordingly.
This concludes the ReplicaSets lab. In the next lesson, we will explore Deployments and demonstrate how they extend the concepts introduced by ReplicaSets. For more details on Kubernetes concepts, visit the Kubernetes Documentation.

Watch Video