FROMDEV

Reasons You Should Use Kubernetes Labels and Annotations

Reasons You Should Use Kubernetes Labels and Annotations

Are you exploring Kubernetes or considering using it? If yes, then you will be thankful you landed on this article about a little pondered but crucial component of Kubernetes: labels and annotations. As we know, Kubernetes is an orchestration tool that is usually used to manage containerized applications.

In this article, we will be understanding labels and annotations are so important for managing containerized applications.

Introduction to Labels and Annotations

Labels are used in configuration files to specify attributes of objects that are meaningful and relevant to the user, especially in the grouping, viewing, and performing operations. In addition, labels can be used in the specs and metadata sections.

Annotations, on the other hand, help provide a place to store non-identifying metadata, which may be used to elaborate on the context of an object.

The following are some Kubernetes labels:

  • name: Name of the application
  • instance: unique name of the instance
  • version: semantic version number
  • component: the component within your logical architecture
  • part-of: the name of the higher-level application this object is a part of.
  • managed-by: the person who manages it.

Note: Labels and selectors work together to identify groups of relevant resources. This procedure must be efficient because selectors are used to querying labels. Labels are always restricted by RFC 1123 to ensure efficient queries. Labels are limited to a maximum of 63 characters by RFC 1123, among other restrictions. When you want Kubernetes to group a set of relevant resources, you should use labels.

Labels

Let’s dive in a little further. Labels are value pairs – like pods – that are attached to objects. They’re used to specify identifying attributes of objects which are relevant to users. They do not affect the semantics of the core system in any way – labels are just used to organize subsets of objects.

Here, I have created labels using kubectl:

Now, the question that arises is this: Why should we use labels in Kubernetes? This is the question most people ask, and so is one of the two main questions of this article.

The main benefits of using labels in Kubernetes include help in organizing Kubernetes workloads in the clusters, mapping our own organizational structures into system objects, selecting or querying a specific item, grouping objects and accessing them when required, and finally enabling us to select anything we want and execute it in kubectl.

You can use labels and annotations to attach metadata to objects – labels, in particular, can be used to select objects and find objects or collections of objects that satisfy certain criteria. Annotations, however, are not used for identification. Let’s look at annotations a bit more.

Annotations

Now, let us turn to the other question this article wishes to address: Why do we use annotations in Kubernetes?

To answer this, let us first look at this photo:

As you can see, I have created an image registry and specified the link. Now, in case of anything changes, I can track the changes in the URL specified. This is how we use annotations. One of the most frequently used examples for explaining the need for annotations is comparing it to storing phone numbers.

The main benefits of using annotations in Kubernetes include helping us in elaborating the context at hand, the ability to track changes, communicating to the scheduler in Kubernetes about the scheduling policy, and keeping track of the replicates we have created. Kubernetes scheduling is the process of assigning pods to matched nodes in a cluster. The scheduler watches for newly created pods and assigns them to the best possible node based on scheduling principles and configuration options.

In addition to that, annotations help us in deployment so that we can track replica sets, they help DevOps teams provide context and keep useful context, and they provide uniqueness, unlike labels.

Some examples of information that can be recorded by annotations are fields; build, release, or image information; pointers to logging; client libraries and tools; user or system provenance information; tool metadata; and directives from end-uses to implementations.

Conclusion

In this article, we first went through the basic concept of what labels and annotations are. Then, we used kubectl, the most powerful and easy-to-use command-line tool for Kubernetes. Kubectl helps us query data in our Kubernetes cluster.

As you can see, the use of labels and annotations in Kubernetes plays a key role in deployment. They not only help us add more information about our configuration files but also help other teams, especially the DevOps team, understand more about your files and their use in managing these applications.

Thanks for reading. Let’s keep building stuff together and learn a whole lot more every day! Stay tuned for more on Kubernetes, and happy learning!

Exit mobile version