How to Troubleshoot and Fix a Kubernetes ReplicaSet
A ReplicaSet manifest is provided at rs.yaml. The manifest contains configuration errors.
rs.yaml manifest:
apiVersion: v1
kind: Replicaset
metadata:
name: webapp
spec:
replicas: 3
selector:
matchLabels:
tier: app
template:
metadata:
labels:
tier: web
spec:
containers:
- name: nginx-container
image: nginxYour task is to review the provided YAML file, identify and fix the errors, and deploy the ReplicaSet in the default namespace.
The ReplicaSet must:
- Be named
webapp - Maintain 3 replicas.
- Use the
nginxcontainer image. - Correctly match the ReplicaSet selector with the Pod labels.
- Have all 3 managed Pods in the Running state.
Ensure that all pods managed by the ReplicaSet are in the Running state.
ReplicaSet, refer to the official Kubernetes documentation.Identify the Errors
There are issues in the provided manifest.
Compare the two manifests
These are the problems you'll identify once you open the file.
Line 1: the wrong API group
apiVersion: v1 is for core resources like Pods, Services, ConfigMaps, and Secrets. ReplicaSet belongs to the apps group, so use apiVersion: apps/v1. In the exam, use kubectl api-resources to check the correct API group.
Line 2: The kind is case sensitive
Replicaset is incorrect. Kubernetes uses ReplicaSet with both words capitalized. Always match the exact capitalization shown by kubectl api-resources.
Line 9: the selector does not match the pod labels
The selector looks for tier: app, but the pod template uses tier: web, so the ReplicaSet cannot match its own pods.
Both values must be identical. Here, change the selector to tier: web.
The API server rejects it with: selector does not match template labels.
Verify your work
All four have to pass for the task to count.
Apply the correct YAML file:
$ kubectl apply -f rs.yaml
replicaset.apps/webapp createdCheck current and ready for all 3 pods:
$ kubectl get rs webapp
NAME DESIRED CURRENT READY AGE
webapp 3 3 3 40sCheck that the three pods are running, not pending or ImagePullBackOff:
$ kubectl get pods -l tier=web
NAME READY STATUS RESTARTS AGE
webapp-9l7wq 1/1 Running 0 81s
webapp-m6bhf 1/1 Running 0 81s
webapp-pvp7h 1/1 Running 0 81s- The ReplicaSet owns those pods:
$ kubectl describe rs webapp | grep -A3 Replicas
Replicas: 3 current / 3 desired
Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: tier=webWhy Does the Selector Need to Match?
A ReplicaSet uses its selector to identify and manage Pods with matching labels. The labels defined in the Pod template must match the ReplicaSet's selector.
For example, matchLabels: tier: web must match the Pod label tier: web. If the labels do not match, the ReplicaSet cannot correctly identify or manage the Pods.
CKA Exam Tips
These tips will help you quickly identify key Kubernetes concepts and avoid common mistakes during the CKA exam and CKA hands-on practice.
Tip 1: Know the API Group
ReplicaSet uses apps/v1. Common resources in this group include Deployment, ReplicaSet, StatefulSet, and DaemonSet.
Tip 2: Kubernetes Resource Kinds Are Case-Sensitive
Use ReplicaSet and Deployment, not Replicaset or deployment.
Tip 3: Always Check Selectors
For a ReplicaSet, make sure selector.matchLabels matches the Pod template's metadata.labels.
Tip 4: Check the Replica Count
If the task requires three Pods, set replicas: 3 and verify with kubectl get rs. Look for DESIRED, CURRENT, and READY all showing 3.