Shubham Agarwal
2 min readOct 17, 2021

Labels and Selectors in Kubernetes for absolute beginners

Labels and Selectors are the standard method to group things together in Kubernetes. We can filter the objects based on the criteria like class, kind, and functions.

  • Labels are the properties attached to each item/object.
  • Selector helps us to filter the items/objects which have labels attached to them.

We have n number of different type of objects in Kubernetes, like Pods, ReplicaSet, Deployments, Services, etc. By time when our infrastructure is increasing we will have 1000s of objects in our cluster.

And their are situations when you need a way to filter and view different objects by different categories. Such as group objects by their types or by application or by their functionality whatever it may be.

In order to work with Labels and Selectors all you need to do is to attach labels as per your requirement in key-value format for each object.

In a pod-definiation file under metdata, create a section call labels. Under that add the labels in a key-value format like:
app: app1
type: frontend

Then while selecting, specify a condition to filter specific objects, i.e type=frontend.

We can add as many labels as we want to a Pod. Once the Pod is created, to select the pod with labels use the “kubectl get pods” command along with the selector option, and specify the condition like:

kubectl get pods --selector type=frontend

So this is the one use case of labels and selectors. Kubernetes objects use labels and selectors internally also to work with differnet objects together.

Let’s create a Replicaset consisting of 3 different objects, we first label the pod definiation and use selector in a Replicaset to group the pods

apiVersion: v1
kind: ReplicaSet
metadata:
name: simple-webapp
labels: ###labels we see here are the labels for replicaSet###
app: app1
function: front-end
annotations:
buildversion: 1.4
spec:
replicas: 3
selector:
matchLabels:
app: App1 ####match label with the labels of pod###
template:
metadata:
labels: ###labels defined under template section are the labels configure on the pods###
app: app1
function: front-end
spec:
containers:
- name: simple-webapp
image: simple-webapp

In the replicaset definiation file, we can see labels defined in the 2 places. The labels on the Replicaset will be used if were configure some other objects to discover the ReplicaSet.

In order to connect the Replicaset to the Pod, we configured the selector field under the Replicaset specification to match the labels defines on the Pod.

A single label on the pod can work with Replicaset if its matches correctly. However if there are other pods with same labels, but with a different function then, we can specify all the labels to ensure the right pods discovered by the ReplicaSet.

It works same for other objects like a service, when a service is created it uses the selector defined in the service definiation file to match the lables set on the pods in the replicaSet definiation file.

apiVersion: v1
kind: Service
metadata:
name: frontendservice
spec:
selector:
app: app1 ###match selector with above pod label
ports:
- protocol: TCP
port: 80
targetport: 9376

Thanks for reading!!! Please share & subscribe :)

Shubham Agarwal
Shubham Agarwal

Written by Shubham Agarwal

Site Reliability Engineer, have 5 years of experience in IT support and Operations

No responses yet