Kubernetes hands on series: Labels and Selectors

Rakesh Jain
5 min readAug 29, 2020

--

For the purpose of this tutorial, we assume that you have a healthy 3-node Kubernetes cluster already been provisioned. Follow this tutorial to spin up a Production ready 3 Node kubernetes cluster.

Labels and Selectors are standard methods to group things together.

To understand them lets take an example -

Say you have a set of different species.

A user wants to be able to filter them based on different criteria such as based on their class or kind if they are domestic or wild or see by their color and not just group.

You want to be able to filter them based on a criteria such as all green animals or with multiple criteria such as everything green and that is also a bird.

Whatever that classification maybe you need the ability to group things together and filter them based on your needs and the best way to do that is with labels.

Selectors help you to filter these items based on the labels.

Now understand what is Label in Kubernetes and how to apply them -
Kubernetes labels allow DevOps teams to identify Kubernetes objects and organize them into groups. One good use-case of this is grouping pods based on the application they belong to.

Beware of the Syntax!

Make sure that you get the syntax right. Below is an overview of the Kubernetes labeling syntax. There are 3 things you have to consider; label key (label prefix, label name) and label value.

Label Key
Label Prefix
Label prefix is optional
Label prefixes can be no longer than 253 characters
Label prefix must be a DNS subdomain
Label prefix can also be a series of DNS subdomains, separated by “.”
Label prefixes have to end with “/”
Label name Label name is required
Label names can be up to 63 characters
Characters have to be alphanumeric characters
Label names can also include “-”, “_” and “.”
Label names have to begin and end with an alphanumeric character
Label value Label values can be up to 63 characters long
Characters have to be alphanumeric characters
Label values can also include “-”, “_” and “.”
Label values have to begin and end with an alphanumeric character

The first rule of Kubernetes labeling is to actually do it, Always!

So How do you specify labels?
Lets create a pod yaml file and specify the labels we want to attach to that.

https://gist.github.com/TroubleshooteRJ/67f6ad0ca7bc2d0a2fdcf8c53535e984

Once the pod is created, to select the pod with labels run the below command

root@kmaster-rj:~/pod-create# kubectl get pods --selector app=front-endroot@kmaster-rj:~/pod-create# kubectl get pods --selector rel=devNAME              READY   STATUS    RESTARTS   AGE
labels-demo-pod 1/1 Running 0 3m21s
root@kmaster-rj:~/pod-create# kubectl get pods --selector rel=dev,app=front-endNAME READY STATUS RESTARTS AGE
labels-demo-pod 1/1 Running 0 3m24s

There are two types of selectors -

Equality-based -

Equality- or inequality-based selectors allow filtering by label keys and values. Means it will only look for the pods which will have the exact same phrase as that of the label.

Three kinds of operators are admitted =,==,!=. The first two represent equality (and are simply synonyms), while the latter represents inequality. For example:

app = front-end
rel!= dev

Set-based -
This type of selector allows filtering keys according to a set of values. So, in other words, the selector based selector will look for pods whose label has been mentioned in the set.

Three kinds of operators are supported: in,notin and exists (only the key identifier). For example:

app in (front-end, back-end)--> selects all resources with key equal to app and value equal to front-end or back-end
app notin (front-end, back-end) --> selects all resources with key equal to app and values other than front-end and back-end, and all resources with no labels with the app key
partition --> selects all resources including a label with key partition; no values are checked
!partition --> selects all resources without a label with key partition; no values are checked

Some Commands to remember -

Check that the labels are applied in the newly created pod:#kubectl get pod my-pod --show-labelsAdd a label to a pod using Kubectl:#kubectl label pod my-pod app=front-endRemove a label from a pod using Kubectl:#kubectl label pod my-pod app-

Update a label for a pod using Kubectl:
#kubectl label --overwrite pods my-pod app=back-end

We have created different types of objects in Kubernetes such as PODs, ReplicaSets, Deployments etc.

Kubernetes use labels to connect different objects together
ReplicaSet -

here all the pods which matches label (tier: front-end) will be part of ReplicaSet. The label in Pod template and selector section should be same.

selector:
matchLabels:
tier: front-end

Services -

here all the pods which matches label (app: front-end) will be part of the Service myapache-service.

Annotations

While labels and selectors are used to group objects, annotations are used to record other details for informative purpose.

Hope you like the tutorial. Please let me know your feedback in the response section.

Happy Learning!

--

--