Skip to main content
In this lab you’ll practice Kubernetes labels and selectors: filtering objects by labels, counting matched objects, combining multiple label selectors, and fixing a ReplicaSet manifest that fails validation because its selector does not match the pod template labels. What you’ll learn
  • How to filter Kubernetes objects with label selectors (-l / --selector)
  • How to count matched objects using --no-headers and wc -l
  • How to require multiple labels in a single selector
  • How to resolve ReplicaSet validation errors caused by mismatched selectors and pod template labels
Overview of the scenario
  • Several pods are deployed and labeled with env, bu, and tier.
  • Tasks: count pods in label groups, list objects matching selectors, find pods matching multiple labels, and correct a ReplicaSet manifest that fails validation.

Inspect available pods

First, list pods to see the objects you can filter:
This shows pod names, readiness, status, restarts, and age. Labels are key=value pairs and selectors filter objects by those pairs. Labels are case-sensitive. Example: get pods for the dev environment:

Count matched objects

When many objects match a selector, count them using --no-headers and wc -l to avoid counting the column header:
This returns the number of pods labeled env=dev. Count pods for the finance business unit (bu label):
Count all object types in a specific environment (pods, Services, ReplicaSets, etc.):
Example outputs and counting

Find a pod matching multiple labels

Combine multiple label expressions with commas to require all labels. For example:
Example output:

Common selector patterns

ReplicaSet validation error and how to fix it

If a ReplicaSet is rejected with an error like: “spec.template.metadata.labels: Invalid value: map[string]string{“tier”:“nginx”}: selector does not match template labels” then the ReplicaSet selector (spec.selector.matchLabels) does not match the labels on the pod template (spec.template.metadata.labels). Kubernetes requires that the selector selects the same labels that are on the pod template. Problematic manifest (replicaset-definition-1.yaml):
The selector expects tier=front-end but the pod template has tier=nginx. To fix this, make the selector and the template labels identical. You can either change the selector to match the template labels or change the template labels to match the selector. Fixed manifest (both selector and template set to tier: nginx):
Create the ReplicaSet and verify:
A ReplicaSet’s spec.selector.matchLabels must match labels on the pod template (spec.template.metadata.labels). If they do not match, Kubernetes will reject the ReplicaSet as invalid. Keep selector keys and values identical to the template labels you expect the ReplicaSet to manage.

Troubleshooting tips

  • If kubectl create -f fails with a selector/template mismatch, inspect both spec.selector.matchLabels and spec.template.metadata.labels for spelling, hyphens, and case differences.
  • Use kubectl get pods --show-labels to see current labels applied to pods.
  • Remember label keys and values are case-sensitive: env=Prod ≠ env=prod.

Summary / Best practices

  • Use kubectl get with -l/--selector to filter objects by labels.
  • Use --no-headers with wc -l to count results reliably.
  • Combine multiple labels with commas to require all conditions (AND logic).
  • Always ensure selectors on controllers (ReplicaSet, Deployment) exactly match the pod template labels to avoid validation failures.

Watch Video