How to add label to running pod in Kubernetes

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.

 

Method-1: Using kubectl label command

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

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

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

 

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

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

 

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.

 

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.

Advertisement

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. 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.

[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.

Advertisement

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.

 

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