CodeBlanks Logo Icon CodeBlanks

Summary - Labels

Combine labels and selectors to group and filter resources.

Summary: Labels

In this group:

  • Labels are selectable key-values; annotations are non-selectable extra info.
  • kubectl label adds, --overwrite changes, key- removes; annotate works with the same syntax.
  • Selectors: matchLabels (equality) and matchExpressions (In, NotIn, Exists, DoesNotExist); a Deployment selector is immutable.
  • -l filters (comma = AND), -L adds columns; bulk deletes, logs and restarts are done with selectors.
  • Set expressions are quoted: -l 'ortam in (prod,staging)'.
kubectl label pods -l app=web surum=v2
kubectl get pods -l 'surum in (v1,v2)' -L surum

Practice Recommended labels

Write a Deployment template's metadata section (starting with metadata:): under labels, app.kubernetes.io/name: web, app.kubernetes.io/version: "1.4.2", app.kubernetes.io/part-of: magaza; under annotations, aciklama: "ana sayfa servisi".

Starter code

terminal
# buraya yaz

Sample solution

terminal
metadata:
  labels:
    app.kubernetes.io/name: web
    app.kubernetes.io/version: "1.4.2"
    app.kubernetes.io/part-of: magaza
  annotations:
    aciklama: "ana sayfa servisi"

Expected output

deployment.apps/web configured

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Why isn't a version label put in a Deployment selector?

  • The selector is immutable; when the version changes, the deployment would have to be recreated Correct answer
  • Version labels are forbidden
  • The Service can't see it
  • The label is too long

Explanation: The version goes in the template label, not the selector.

2 Add a new label to all pods that have a label.

Question
kubectl label pods ____ app=web surum=v2
Answer
kubectl label pods -l app=web surum=v2

Explanation: All pods selected with -l are labelled.

3 Match.

Term Meaning
label For selection
annotation For information
-l Filter
-L Column

All lessons in Intermediate