How to add label to running pod in Kubernetes

 

Add Label to running pods in Kubernetes - Overview

  • Kubernetes has a very flexible mechanism for connecting and referencing objects that it manages. Rather than having a very strict hierarchy of what can be connected, the Kubernetes project uses short, definitive key/value pairs as set of tags on resources, called labels. There is a matching mechanism to query and find related labels, called Selectors.
  • Labels are fairly strictly defined in format, and are intended to group together resources in Kubernetes. They are not intended to identify a single or unique resource.
  • Kubernetes Labels can be used to describe relevant information about a set of Kubernetes resources, be that a Pod, ReplicaSet, Deployment, and so on.
  • Labels are intended to represent semantic information about a resource, and having multiple labels is not only acceptable, but expected

 

In this article we will learn how to add or remove label from running Pod in Kubernetes. This article assumes that you have knowledge on Kubernetes Labels and Selectors.

 

List existing labels on running Pods

Before we learn to apply labels to running pods, we must know how to check existing labels to the pods. To achieve this you can use --show-labels argument with kubectl get pods command as shown below:

Advertisement
~]# kubectl get pods -n deepak --show-labels
NAME                        READY   STATUS    RESTARTS   AGE    LABELS
test-pod-rsyslog            1/1     Running   0          12d    <none>
test-statefulset-sudo-1-0   1/1     Running   0          5d2h   app=dev,statefulset.kubernetes.io/pod-name=test-statefulset-sudo-1-0
test-statefulset-sudo-1-1   1/1     Running   0          5d2h   app=dev,statefulset.kubernetes.io/pod-name=test-statefulset-sudo-1-1

As you can see here, I have labelled app=dev for two of my pods which were deployed as part of statefulset while the other pod i.e. test-pod-rsyslog has no labels. The app=dev label is used by the statefulset to identify the pods which it has deployed.

 

Method-1: Using kubectl label command

Syntax to apply label to running pod

You can update the labels on any Kubernetes object using the kubectl label command.

kubectl label pods <your-pod-name> <label-name>

 

Example-1: Apply single label to running pod

For example to apply label app=prod to a running test-pod we will use:

kubectl label pods test-pod-rsyslog app=prod

Verify the applied label:

~]# kubectl get pod test-pod-rsyslog -n deepak --show-labels
NAME               READY   STATUS    RESTARTS   AGE   LABELS
test-pod-rsyslog   1/1     Running   0          12d   app=prod

 

Example-2: Apply multiple labels to running pod

We can also apply multiple labels using single command to any running pod using following syntax:

kubectl label pod <pod-name> {key1=value1,key2=value2,key3=value3}

For example, I will add some more label to my pod:

Advertisement
~]# kubectl label pods test-pod-rsyslog -n deepak {color=blue,env=prod1}
pod/test-pod-rsyslog labeled

Verify the labels on your pod:

~]# kubectl get pod test-pod-rsyslog -n deepak --show-labels
NAME               READY   STATUS    RESTARTS   AGE   LABELS
test-pod-rsyslog   1/1     Running   0          12d   app=prod,color=blue,env=prod1

 

Example-3: Overwrite any existing label

By default, kubectl label will not let you overwrite an existing label. To do this, you need to add the --overwrite flag.

For example, we try to add a new value to app key for our pod:

~]# kubectl label pods test-pod-rsyslog -n deepak app=dev
error: 'app' already has a value (prod), and --overwrite is false

As expected the command has failed, as we already have a label app=prod applied to our pod, so we must use --overwrite to apply the new label:

~]# kubectl label pods test-pod-rsyslog -n deepak app=dev --overwrite
pod/test-pod-rsyslog labeled

So we have successfully applied the new label using overwrite.

 

Method-2: Using kubectl edit command

If you feel like making interactive edits instead of editing a local file, you can instead use the kubectl edit command, which will download the latest object state and then launch an editor that contains the definition:

$ kubectl edit <resource-name> <obj-name>

After you save the file, it will be automatically uploaded back to the Kubernetes cluster.

So we will use kubectl edit command to edit any existing resource, in this exercise we will add a label to running Pod.

Advertisement

 

Example: Add or Remove label from running pod

I will only demonstrate the usage of kubectl edit command as the other method is quite straight forward.

 

Create a sample deployment

We will use a deployment for our example with 2 replicas. So these replicas will be our Pod where we will remove and add label while the Pod is in running state.

[root@controller ~]# cat nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    ## This label is applied to the deployment
    type: dev
  name: nginx-deploy
spec:
  replicas: 2
  selector:
     matchLabels:
        ## Search for this label in the pod to map it to the deployment
        app: webserver
  template:
    metadata:
      labels:
        ## Apply this label to the pod
        app: webserver
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
        - containerPort: 80

So in this example our deployment will search for app=webserver label in the pods. Let's go ahead and deploy this deployment:

[root@controller ~]# kubectl create -f nginx-deploy.yaml
deployment.apps/nginx-deploy created

Verify the list of available deployments and their labels:

How to add label to running pod in Kubernetes

 

So our deployment is using type=dev label. Let's check our replicas:

How to add label to running pod in Kubernetes

 

So our pods have the labels app=webserver based on which the deployment will identify it's replicas.

 

Apply labels using kubectl edit command

Now we will use kubectl edit to edit the pod resource and remove the existing label and add a new one i.e type=database:

....
  labels: <-- Look out for this section
    type: database
...

So to modify the label we will edit one of the pods which will open the resource file in YAML format using your default editor. After doing your changes you can save the file.

Advertisement
[root@controller ~]# kubectl edit pod nginx-deploy-5cc69bf9c4-6dsjx
pod/nginx-deploy-5cc69bf9c4-6dsjx edited

Now you can see we have a new pod starting UP:

~]# kubectl get pods --show-labels
NAME                            READY   STATUS              RESTARTS   AGE   LABELS
nginx-deploy-5cc69bf9c4-6dsjx   1/1     Running             0          21m   type=database,pod-template-hash=5cc69bf9c4
nginx-deploy-5cc69bf9c4-j99ns   1/1     Running             0          17m   app=webserver,pod-template-hash=5cc69bf9c4
nginx-deploy-5cc69bf9c4-slsf8   0/1     ContainerCreating   0          38s   app=webserver,pod-template-hash=5cc69bf9c4

This is due to the fact that we modified the labels of one of the existing replicas so deployment assumed that one of the pod has crashed or gone missing or whatever. Due to this the deployment started a new Pod replica.

Let's re-add the label app=webserver to this pod again using kubectl edit command:

[root@controller ~]# kubectl edit pod nginx-deploy-5cc69bf9c4-6dsjx
...
  labels: <-- Look out for this section
    pod-template-hash: 5cc69bf9c4
    type: database
    app: webserver
...

So now our pod has two labels, let's see the impact of this change:

[root@controller ~]# kubectl get pods --show-labels
NAME                            READY   STATUS        RESTARTS   AGE     LABELS
nginx-deploy-5cc69bf9c4-6dsjx   1/1     Running       0          28m     app=webserver,pod-template-hash=5cc69bf9c4,type=database
nginx-deploy-5cc69bf9c4-j99ns   1/1     Running       0          24m     app=webserver,pod-template-hash=5cc69bf9c4
nginx-deploy-5cc69bf9c4-slsf8   0/1     Terminating   0          7m32s   app=webserver,pod-template-hash=5cc69bf9c4

As you can see, since the deployment was able to locate it's replica with the matching label. As we had only asked for 2 replicas in the deployment, it is now terminating the last pod which was created.

 

How to delete label for Kubernetes Pods

Use the following syntax to remove a label from a node:

kubectl label pods <your-pod-name> <label-name>-

For example I will remove app label from my pod:

~]# kubectl label pods test-pod-rsyslog -n deepak app-
pod/test-pod-rsyslog labeled

Verify the same:

~]# kubectl get pod test-pod-rsyslog -n deepak --show-labels
NAME               READY   STATUS    RESTARTS   AGE   LABELS
test-pod-rsyslog   1/1     Running   0          12d   color=blue,env=prod1

 

Summary

In this tutorial we learned how to add or remove or modify a label from running Pod. There are different methods to manipulate labels in a Pod. If you are create a pod then you can easily assign your labels in the YAML file but for an existing pod you must either edit the object YAML file or use kubectl label command. I also gave one example to demonstrate the behaviour and impacts of adding or removing labels runtime to an existing Pod.

 

Further Reading

Kubernetes Recommended Labels
Kubernetes Labels and Selectors

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment