CodeBlanks Logo Icon CodeBlanks

Adding labels

Learn to define labels in YAML.

Adding labels

Labels can be written in the YAML or added to and removed from running objects with kubectl label. Changing an existing key requires --overwrite; putting - at the end of the key removes the label:

kubectl label pod web takim=odeme
kubectl label pod web ortam=test --overwrite
kubectl label pod web takim-                 # remove
kubectl label pods -l app=web surum=v2       # add in bulk

Adding annotations

kubectl annotate adds annotations with the same syntax as labels. Tools keep their settings in annotations (e.g. Ingress controllers, Prometheus):

kubectl annotate deploy web aciklama="ana sayfa servisi"
kubectl annotate svc web prometheus.io/scrape="true"
kubectl get deploy web -o jsonpath='{.metadata.annotations.aciklama}'

Annotation values can contain spaces and long text; labels can't.

Practice Manage labels

Write three commands: add the label takim=odeme to the web pod; overwrite the ortam label with test; remove the katman label.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl label pod web takim=odeme
kubectl label pod web ortam=test --overwrite
kubectl label pod web katman-

Expected output

pod/web labeled
pod/web labeled
pod/web unlabeled

Questions and answers

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

1 What does kubectl label pod web takim- do?

  • It removes the takim label Correct answer
  • It adds the takim label with an empty value
  • It errors
  • It deletes the pod

Explanation: The trailing hyphen means removal.

2 Remove the label.

Question
kubectl label pod web ____
Answer
kubectl label pod web takim-

Explanation: A hyphen is appended to the key name.

3 Match.

Term Meaning
label key=value Add a label
--overwrite Change
key- Remove
annotate Add an annotation

All lessons in Intermediate