concept label in category kubernetes

appears as: labels, label, labels, label, A label, Labels
Learn Kubernetes in a Month of Lunches

This is an excerpt from Manning's book Learn Kubernetes in a Month of Lunches.

How the deployment keeps track of its resources does matter though, because it's a pattern which Kubernetes uses a lot. Any Kubernetes resource can have labels applied which are simple key-value pairs. You can add labels to record your own data - you might add a label to a deployment with the name release and the value 20.04 to indicate this deployment is from the 20.04 release cycle. Kubernetes also uses labels to loosely couple resources, mapping the relationship between objects like a deployment and its pods.

Try it now The deployment adds labels to pods it manages. Use Kubectl to print out the labels the deployment adds, and then list the pods which match that label:

# print the labels which the deployment adds to the pod:
kubectl get deploy hello-kiamol-2 -o jsonpath='{.spec.template.metadata.labels}'
 
# list pods which have that matching label:
kubectl get pods -l app=hello-kiamol-2

My output is in figure 2.8, where you can see some internals of how the resources are configured. Deployments use a template to create pods, and part of that template is a metadata field which includes the labels for the pod(s). In this case the deployment adds a label called app with the value hello-kiamol-2 to the pod. Querying pods which have a matching label returns the single pod managed by the deployment.

Figure 2.8 Deployments add labels when they create pods, and you can use those labels as filters

Using labels to identify the relationship between resources is such a core pattern in Kubernetes that it's worth a diagram to make sure it’s clear. Resources can have labels applied at creation, and then added, removed or edited during their lifetime. Controllers use a label selector to identify the resources they manage - and that can be a simple query matching resources with a particular label, as you see in figure 2.9.

Figure 2.9 Controllers identify the resources they manage using labels and selectors

This is very flexible because it means controllers don't need to maintain a list of all the resources they manage, the label selector is part of the controller specification and controllers can find matching resources at any time by querying the Kubernetes API. It's also something you need to be careful with, because you can edit the labels for a resource and end up breaking the relationship between it and its controller.

Kubernetes in Action

This is an excerpt from Manning's book Kubernetes in Action.

3.3.2. Specifying labels when creating a pod

Now, you’ll see labels in action by creating a new pod with two labels. Create a new file called kubia-manual-with-labels.yaml with the contents of the following listing.

Listing 3.3. A pod with labels: kubia-manual-with-labels.yaml
apiVersion: v1
kind: Pod
metadata:
  name: kubia-manual-v2
  labels:
    creation_method: manual          #1

    env: prod                        #1

spec:
  containers:
  - image: luksa/kubia
    name: kubia
    ports:
    - containerPort: 8080
      protocol: TCP

You’ve included the labels creation_method=manual and env=prod in the metadata.labels section. You’ll create this pod now:

3.3.1. Introducing labels

Labels are a simple, yet incredibly powerful, Kubernetes feature for organizing not only pods, but all other Kubernetes resources. A label is an arbitrary key-value pair you attach to a resource, which is then utilized when selecting resources using label selectors (resources are filtered based on whether they include the label specified in the selector). A resource can have more than one label, as long as the keys of those labels are unique within that resource. You usually attach labels to resources when you create them, but you can also add additional labels or even modify the values of existing labels later without having to recreate the resource.

Let’s turn back to the microservices example from figure 3.6. By adding labels to those pods, you get a much-better-organized system that everyone can easily make sense of. Each pod is labeled with two labels: