Kubernetes : Labels and Annotation

In this blog, we will explore about Labels and Annotation. We will explain their use cases so that we can decide whether to use labels or annotation for a particular use case.

In my previous blog we have already seen different kinds of pods and their life cycle.

In general, we always want to organize our pods based on some properties so that we can query against specific pods. To do so we will be add labels to the pods.


  1. Functional Kubernetes Cluster configured
  2. Kubernetes’ command-line tool, kubectl, which is the command-line interface for running commands against Kubernetes clusters.
  3. Clone this git repo for all the yaml files which we will be using in this blog


  1. Labels
  2. Pod Create with Labels
  3. Adding Labels to a Running Pod
  4. Select Kubernetes objects using Label selector
  5. Modify/Delete the Label
  6. Annotation
  7. Pod create with Annotation
  8. Modify/Delete the annotation


  • They are are the metadata which contain unique information of the Kubernetes objects
  • key-value pair attached to the objects like pod
  • We can attach the label at the time of creation or we can update it at runtime
  • Kubernetes internally maintains the mappings of labels to corresponding objects using optimized data structures to make these queries faster.
  • With labels, you can also run your specific pods on selected nodes.

Organize Pods by team/project/organization

  • You can organize pods so that each team will see only their pods
  • Specific to certain projects
environment: development
team: infra
project: k8s-infra

Run a Pod on Specific Nodes

This is a very basic requirement when you want to run your pod on a specific node because of its hardware/specific configuration etc.

We can do that using node-selector

region: east-us
disktype: ssd

Create a Pod with Labels

kind: Pod
apiVersion: v1
name: pod-with-labels
app: nginx
foo: bar
- name: first-container
image: nginx

Create a pod

kubectl create -f pod-with-labels.yaml
Image for post
Image for post

Adding Labels to a Running Pod

You can update the existing label or add the new one using below command

kubectl label pod pod-with-labels app=nginx1 --overwrite
kubectl label pod pod-with-labels env=dev
kubectl label pod pod-with-labels team=dev org=abc
Image for post
Image for post

How to remove a label

kubectl label pod pod-with-labels env-

Selecting Kubernetes Objects Using Label Selectors

kubectl get pods -l app=nginx1
kubectl get pods -l app=nginx1,foo=bar
Image for post
Image for post


Annotations are also key-value pairs that can be used to store the unstructured information pertaining to the Kubernetes objects.

Annotations can be used to add timestamps, commit SHA, issue tracker links, or names/information about users who are responsible for specific objects in an organization

Annotations can also be used to add information about client libraries or tools.

kind: Pod
apiVersion: v1
name: pod-with-annotation
commit-message: testign for nginx
owner: Nidhi

- name: first-container
image: nginx

Create a Pod

kubectl create -f pod-with-annotation.yaml

How to add annotation using command line

kubectl annotate pod pod-with-annotation jiralink=DED-232
kubectl annotate --overwrite pod <pod_name> <annotation_key>=<annotation_label>

Delete the annotation

kubectl annotate pod pod-with-annotation jiralink-

You can find more articles on Kubernetes on my website.

Written by

Devops Automation Enginneer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store