Kubernetes is a container orchestration tool, provides a platform for automating deployment, scaling, and management of our containers. Kubernetes Labels and Kubernetes Annotations are one of the main components. They both provide a way for adding additional metadata to our Kubernetes Objects.

Are you new to Kubernetes? Check out our blog Kubernetes for Beginners to know in detail.

What Is Kubernetes Labels?

Labels in Kubernetes are intended to be used to specify identifying attributes of objects that are meaningful and relevant to users but are not used by the Kubernetes itself. Labels are fundamental qualities of the object that will be used for grouping, viewing, and operating. Each object can have a set of key/value labels defined. Each Key must be unique for a given object.

Labels have a simple syntax, where both the key and value are represented by strings. Label keys can be broken down into two parts: an optional prefix and a name, separated by a slash(/). The prefix can be a DNS sub-domain with a 253-character limit. The key name is required and must be shorter than 63 characters, beginning and ending with an alphanumeric character (a-z0-9A-Z) with dashes (), underscores (_), dots (.), and alphanumerics between.

Label values are strings with a maximum length of 63 characters. The contents of the labels follow the same rules as for label keys. Here are some examples of labels in Kubernetes
1) key – kubernetes.io/cluster-service
value – true
2) key – appVersion
value – 1.0.0

Also Read: Our blog post on Persistent Storage in Kubernetes. Click here

Creating Labels In Kubernetes

Labels are attached to the Kubernetes objects and always defined in the metadata section of the configuration files.
The commonly used key and label conventions are:

 #Using environment as a key:
  environment: "dev"
  environment: "production"
  environment: "qa"
 # Using release as a key:
  release: "canary"
  release: "stable"
 #Using app as a key:
  app: "name of your app"

Also Read: our blog post on Kubernetes Scheduler

Here’s the configuration file for a Pod that has two labels “environment: dev ” and “app: <name of the app>

apiVersion: v1
  kind: Pod
  metadata:

      name: <name of the pod>
      labels:
          environment: dev
          app: <name of the app>
  spec:
      containers:
      -  name: <name of the container>
         image: <name of the image>
         ports:
         -  containerPort: 80

Save the file as pod.yaml and then run the following command:

  $ kubectl create -f pod.yaml

To see the labels use the following command:

 $ kubectl get pods --show-labels


We can also specify labels through a kubectl command when creating a resource such as a Pod.

 $ kubectl run <pod name> --image=<imagename>  --labels="key=value, key=value"


We can see the labels using the following command:

 $ kubectl get pods --show-labels

Read More: About Kubernetes cluster setup: A Complete Step-by-Step Guide

Label Selectors

Label selectors are used for filter Kubernetes objects based on a set of labels. Selectors use a simple Boolean language.
There are two kinds of selectors: Equality based and Set based.

Equality based

Equality-based selectors allow filtering by label keys and values. Three kinds of operators are used: =, ==, !=
Example: If we wanted to list all the Pods that had the environment label set to dev, we can use the selector flag:

 $ kubectl get pods --selector="environment=dev"

Check Out: Our blog post on Multi Container Pod Kubernetes. Click here

Set-based

Set-based label selectors allow filtering keys according to a set of values. Three kinds of operators used: in, notin, exists
Example: If we wanted to list all the Pods that had the environment label set to dev or production:

 $ kubectl get pods --selector="environment in (dev, production)"

Install Docker on windows, ubuntu, mac with easy steps.

Services And Labels

A Service sits in front of our Pods and distributes requests to them. They route traffic across the Pods. Services are the abstraction that allows pods to die and replicate in Kubernetes without impacting the application.
They use Kubernetes Labels and Selectors to match a set of Pods.

Also read: our blog on Kubernetes Networking Services

What Are Kubernetes Annotations?

Annotations in Kubernetes provide a place to store non-identifying metadata for Kubernetes Objects which can be used to get a more elaborate context for an object.
Annotations are also key/value pairs like Labels. Annotation keys use the same format as Label keys.

Why Annotations Are Used?

  • Keep track of a “reason” for the latest upgrade to an object.
  • Communicate a specialized scheduling policy to a specialized scheduler.
  • Extend data about the last tool to update the resource and how it was updated (used for detecting changes by other tools and doing a smart merge).
  • Attach build, release, or image information that isn’t appropriate for labels.
  • Enable the Deployment object to keep track of ReplicaSets that it is managing for rollouts.
  • Phone or pager numbers of persons responsible, or directory entries that specify where that information can be found, such as a team web site.

Also read: What is the difference between Container vs VM

Creating Annotations In Kubernetes

Annotations are defined in the common metadata section in every Kubernetes object.
Here’s the configuration file for a Pod that has the annotation.

apiVersion: v1
  kind: Pod
  metadata:
      name: <Pod name>
      annotations:
          imageregistry: "https://hub.docker.com/"
  spec:
      containers:
      -  name: <Container name>
         image: <image name>
         ports:
         -  containerPort: 80

Save the above file with annotations.yaml and run the following command:

 $ kubectl create -f annotation.yaml

Also Check: Our blog post on Kubernetes pod. Click here

To see the annotations, use the following command:

  $ kubectl describe pods <name of the pod>

Also Read: what is helm Kubernetes?

Difference Between Labels And Annotations

Kubernetes Labels and Kubernetes Annotations are used to add the metadata to our Kubernetes objects. But there is a difference between both of them. Kubernetes Labels allow us to do a grouping of our objects so that we can perform queries for viewing and operating.

Kubernetes Annotations are used for adding non-identifying metadata to Kubernetes objects. This metadata information is only for the user. Annotations can hold any kind of information that is useful and can provide context to DevOps teams. Examples include phone numbers of persons responsible for the object or tool information for debugging purposes.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *