By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Labels, selectors, and annotations are the "metadata glue" that makes Kubernetes (and Docker, to a lesser extent) manageable at scale. Think of them like sticky notes on a filing cabinet—without them, you’d have no way to organize, filter, or automate your workloads.
canary
tier: frontend
tier: backend
env: staging
You’re a DevOps engineer at a fintech startup. Your team deploys 100+ microservices across multiple environments (dev, staging, prod). A security audit flags that old staging pods are still running and consuming resources. Without proper labels, you’d have to manually delete each pod—a nightmare at scale. With labels, you run:
dev
staging
prod
kubectl delete pods -l env=staging
Done in one command.
name
metadata.name
app.kubernetes.io/name
my app
kubectl get
kubectl get pods -l app=nginx
git-commit: abc123
prometheus.io/scrape: "true"
description: "This pod runs the payment service"
env=prod
git-commit
kubectl
apply
get
delete
Goal: Deploy two versions of an app (v1 and v2) and use labels to route 10% of traffic to v2 (canary testing).
v1
v2
# app-v1.yaml apiVersion: apps/v1 kind: Deployment metadata: name: myapp-v1 spec: replicas: 3 selector: matchLabels: app: myapp version: v1 template: metadata: labels: app: myapp version: v1 spec: containers: - name: nginx image: nginx:1.23 ports: - containerPort: 80
Apply it:
kubectl apply -f app-v1.yaml
# app-v2.yaml apiVersion: apps/v1 kind: Deployment metadata: name: myapp-v2 spec: replicas: 1 # Only 1 pod for canary (10% of total traffic) selector: matchLabels: app: myapp version: v2 template: metadata: labels: app: myapp version: v2 spec: containers: - name: nginx image: nginx:1.24 ports: - containerPort: 80
kubectl apply -f app-v2.yaml
# service.yaml apiVersion: v1 kind: Service metadata: name: myapp-service spec: selector: app: myapp # Matches BOTH v1 and v2 pods ports: - protocol: TCP port: 80 targetPort: 80
kubectl apply -f service.yaml
bash kubectl get pods -l app=myapp --show-labels
NAME READY STATUS LABELS myapp-v1-5f7d8c6b7c-abc12 1/1 Running app=myapp,version=v1 myapp-v1-5f7d8c6b7c-def34 1/1 Running app=myapp,version=v1 myapp-v1-5f7d8c6b7c-ghi56 1/1 Running app=myapp,version=v1 myapp-v2-7d8f9g0h1i-jkl78 1/1 Running app=myapp,version=v2
bash kubectl get endpoints myapp-service -o yaml
bash for i in {1..10}; do kubectl exec -it <any-pod> -- curl myapp-service; done
kubectl delete -f app-v1.yaml -f app-v2.yaml -f service.yaml
password: 12345
app.kubernetes.io/
app.kubernetes.io/name: myapp
cost-center: marketing
lifecycle: delete-after-30d
kube-janitor
env
environment
stage
version: v1
contact: [email protected]
fluentd-tag: app.logs
kubernetes.io/change-cause: "Updated to v2.1.0"
Running
spec.selector.matchLabels
spec.template.metadata.labels
kubectl get -l
kubectl get pods --show-labels
environment=production
app
version
123app
✅ app (valid)
"How do you delete all pods with env=staging?"
env=staging
kubectl delete pods --selector env=staging
❌ kubectl delete pods --annotation env=staging (annotations ≠ labels)
kubectl delete pods --annotation env=staging
"What happens if a Service’s selector doesn’t match any pods?"
kubectl get endpoints <service> will show no IPs.
kubectl get endpoints <service>
"Can you use annotations for filtering in kubectl get?"
You have a Kubernetes cluster with 10 pods running different versions of an app (v1, v2, v3). Delete all v2 pods without affecting v1 or v3.
v3
kubectl delete pods -l version=v2
Why it works:- -l version=v2 selects only pods with the label version: v2.- The other pods (v1, v3) remain untouched.
-l version=v2
version: v2
app=nginx
kubectl label pod mypod env=prod
kubectl annotate pod mypod description="API service"
kubectl describe pod mypod
Annotations:
spec.selector.app: myapp
spec.selector.matchLabels.app: myapp
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.