CodeBlanks Logo Icon CodeBlanks

What is a label?

Learn to tag resources with labels.

What is a label?

A label is a key: value pair attached to objects. Kubernetes links objects to each other not by name but by label: a Service finds its pods and a Deployment its ReplicaSets by label. Labels are also used for filtering and organization:

metadata:
  name: web-7c5b9f6d8-abcde
  labels:
    app: web
    katman: on
    ortam: prod
    app.kubernetes.io/version: "1.4.2"

The recommended common labels are app.kubernetes.io/name, /instance, /version, /component and /part-of.

Label rules

A label key consists of an optional prefix (domain.com/) and a name of at most 63 characters; the value is also at most 63 characters and may contain letters, digits, -, _ and .. Spaces and Turkish characters can't be used. Numbers like version numbers are quoted in YAML:

labels:
  app: web            # valid
  surum: "2"          # a number: quote it
  takim: odeme-ekibi  # valid
  aciklama: ana sayfa # INVALID: space

Practice A labelled pod

Write a pod YAML: name web; labels app: web, katman: on, ortam: prod; container named nginx, image nginx:1.27.

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: v1
kind: Pod
metadata:
  name: web
  labels:
    app: web
    katman: on
    ortam: prod
spec:
  containers:
    - name: nginx
      image: nginx:1.27

Expected output

pod/web created

Questions and answers

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

1 What is the difference between a label and an annotation?

  • Labels can't be changed
  • Labels are used in selectors; annotations are extra info that can't be selected Correct answer
  • They are the same
  • Annotations exist only on pods

Explanation: Annotations are for data like long text, URLs and tool settings.

2 Define the object's labels.

Question
metadata:
  name: web
  ____:
    app: web
Answer
metadata:
  name: web
  labels:
    app: web

Explanation: labels is under metadata.

3 Match.

Term Meaning
label Selectable key-value
annotation Non-selectable extra info
app.kubernetes.io/name Recommended common label
63 karakter Name/value limit

All lessons in Intermediate