Skip to content
Rahul Shishodiaon GitHub LinkedIn profile

Labels and Annotations

Labels

  • Key-value pairs for categorizing and selecting objects
  • Max 63 chars; alphanumeric + separators
  • Used by Deployments, Services, NetworkPolicies: essential for selectors
  • Query: kubectl get pods -l key=value
MechanismCLIManifest
Equality-l env=prod (AND with commas)matchLabels
Set-based-l 'team in (a,b)'matchExpressions with In, NotIn, Exists

Label commands

  • kubectl run <n> --image=<img> --labels=k=v,k2=v2
  • kubectl get po --show-labels
  • kubectl label pod <n> key=value / --overwrite / key- to remove
  • app.kubernetes.io/name, version, component, managed-by

Annotations

  • Key-value metadata only: not queryable (-l does not work)
  • No --annotations on kubectl run: YAML or kubectl annotate after create
  • Uses: commit hash, author, release notes, on-call

Reserved annotations

  • kubernetes.io/change-cause: rollout history
  • pod-security.kubernetes.io/enforce: baseline: namespace Pod security

Labels vs annotations

LabelsAnnotations
QueryableYes (-l)No
Used in selectorsYesNo
Imperative at create--labelsNo
Modifykubectl labelkubectl annotate
Removekey-key-

Exam tips

  • Equality selectors: comma = AND, not OR: use in for OR
  • NetworkPolicy podSelector.matchLabels must match Pod labels exactly