This guide reviews the ReplicaSets lab exercise, detailing commands, expected outputs, and steps for diagnosing issues and scaling efficiently.
In this guide, we review the ReplicaSets lab exercise by detailing each command, expected output, and the rationale behind every step. Follow along to understand how to diagnose issues, update configurations, and scale your ReplicaSet efficiently.
ReplicaSets are designed to maintain the desired number of Pods automatically. Even if you delete a Pod, the ReplicaSet immediately replicates a new one.
The ReplicaSet "replicaset-2" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"tier":"nginx"}: selector does not match template labels
This occurs because the selector (tier: frontend) does not match the Pod template labels (tier: nginx). Correct the Pod template labels to use tier: frontend so they match the selector. After this fix, create the ReplicaSet again using:
Save the changes and verify by describing the ReplicaSet:
Copy
Ask AI
kubectl describe rs new-replica-set
Despite the update, existing Pods may still reflect the previous error because updating the ReplicaSet does not restart the existing Pods. To resolve this issue, manually delete the problematic Pods so that the ReplicaSet creates new ones with the corrected image:
Copy
Ask AI
kubectl delete pod new-replica-set-vpkh8 new-replica-set-tn2mp new-replica-set-7r2qw
After a short wait, list the Pods again:
Copy
Ask AI
kubectl get pods
New Pods should appear and transition to the Running state.
Below is a summary of the commands used throughout this lab exercise:
Copy
Ask AI
# Check initial statekubectl get podskubectl get replicaset# After changes, verify new ReplicaSet and its detailskubectl get replicasetkubectl describe replicaset new-replica-set# Determine the image used and check Pod errorkubectl describe pod new-replica-set-7r2qw# Demonstrate automatic Pod replacementkubectl delete pod new-replica-set-wkzjhkubectl get pods# Create ReplicaSets from YAML and fix errorskubectl create -f /root/replicaset-definition-1.yamlkubectl create -f /root/replicaset-definition-2.yaml# Delete newly created ReplicaSetskubectl delete rs replicaset-1kubectl delete rs replicaset-2# Update the image in the original ReplicaSetkubectl edit rs new-replica-set# Delete faulty Pods so new ones are created with the correct imagekubectl delete pod new-replica-set-vpkh8 new-replica-set-tn2mp new-replica-set-7r2qw# Scale the ReplicaSet upwardkubectl scale rs new-replica-set --replicas=5kubectl get pods# Scale the ReplicaSet downward using editingkubectl edit rs new-replica-set
Happy learning and successful Kubernetes management!