0
0
Kubernetesdevops~10 mins

Annotations vs labels in Kubernetes - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Process Flow - Annotations vs labels
Start: Define Metadata
Add Labels
Used for Selection & Grouping
Add Annotations
Used for Non-Selector Metadata
Apply to Kubernetes Objects
Labels affect Queries & Scheduling
Annotations store Extra Info
End
Labels are key-value pairs used to select and organize objects. Annotations store extra metadata not used for selection.
Execution Sample
Kubernetes
apiVersion: v1
kind: Pod
metadata:
  name: mypod
  labels:
    app: frontend
  annotations:
    description: "This pod runs the frontend app"
Defines a pod with a label for grouping and an annotation for extra info.
Process Table
StepActionMetadata AddedEffect on Object
1Define pod metadataNonePod created with name 'mypod'
2Add label 'app: frontend'labels: {app: frontend}Pod can be selected by label 'app=frontend'
3Add annotation 'description'annotations: {description: 'This pod runs the frontend app'}Extra info stored, no effect on selection
4Apply metadata to podlabels and annotations setPod is ready with labels and annotations
5Use label selector 'app=frontend'labels usedPod is selected by controllers or queries
6Use annotation infoannotations usedAnnotations can be read by tools but not selectors
💡 All metadata applied; labels affect selection, annotations store extra info only
Status Tracker
VariableStartAfter Step 2After Step 3Final
metadata.labels{}{app: frontend}{app: frontend}{app: frontend}
metadata.annotations{}{}{description: 'This pod runs the frontend app'}{description: 'This pod runs the frontend app'}
Key Moments - 2 Insights
Why can't annotations be used to select pods like labels?
Annotations are not indexed or used by Kubernetes for selection, as shown in execution_table step 6 where only labels affect selection.
Can a pod have multiple labels and annotations at the same time?
Yes, as shown in variable_tracker, labels and annotations are separate dictionaries and can hold multiple key-value pairs simultaneously.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what metadata is added to the pod?
Aannotations: {description: 'This pod runs the frontend app'}
Blabels: {app: frontend}
Clabels: {description: 'This pod runs the frontend app'}
Dannotations: {app: frontend}
💡 Hint
Check the 'Metadata Added' column at step 2 in execution_table
At which step does the pod become selectable by label?
AStep 2
BStep 5
CStep 6
DStep 3
💡 Hint
Look at the 'Effect on Object' column for label selection in execution_table
If we remove the label, what happens to pod selection?
APod is deleted automatically
BPod can still be selected by annotations
CPod cannot be selected by label selectors
DAnnotations become labels
💡 Hint
Recall that annotations do not affect selection as per execution_table step 6
Concept Snapshot
Labels and annotations are key-value pairs in Kubernetes metadata.
Labels are used to select and group objects.
Annotations store extra info not used for selection.
Labels affect queries, scheduling, and controllers.
Annotations are for tools and debugging info.
Use labels for grouping; annotations for details.
Full Transcript
In Kubernetes, objects like pods have metadata that includes labels and annotations. Labels are key-value pairs used to select and group objects, for example, selecting pods with label 'app=frontend'. Annotations also store key-value pairs but are used to hold extra information that does not affect selection or grouping. When creating a pod, you can add labels and annotations under metadata. Labels help controllers and queries find the pod, while annotations provide additional details for tools or humans. Labels are indexed and used by Kubernetes internally, but annotations are not. This distinction is important for organizing and managing Kubernetes resources effectively.