Updated April 15, 2023
What is Kubernetes Labels?
As the name suggests labels are used to provide identify the objects, it is representing in the form of key and value pairs. This key-value pair is attached to the Kubernetes object that is pods, labels help us to identify the attribute of the object. This is meaningful and relevant information to the user as well. It is used to provide the meaning information to the user but not to the core system. We can attach labels to the object at the time of creation we can easily modify them at any point in time if required. Each of the objects we have should have the key-value pair defined with it and every key of the given object should be unique in order to identify it correctly. In the coming section of the tutorial, we will have closer look at the implementation and syntax in order to get a better idea about the labels in detail for better understanding and clarity.
Kubernetes Labels Requirements
Before creating labels, we have few syntaxes which needs to be followed, also labels help us to map our organization or company structure to the system objects, which will be loosely coupled. These mapping client do not require to store at their end. Before starting creatin of the labels we should keep in mind few things which are described as below;
1) labels should always in the form of key-value pair.
2) key of the labels consists of two segments, which is represent by optional prefix and name, it should be separated by the (/) slash.
3) The name segment is consisting of 63 character or can be less, it is required to have.
4) the name segment must be end and begin with the alpha numeric character it should separate by the (-) dash, dots (.) and (_) underscore.
5) the prefix segment is optional
So the valid label of the Kubernetes should have the below points in mind see below;
1) name segment should be 63-character long.
2) it cannot be empty
3) prefix is optional.
4) it should follow this regex while creating the label, ‘[a-z0-9A-Z]’ must be separated with the dot, underscore, and dash.
Below is the syntax by which we can create the labels in Kubernetes see below for reference;
Syntax:
"metadata": {
"labels": {
"key1" : "value1",
"key2" : "value2",
"key3" : "value3",
"key4" : "value4",
"key5" : "value5"
}
}
Methods
In order to recognize the resources with the help of labels, Kubernetes provides us two different methods by which we can do this. One of them is via labels, and other one is via annotations. In this section we will going to discuss the different methods we have, let’s discuss each of them in detail see below;
1) via labels
2) via annotations
Let’s taken a look at each of them in detail see below;
- via labels: as we know that labels are the key-value pair which issued to represent the Kubernetes objects, basically it helps us to identifying the metadata that is neigh attached to the Kubernetes objects, they provide relevant and meaning information about the object. In Kubernetes, we have support by which we can querying objects based on the label we have also we can have apply bulk of operations on that. So they are often used as the information which we want to share within the team, because it provides us the metadata.
- via annotations: we have one more method that help us to identify the metadata of the objects it is done via the annotations. on the place of labels, we can use ‘annotations’. annotation is also a key value-based structure that helps us to identify the metadata attach to the object. but we cannot use annotations to querying the object based on the annotations.
let’s take a closer look at the syntax for both methods in detail for better understanding and clarity see below;
method one via labels :
Syntax:
"metadata": {
"labels": {
"key1" : "value1",
"key2" : "value2",
"key3" : "value3",
"key4" : "value4",
"key5" : "value5"
}
}
method two via annotations:
Syntax:
"metadata": {
"annotations": {
"key1" : "value1",
"key2" : "value2",
"key3" : "value3",
"key4" : "value4",
"key5" : "value5"
}
}
Web Applications Kubernetes labels & Example
In this section, we will have closer look at the web application-specific labels which will also include the example to understand it better, so let’s get started see below;
1) app.kubernetes.io/version: this label is used to represent the version of the application we can say current version.
2) app.kubernetes.io/instance: this label is used to uniquely identified the instance of the application.
3) app.kubernetes.io/name: by the help of this label we can easily defined the name of our application should be always on the top.
4) app.kubernetes.io/created-by: this label is used to identified or defined the name owner who has created the resource or the owner of the controller/Resource.
5) app.kubernetes.io/part-of: this label is sued to defined the name of the higher application which this application is the part.
6) app.kubernetes.io/managed-by: this label is used to defined the tool, this tool is something which manages the operation of the application for us.
7) app.kubernetes.io/component: this is also one important label used, which helps us to identified the component which is used in the application.
now let’s take a closer look at the example to use this labels properly in application;
Examples
apiVersion: your_api_version
kind: StatefulSet
metadata:
labels:
app.kubernetes.io/name: mysql
app.kubernetes.io/instance: mysql-abcxzy
app.kubernetes.io/version: "5.7.21"
app.kubernetes.io/component: database
app.kubernetes.io/part-of: wordpress
app.kubernetes.io/managed-by: helm
app.kubernetes.io/created-by: controller-manager
Conclusion
Kubernetes labels help us to identify the attribute of an object, because they can easily be created for the Kubernetes objects. Also, it follows the key-value pair pattern, which we have already discussed. Go through the full document to get a better understanding of the topic in detail also it will give your idea about the syntax and restriction for /,.the labels.
Recommended Articles
We hope that this EDUCBA information on “Kubernetes Labels” was beneficial to you. You can view EDUCBA’s recommended articles for more information.