Skip to content

Annotation vs Label

Both are in metadata.

Labels

  • Used to organize and manage your objects
  • usually intern to kubernetes
  • used to select objects

Examples of usage

  • Environments (not use to isolate of segregate) like dev, test, prod
  • Roles (frontend, backend, db)
  • Teams (dev, ops, sec)
  • Version (v1, v2)
  • verbosity
  • location

Allow you to

  • selector in deployment
  • selector in service
  • group logs (selector) ex: kubectl logs -l app=frontend
  • filter objects (selector)
  • podAffinity

Built-in labels

  • docs

  • kubectl describe nodes

example:

...
Labels:
  beta.kubernetes.io/arch=arm64
  beta.kubernetes.io/os=linux
  kubernetes.io/arch=arm64
  kubernetes.io/hostname=cb-node-2
  kubernetes.io/os=linux
  kubernetes.io/role=worker
...
  • kubectl get pods --show-labels
  • kubectl get all --show-labels --all-namespaces

-L vs -l

  • kubectl get pods -L app,env to show labels
  • kubectl get pods -l app=frontend to filter by label

Logs

app=... is only available whenyouuse a deployment

  • kubectl logs -l app=frontend to show logs of all pods with label app=frontend
  • to stream : kubectl logs -l app=frontend -f but only 5 pods in the same time are allowed

Manage labels in cli

  • kubectl label pod <pod-name> env=dev
  • kubectl label pod <pod-name> env=dev --overwrite (used after the first time)
  • kubectl label pod <pod-name> env- to remove the label

In deployment

** Always put all the labels of the pods template in the selectror/matchLabels. **

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  namespace: my-app
  labels:
    # labels for the deployment
    app: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      # labels for the pods always put all the labels of the pods template
      app: my-app
      env: dev
  template:
    metadata:
      labels:
        # labels for the pods
        app: my-app
        env: dev
    spec:
      containers:
        - name: my-app
          image: nginx

Annotations

  • Used to calculate something, controllers, affinity etc.
  • Usually contain a domain name

Examples of usage

Used by controllers to store information about the object.

  • rollout, everry time you update the deployment a new annotation will be created
  • pods
  • deployment
  • service
  • etc

Allow

  • restart pods on configmap change
  • configure ingress controllers
  • add consul tags
  • add scrape & config prometheus