📖 Scenario: You are managing a Kubernetes cluster for a small company. You want to organize your pods by adding metadata that helps you identify and manage them easily.Labels and annotations are two ways to add metadata to Kubernetes objects. Labels are used for grouping and selecting objects, while annotations store additional information that is not used for selection.
🎯 Goal: Learn how to add labels and annotations to a Kubernetes pod manifest and understand the difference between them.
📋 What You'll Learn
Create a pod manifest with specific labels
Add annotations to the pod manifest
Use kubectl commands to view labels and annotations
Understand the difference between labels and annotations
💡 Why This Matters
🌍 Real World
In real Kubernetes clusters, labels help you group and manage resources easily, for example, to deploy updates or monitor specific sets of pods. Annotations provide extra information like build version or contact info that tools or humans can use.
💼 Career
Understanding labels and annotations is essential for Kubernetes administrators and DevOps engineers to organize, manage, and automate cluster resources effectively.
Progress0 / 4 steps
1
Create a Pod manifest with labels
Create a YAML file named pod.yaml with a pod named myapp-pod in the default namespace. Add the following labels under metadata.labels: app: myapp and env: production.
Kubernetes
Hint
Labels go under metadata.labels as key-value pairs.
2
Add annotations to the Pod manifest
Add annotations under metadata.annotations in the same pod.yaml file. Add these exact annotations: description: "My application pod" and owner: "team-a".
Kubernetes
Hint
Annotations go under metadata.annotations as key-value pairs, similar to labels.
3
Use kubectl to view labels and annotations
Run the command kubectl apply -f pod.yaml to create the pod. Then run kubectl get pod myapp-pod --show-labels to see the labels. Next, run kubectl describe pod myapp-pod and look for the annotations section.
Kubernetes
Hint
Use kubectl apply to create the pod, kubectl get with --show-labels to see labels, and kubectl describe to see annotations.
4
Print the difference between labels and annotations
Write a command to print exactly this text: Labels are for grouping and selecting objects; annotations store additional metadata not used for selection.
Kubernetes
Hint
Use echo with the exact text.
Practice
(1/5)
1. What is the primary purpose of labels in Kubernetes?
easy
A. To group and select objects for operations like deployments and services
B. To store large binary data inside objects
C. To provide detailed descriptions for human readers only
D. To encrypt sensitive information within the cluster
Solution
Step 1: Understand labels' role
Labels are key-value pairs used to organize and select Kubernetes objects for management tasks.
Step 2: Differentiate from annotations
Annotations store metadata but are not used for selecting or grouping objects.
Final Answer:
To group and select objects for operations like deployments and services -> Option A
Quick Check:
Labels = grouping and selection [OK]
Hint: Labels are for grouping; annotations are for extra info [OK]
Common Mistakes:
Confusing annotations as selectors
Thinking labels store large data
Assuming labels encrypt data
2. Which of the following is the correct syntax to add a label named env=production to a pod YAML?
easy
A. metadata:
annotations:
env: production
B. spec:
labels:
env: production
C. metadata:
labels:
- env=production
D. metadata:
labels:
env: production
Solution
Step 1: Locate correct YAML section
Labels belong under metadata.labels in Kubernetes YAML.
Step 2: Check syntax format
Labels are key-value pairs, not lists or under spec.
Final Answer:
metadata:
labels:
env: production -> Option D
Quick Check:
Labels under metadata.labels with key: value [OK]
Hint: Labels go under metadata.labels as key-value pairs [OK]
Common Mistakes:
Putting labels under spec section
Using annotations instead of labels
Writing labels as a list instead of map
3. Given this pod YAML snippet, what will kubectl get pods -l app=web return?
metadata:
labels:
app: web
annotations:
description: "Frontend web server"
medium
A. The command will fail due to syntax error
B. The pod will not be listed because annotations are ignored
C. The pod will be listed because it has label app=web
D. The pod will be listed only if annotation description exists
Solution
Step 1: Understand label selector behavior
The command kubectl get pods -l app=web lists pods with label app=web.
Step 2: Check pod labels and annotations
The pod has label app: web, so it matches the selector. Annotations do not affect selection.
Final Answer:
The pod will be listed because it has label app=web -> Option C
Hint: Label selectors ignore annotations, only match labels [OK]
Common Mistakes:
Thinking annotations affect label selectors
Assuming command fails due to annotations
Confusing labels and annotations roles
4. You want to add extra metadata to a deployment without affecting its selection. Which is the correct fix if you mistakenly added this under metadata.labels instead of metadata.annotations?
metadata:
labels:
description: "Handles user login"
medium
A. Move the key-value pair under metadata.annotations instead of labels
B. Keep it under labels but rename key to 'desc'
C. Remove the key-value pair entirely
D. Add the key-value pair under spec.containers
Solution
Step 1: Identify misuse of labels
Labels affect selection and grouping; adding descriptive info there can cause unintended behavior.
Step 2: Correct placement for metadata
Annotations are meant for extra metadata that does not affect selection, so move the key-value pair there.
Final Answer:
Move the key-value pair under metadata.annotations instead of labels -> Option A
Quick Check:
Metadata info belongs in annotations, not labels [OK]
Hint: Put extra info in annotations, not labels [OK]
Common Mistakes:
Using labels for descriptive metadata
Renaming keys instead of moving
Removing useful metadata
5. You have a service that selects pods with label tier=backend. You want to add a note about the pod's version without affecting service selection. Which YAML snippet correctly applies this?
hard
A. metadata:
labels:
tier: backend
version: "v2.1"
B. metadata:
labels:
tier: backend
annotations:
version: "v2.1"
C. metadata:
annotations:
tier: backend
version: "v2.1"
D. spec:
labels:
tier: backend
annotations:
version: "v2.1"
Solution
Step 1: Identify label for selection
The service selects pods with label tier=backend, so this label must be present under metadata.labels.
Step 2: Add version note without affecting selection
Version info is metadata and should go under metadata.annotations to avoid changing label selectors.
Final Answer:
metadata:
labels:
tier: backend
annotations:
version: "v2.1" -> Option B
Quick Check:
Labels for selection, annotations for extra info [OK]
Hint: Keep selectors in labels; notes in annotations [OK]
Common Mistakes:
Putting version as label and changing selectors
Putting tier in annotations losing selection
Placing labels or annotations under spec
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.