DEV Community

Yusuf Isah
Yusuf Isah

Posted on • Originally published at yuscode.hashnode.dev

Chapter 5 - Kubernetes Labels and Annotations

Introduction

In Kubernetes, labels and annotations are essential tools for managing and organizing resources. Labels provide a way to attach metadata to objects, which can be used for grouping, filtering, and selecting subsets of resources. Annotations, on the other hand, allow you to attach non-identifying metadata to objects, enabling you to store additional information that can be accessed by external tools or processes.

In this chapter, we'll explore the concepts of labels and annotations, how to apply and modify them, and how they play a vital role in Kubernetes architecture.

Table of Contents

Labels

Labels are key-value pairs that are attached to Kubernetes objects, such as Pods, Nodes, Services, and Deployments. They are used to categorize and select subsets of objects based on criteria you define.

Applying Labels

In this section, we'll create two deployments (a way to create an array of Pods) and attach some labels to them. We’ll take two apps (called egypt and tanzania) and have one environment and one version for each.

First, create the tanzania-staging deployment and set the ver, app, and env labels:

tanzania-staging.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tanzania-staging
  labels:
    app: tanzania
    env: staging
    ver: "1"
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tanzania
      env: staging
      ver: "1"
  template:
    metadata:
      labels:
        app: tanzania
        env: staging
        ver: "1"
    spec:
      containers:
      - name: tanzania-staging-container
        image: dockeryu853/greetings:tanzania
Enter fullscreen mode Exit fullscreen mode

Finally, create the egypt-production deployment.

egypt-production.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: egypt-production
  labels:
    app: egypt
    env: production
    ver: "2"
spec:
  replicas: 1
  selector:
    matchLabels:
      app: egypt
      env: production
      ver: "2"
  template:
    metadata:
      labels:
        app: egypt
        env: production
        ver: "2"
    spec:
      containers:
      - name: egypt-production-container
        image: dockeryu853/greetings:egypt
Enter fullscreen mode Exit fullscreen mode

After applying both manifests, you should have two deployments in your Kubernetes cluster - tanzania-staging and egypt-production.

Image description

To see the labels associated with your deployments, simply run:

kubectl get deployment --show-labels
Enter fullscreen mode Exit fullscreen mode

Image description

Modifying Labels

Labels can be modified using the kubectl label command. Here's how you can add or update a label on an existing object:

kubectl label deployment tanzania-staging env=test --overwrite
Enter fullscreen mode Exit fullscreen mode

This command updates the environment label on the tanzania-staging deployment to test.

You can confirm this update by running the command below:

kubectl get deployment --show-labels
Enter fullscreen mode Exit fullscreen mode

Image description

Label Selectors

Label selectors allow you to filter and select resources based on their labels. They are commonly used in Kubernetes to specify which objects should be targeted by a particular operation.

For example, to list all deployments with the label app=egypt, you can use the kubectl get command with the -l option:

kubectl get deployment -l app=egypt
Enter fullscreen mode Exit fullscreen mode

You can also use the --selector flag to list which resources have a particular label in your Kubernetes cluster. For example, if you want to list only deployments that have the ver label set to 2, you can simply run:

kubectl get deployment --selector="ver=2"
Enter fullscreen mode Exit fullscreen mode

Label Selectors in API Objects

Label selectors are also used in API objects like Services, ReplicaSets, and Deployments to define which Pods they should manage. For example, in the egypt-production.yaml manifest we created, the selector field uses label selectors to target Pods with the app=egypt label.

Labels in Kubernetes Architecture

Labels are integral to the Kubernetes architecture. They are used by the system to determine how to manage resources, such as deciding which Nodes a Pod should run on, or which Pods should receive traffic from a Service.

Annotations

Annotations are another form of metadata that can be attached to Kubernetes objects. Unlike labels, annotations are not used for selection or grouping. Instead, they are used to store non-identifying information that might be needed by tools or processes.

Annotations are key-value pairs, just like labels, but their purpose is different. Here’s an example of applying annotations to a Pod:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  annotations:
    information: "This is my first pod"
    group: "devops"
spec:
  containers:
    - name: my-container
      image: nginx
Enter fullscreen mode Exit fullscreen mode

In this example, the Pod has two annotations: information and group. These annotations provide additional context about the Pod but are not used for selection or filtering.

Annotations are commonly used to store information such as build details, release versions, or URLs for documentation.

Conclusion

In this chapter, we explored the power of labels and annotations in Kubernetes. By understanding how to apply, modify, and utilize these metadata mechanisms, you'll be able to effectively organize and manage your Kubernetes resources. Stay tuned for the next chapter in our Kubernetes series!

Feel free to leave comments and share this article. Follow my blog for more insights on Kubernetes!

Top comments (0)