Skip to main content

Command Palette

Search for a command to run...

Labels & Selectors

Published
4 min read
Labels & Selectors
D

Passionate about technology and its endless possibilities, I am a BCA graduate who embarked on a journey to explore the world of IT. With a strong foundation in Linux and a keen interest in cloud computing, I delved into the realms of Docker, Podman, and AWS, honing my skills along the way.

As a constant learner, I am currently immersing myself in the intricacies of Kubernetes, eager to unlock its full potential for scalable and efficient application deployment. My goal is to leverage this powerful container orchestration platform to streamline DevOps processes and drive seamless digital transformations.

With a solid understanding of system administration, containerization, and cloud infrastructure, I am equipped to tackle complex challenges and deliver innovative solutions. I thrive in collaborative environments, leveraging my communication skills to effectively bridge the gap between technical complexities and business objectives.

Driven by curiosity and fueled by a growth mindset, I continuously seek out new technologies and industry trends. I am excited to connect with like-minded professionals, exchange knowledge, and contribute to the ever-evolving tech landscape.

If you're looking for a dedicated and adaptable IT professional who can navigate the dynamic world of containers, cloud, and beyond, let's connect and embark on a transformative journey together.

In Kubernetes, labels and selectors are key concepts used to identify and organize objects within the cluster. Labels are key-value pairs attached to Kubernetes objects, such as pods, services, or deployments, allowing you to categorize and group them based on your requirements. Selectors, on the other hand, are used to query and filter objects based on their labels.

Here's a breakdown of labels and selectors in Kubernetes:

Labels:

  • Labels are arbitrary metadata that you can attach to Kubernetes objects.

  • They consist of key-value pairs, where both the key and value are strings.

  • Labels are typically used to describe the characteristics or properties of objects.

  • You can assign multiple labels to an object, allowing for flexible grouping and categorization.

  • Labels are not unique across objects and can be shared among different resources.

Selectors:

  • Selectors are used to query and filter objects based on their labels.

  • They allow you to define rules to select a specific set of objects that match the label criteria.

  • Selectors use label-matching expressions to specify the criteria for object selection.

  • There are two types of selectors: equality-based selectors and set-based selectors.

  • Equality-based selectors match labels based on exact key-value matches.

  • Set-based selectors support more advanced label-matching operations, such as set membership and set intersection.

  • Selectors are commonly used in various Kubernetes features, such as service discovery, deployments, and replica sets.


Prerequisite:

Created three deployments with different labels as follows:

  1. Deployment one is named mydep1 with labels as class=unnati and course=cka.

  2. Deployment two is named mydep2 with labels as class=cidco and course=cca.

  3. Deployment three is named mydep3 with labels as class=darga course=rhcsa.

  • Yaml files for all deployments:
#Yaml file for deployment one (mydep1)

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    class: unnati
    course: cka
  name: mydep1
spec:
  replicas: 6
  selector:
    matchLabels:  
      class: unnati 
      course: cka
  strategy: {}
  template:
    metadata:
      labels:
        class: unnati
        course: cka
    spec:
      containers:
      - image: docker.io/httpd
        name: httpd
        resources: {}
status: {}

#Yaml file deployment two (mydep2)

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    class: cidco
    course: cca
  name: mydep2
spec:
  replicas: 5
  selector:
    matchLabels:
      class: cidco
      course: cca
  strategy: {}
  template:
    metadata:
      labels:
        class: cidco
        course: cca
    spec:
      containers:
      - image: docker.io/httpd
        name: httpd
        resources: {}
status: {}

#Yaml file for deployment three (mydep3)

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    class: darga
    course: rhcsa
  name: mydep3
spec:
  replicas: 4
  selector:
    matchLabels:
      class: darga
      course: rhcsa
  strategy: {}
  template:
    metadata:
      labels:
        class: darga
        course: rhcsa
    spec:
      containers:
      - image: docker.io/httpd
        name: httpd
        resources: {}
status: {}

Equity-based selectors & Set-based selectors:-

Equity-based selectors:

  • Equality-based selectors match labels based on exact key-value matches.

  • Three kinds of operators are admitted =,==,!= in equity-based selectors.

  • With equality-based selectors, you can select resources that have labels matching the specified key-value pairs.

  • Equality-based selectors support matching multiple labels using comma-separated key-value pairs.

  • An example of an equality-based selector is match-labels, which is commonly used in Kubernetes resource definitions.

  • When using an equality-based selector, all specified labels must match exactly for a resource to be selected.

How to use an equity-based selector

Total pods running in a cluster with labels

  • Example of '=' operator: Used to show specific key-value

  • Example of '==' operator: Used to show specific key-value

  • Example of '!=' operator: Used to show the key-values leaving that are mentioned


Set-based selectors:

  • Set-based selectors provide more advanced label-matching operations, such as set membership and set intersection.

  • They use set-based operators (IN, NOTIN, EXISTS and DOSENOTEXISTS) to specify the label-matching criteria.

  • Set-based selectors allow for more complex label-matching scenarios compared to equality-based selectors.

  • They are useful when you need to match resources based on labels that satisfy certain conditions or combinations.

  • Set-based selectors can match labels across multiple key-value pairs.

  • An example of a set-based selector is matchExpressions, which allows you to define complex label-matching expressions using operators like IN, NOTIN, EXISTS and DOSENOTEXISTS

How to use set-based selectors:

Total pods running in a cluster with labels

  • Example of IN: Used to get the multiple values of the same key.

  • Example for NOTIN: Used to get the values leaving the values mentioned in the key.

  • Example of EXITS(key): Used to get all the values of one key.

  • Example of DOESNOTEXISTS(!key): Used to get value by leaving the key mentioned. To use this operator we have to first disable the bash=hashing or else it will give the error. To disable the bash-hashing use command "set +H"