(K8s) Kubernetes lab 106 Labels and Annotations


Labels plays a very important role in kubernetes.

Labels are key/value pair used to tag and select an object in kubernetes.

They can help to select one or group of objects with same label or labels.

You can add them, while creating an object and modify them later if required.

All key's in an object must be unique



key1: value1

key2: value2

You can add multiple labels to your kubernetes objects.

Example :

release : stable

release : alpha

release : beta

environment : development

environment : stage

environment : production

A label key has two parts, an optional prefix and key name, they are separated by / .

Label key prefix must be a DNS subdomain and could have max 253 character.

If there is no prefix, label is assumed to be private to user.

Control plane component and automation tools must add prefix, when adding a label.

Label key name and value are must and could have max 63 character.

They can only start and end with a letter [a-z,A-Z] or numbers [0-9], you can use dash -, underscore _ , dot . or letters and numbers in between.

Label Selectors

Label selectors are used to identify set of objects

Example :

environment = stage

release != stable

environment in (stage, development)

release notin (alpha, beta)




Annotation are similar to labels, they are used for information purpose only.

This information could be fetched later.

Some examples are build, release, timestamps, PR number or any information related to an object.

kubernetes.io and k8s.io is the prefix used by kubernetes core components.

Rules similar to Label apply to Annotation key prefix and name.

Annotation value could be larger and have non human readable data.

Lets Practice

Step 0 : Create docker hub account https://hub.docker.com

Step 1 : Open Play with Kubernetes login with your docker hub account.

Step 2 : Click on start

Step 3 : It will start a 4 hr session, click on + ADD NEW INSTANCE

Step 4 : Click in terminal and run steps from lab 101 to build cluster

Step 5 : kubectl get nodes --show-labels

  • you can observe below labels for node1

  • beta.kubernetes.io/arch=amd64

  • beta.kubernetes.io is key prefix arch is key name and amd64 is value

  • beta.kubernetes.io/os=linux

  • kubernetes.io/arch=amd64

  • kubernetes.io/hostname=node1

  • kubernetes.io/os=linux

  • node-role.kubernetes.io/master=

Step 6 : kubectl label node node2 dc=south

  • this command will label node2 with key=dc and vale=south

Step 7 : kubectl label node node3 dc=east

  • this command will label node2 with key=dc and vale=east

Step 8 : kubectl get nodes -l dc=south

  • this command will list all nodes with label dc and value south

Step 9 : kubectl get nodes -l dc!=south

  • this command will list all nodes excepts nodes label dc and value south

Step 10: kubectl run web --image=nginx

  • this command will create pod with image nginx and name web

Step 11 : kubectl get pods --show-labels

  • this command will list pods in default namespace with their labels

Step 12 : kubectl label pods web enviornment=prod

  • this command will add label to pod web with key=environment and value=prod

Step 13 : kubectl get pods --show-labels

  • this command will list pods in default namespace with their labels

Step 14 : kubectl run web1 --image=nginx -l environment=stage

  • this command will create a new pod web1 with label key=environment and value=stage

Step 15 : kubectl get pods --show-labels

  • this command will list pods in default namespace with their labels

Step 16 : kubectl annotate pods web release=1.0

  • this command will add annotation to pod web with key=release and value=1.0

Step 17 : kubectl get pods web -o yaml | grep -A 4 annotations

  • this command with provide annotation for pod web

Step18 : kubectl annotate pods web release=1.1 --overwrite

  • to change current label or annotation you have to use --overwrite

Step19: kubectl label pods web tier=prod type=test

  • you can add more than one label with single command

Step20 : vi pods.yaml

apiVersion: v1

kind: Pod


name: demo


app: demo

type: web


release: v1.0

delivery: Q3



- name: demo-nginx

image: nginx


- containerPort: 80

  • create yaml file for pod with labels and annotations

Step21: kubectl apply -f pods.yaml

  • use kubectl apply to create pod using pods.yaml

Step22: kubectl get pods demo -o yaml | grep -A 10 annotations

  • you can observe labels and annotations you provided in yaml file under metadata

  • you will observe something similar to below in annotations, this has been added by kuberentes

kubectl.kubernetes.io/last-applied-configuration: |