Introduction

Kubernetes uses labels and annotations to attach metadata to various resources and objects in cluster. This metadata comes in handy for things like resource management and usage with external tools. When managing and monitoring your cluster, you will have an easier time using these labels and annotations to figure out things.

What is an object?

An object in Kubernetes is a persistent entity in the Kubernetes system that (collectively) records the desired (intent) and actual states of a Kubernetes cluster. You can think of it as whatever is represented in a .yaml file.

An object will describe a specific concept and it will store a different data structure. So an object will be a kind of a data structure, with a name, that describes different properties and values.

Some examples of kinds of data structures are:

  • Nodes
  • Pods
  • ReplicaSets
  • Custom Resource Definitions.

In Kubernetes, a serialized state of every object is stored in etcd store. Whenever you delete a resource associated with an object, it will remain in the cluster until you manually delete it or have a controller that will clean it up.

What is a resource?

A resource in Kubernetes is an endpoint that stores a collection of API objects. For example, the Pod object collections can be accessed on the /api/v1/pods api endpoint.

What is annotations?

In Kubernetes, annotations are metadata used to express additional information related to a resource or an object.

They are key-value pairs.

When to use annotations?

You can use annotations on any resource or object. You can define them and customized them based on the required needs.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  annotations:
    key1: value1
    key2: value2

Many administrators of Kubernetes cluster use annotations for integration of the cluster to external tools.

You can read how Netflix uses annotations in their container platform Tiger to connect linux kernel panics to Kubernetes pods here.

What are labels?

In Kubernetes, labels are metadata that is used for identifying and grouping resources. They have semantic meaning and you can use labels for querying the Kubernetes API.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: web
    environment: staging

With a pod deployed like above, you can run a query to get you all the pods with app: web in the staging environment.

What is the difference between labels and annotations?

They are both similar in that they are metadata, key-pair values. However, labels are more in tune to be used when you want to have kubernetes handle things for you like scaling.

On the other hand annotations, they are opaque strings with no impact to Kubernetes operations.

Labels are for kubernetes, annotations are for humans.

Conclusion

When it comes to adding metadata to your resources or objects, if you just need to have Kubernetes level control then labels are your best bet.

However, if you need to add external tools or customize experience of managing and monitoring the cluster, then you have annotations to play with.