This lab explores ReplicaSets in Kubernetes, covering verification, troubleshooting, scaling, and modifying ReplicaSets through practical commands and configurations.
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.
Initially, no ReplicaSets are available. After making configuration changes, a new ReplicaSet may be created. Check its details using the same command:
Copy
Ask AI
kubectl get replicaset
You might see an output similar to:
Copy
Ask AI
NAME DESIRED CURRENT READY AGEnew-replica-set 4 4 0 9s
Investigate why the pods are not transitioning to a ready state by describing one of them:
Copy
Ask AI
kubectl describe pod new-replica-set-7r2qw
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:
Copy
Ask AI
State: WaitingReason: ImagePullBackOff...Events: Normal Pulling ... kubelet Pulling image "busybox777" Warning Failed ... kubelet Failed to pull image "busybox777": pull access denied, repository does not exist or may require authorization...
Kubernetes is unable to pull the image “busybox777” because it either does not exist or you might need to provide proper credentials.
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.
Attempt to create a second ReplicaSet using another definition file:
Copy
Ask AI
kubectl create -f replicaset-definition-2.yaml
If you encounter this error:
Copy
Ask AI
The ReplicaSet "replicaset-2" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"tier":"nginx"}: selector does not match template labels
Open the file for editing:
Copy
Ask AI
vi replicaset-definition-2.yaml
Examine the spec.selector.matchLabels and template.metadata.labels sections. They must match exactly. For example:
The original ReplicaSet is still configured to use busybox777. To update the image to busybox, edit the ReplicaSet:
Copy
Ask AI
kubectl edit rs new-replica-set
Locate the container section and change:
Copy
Ask AI
image: busybox777
to
Copy
Ask AI
image: busybox
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:
Copy
Ask AI
kubectl get pods
Then, delete the current pods:
Copy
Ask AI
kubectl delete pod new-replica-set-vpkh8 new-replica-set-tn2mp new-replica-set-7r2qw new-replica-set-hcmbw
After deletion, new pods will be created. Verify that they are transitioning from “ContainerCreating” to “Running”:
To reduce the number of pods to two, edit the ReplicaSet:
Copy
Ask AI
kubectl edit rs new-replica-set
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.