Kubernetes Labels and annotations (Kubernetes Course – 12)

In the previous post we talked about Namespaces. In this post I will cover Kubernetes Labels and annotations.

Difference between labels and annotations?

Kubernetes allows adding additional info to an object using Labels and annotations. The purpose of labels and annotations, however, differ. The label information can be used to select objects by using a selector while querying for the object. Annotation on the other hand is only used to provide additional information for the object.

Kubernetes Labels

Kubernetes labels allow adding additional information to Kubernetes objects and also allow selecting those objects using selectors. Lets look at an example. Open up the yaml that we used to deploy the static website.

Kubernetes Labels and annotations

The deployment consists of a deploymentSpec which contains podTemplateSpec. The podTemplateSpec describes what pods will be created. In the metadata of the podTemplateSpec we specify that the pod should have a label whose key is ‘app’ and value is ‘static-website’. We can also see in the yaml that the DeploySpec contains a selector that uses this label to find out which pods are affected by this deployment. In a way the label ties the pod or the replicaset to the deployment.
It is generally a best practice to use labels that can help identify, manage and organize objects. Labels are generally the most efficient way to give a structure to a Kubernetes cluster since there in inherently no structure in the cluster and it can be thought of as a loose collection of pods and services.

Recommended Kubernetes Labels


Kubernetes recommends certain labels such as app.kubernetes.io/name – for the name of the applications, app.kubernetes.io/instance – for the name of the instance. This is different from the application name since you can have multiple instances of the same application. For example, you can have multiple instances of wordpress. /version – for the current version of the application . /component – the part of the architecture e.g. a database. /part-of – the parent of this application. /managed-by – tool used to manage the application e.g. Helm.
These are the labels recommended by Kubernetes.

Kubernetes Labels and annotations

Good practices for Kubernetes labels

There are some good practices around labels. The first is to make sure that labels are used and they follow the same convention throughout the company. The second is to create a list of mandatory labels for any application deployed on Kubernetes. The list can contain labels such as application id, version, stage, release and owner. In addition to the mandatory list we can create a list of optional labels. This should be an exhaustive list so that any team can pick from it.

Kubernetes annotations


Annotations are used to add additional information to the objects. These include information such as build info, user info such as email addresses, monitoring info. for example, Prometheus uses annotations to store configuration information
This post gave an introduction to labels, in the next post we will deploy a set of applications containing various labels and then use selectors to select applications based on those labels.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.