Kubernetes and Cloud Native Associate - KCNA

Scheduling

Labels and Selectors

Labels and selectors provide a standard method to group and filter items based on various criteria. Think of it like sorting species by attributes such as class (mammal, bird, etc.), domestication status, or color. Whether you want to list all green animals or specifically all green birds, labels let you attach key-value pairs to each item, while selectors help retrieve items that meet your criteria.

This concept is applied widely—from tagging YouTube videos or blog posts, to using filters that sort products in an online store.

In Kubernetes, labels and selectors are essential for managing a cluster filled with objects such as Pods, Services, ReplicaSets, and Deployments. Labels help organize these resources by applying characteristics like application type or functionality, allowing you to filter and group them even when dealing with hundreds or thousands of objects. Selectors then query these objects based on the criteria specified by the labels.

The image illustrates "Labels & Selectors in Kubernetes," showing various labeled components like Front-End, Web-Servers, and DB, represented by different colored shapes.

For every Kubernetes object, you attach labels as needed (for example, app or function) and then use selectors to filter the objects. For instance, if you want to filter objects where the label app equals App1, your selector would look like this:

app = App1

Specifying Labels in a Pod Definition

In a Pod definition file, labels are defined under the metadata section. Below is an example of how to add labels in a key-value format:

apiVersion: v1
kind: Pod
metadata:
  name: simple-webapp
  labels:
    app: App1
    function: Front-end
spec:
  containers:
    - name: simple-webapp
      image: simple-webapp
      ports:
        - containerPort: 8080

After creating the Pod, you can run the following command with a selector to filter for Pods with a specific label, such as app=App1:

kubectl get pods --selector app=App1

Console output:

NAME             READY   STATUS      RESTARTS   AGE
simple-webapp    0/1     Completed   0          1d

Using Labels and Selectors with ReplicaSets

When working with ReplicaSets to manage multiple Pods, you label the Pod definition and then specify a selector in the ReplicaSet to group the desired Pods. It’s crucial that the selector in the ReplicaSet specification matches the labels on the Pods exactly.

Note

Only the labels on the Pod template are used for selection; labels on the ReplicaSet itself are not considered.

Below is a correct ReplicaSet definition that connects the ReplicaSet to its Pods by matching labels:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: simple-webapp
  labels:
    app: App1
    function: Front-end
spec:
  replicas: 3
  selector:
    matchLabels:
      app: App1
  template:
    metadata:
      labels:
        app: App1
        function: Front-end
    spec:
      containers:
      - name: simple-webapp
        image: simple-webapp

A single matching label is sufficient. However, if there is a chance that other Pods might share a common label, you should include additional labels in the selector to guarantee that only the intended Pods are selected.

This principle also applies to other Kubernetes objects like Services. When a Service is created, its selector—defined in the Service specification—matches the labels on the Pods (or ReplicaSets) that it targets.

Annotations in Kubernetes

While labels and selectors are used for grouping and filtering, annotations offer a way to attach non-identifying metadata to objects for informational purposes. Annotations might include details about the build version, contact information, or other integration data.

For example, consider the following ReplicaSet definition that includes an annotation:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: simple-webapp
  labels:
    app: App1
    function: Front-end
  annotations:
    buildversion: "1.34"
spec:
  replicas: 3
  selector:
    matchLabels:
      app: App1
  template:
    metadata:
      labels:
        app: App1
        function: Front-end
    spec:
      containers:
      - name: simple-webapp
        image: simple-webapp

Annotations are ideal for adding tool details, version information, or contact data without impacting how the system groups or selects objects.

Summary

Through the careful use of labels, selectors, and annotations, Kubernetes offers a powerful framework to manage and organize vast numbers of objects effectively. This flexible mechanism improves resource management and simplifies querying, ensuring smooth operations in complex environments.

For more detailed information, consider exploring:

Helpful Tip

Using specific labels and selectors efficiently can significantly streamline your Kubernetes resource management. Experiment with various configurations to understand the best practices for your deployment needs.

Watch Video

Watch video content

Previous
Manual Scheduling