Labels and Selectors

Labels and Selectors

Labels and Selectors are standard method to group things together. For example, we have set of different species and a user wants to be able to filter them based on different criteria. Such as based on their class or kind or by their color.

Labels and Selectors

And not just group, you want to be able to filter them based on criteria. Such as all blue animals, or with multiple criteria such as everything green that is also a bird. Whatever the classification may be you need ability to prove things together and filter them based on your needs.

The best way to do that with specific Labels. Labels are properties attached to each item. You add properties for each item. So you add properties to each item for their class, kind and color.

Kubernetes Labels

Selectors are the filters of these items. For example, when you say class=Mammal We get a list of Mammals and when you say color is green then we get the green mammal.

We use labels and selectors every where. Such as keywords you tagged in YouTube videos or blogs that have users filter and find the right content. We can see labels in online store that help you add different kinds of filters to view your products.

Now coming to how are labels and selectors used in Kubernetes. We have created a lot of different types of objects in Kubernetes. PODs, Services, ReplicaSet, Deployments etc.

For Kubernetes all of these are different objects. Over time you may deploy 100’s or 1000’s of these objects in your cluster. Then you will need a way to filter and view different objects by different categories. Such as to group objects by their type.

Kubernetes Labels and Selectors
Kubernetes Labels and Selectors

also you can group by their application or group by their functionality. What ever it may be you can group and select objects using labels and selectors.

So how exactly do we specify labels in Kubernetes.

Kubernetes Labels and Selectors 1 1
apiVersion: v1
kind: Pod
  name: simple-webapp
    app: App1
    function: Front-end

    - name: simple-webapp
      image: simple-webapp
        - containerPort: 8080

Once POD is created you can select the POD using the labels by following command.

> kubectl get pods --selector app=App1
NAME                READY   STATUS      RESTARTS   AGE
simple-webapp       1/1     Running        0       24s

Kubernetes objects use labels and selectors internally to connect different objects together. For example, to create a ReplicaSet consisting of three different PODs. We first create label the POD definition and use selector in a ReplicaSet to group the PODs.


  1. The labels defined under templates section are the labels configured on the POD.
  2. Labels defined under top metadata section are the labels of ReplicaSet.
Labels and Selectors
Scroll to top