15

I wonder if it is possible to change labels of pods on the fly so services route requests to those pods based on new labels.

For example I have two services A and B. Then I have 10 pods, where 5 have label type = A (matches service A) and the other 5 have label type = B (matches service B). At some point I want to change labels on pods to achieve a configuration of 2 with label type = A and 8 with label type = B.

I want to know if I can just change the labels and services will be updated accordingly without having to stop and start new pods with different labels.

dgaviola
  • 2,421
  • 2
  • 26
  • 37

2 Answers2

30

You can change the labels on individual pods using the kubectl label command, documented here.

Changing the label of a running pod should not cause it to be restarted, and services will automatically detect and handle label changes.

So in other words, yes you can :)

Paul DelRe
  • 4,003
  • 1
  • 24
  • 26
Alex Robinson
  • 12,633
  • 2
  • 38
  • 55
  • Is it possible to do it programmatically? – Satyajit Das Feb 13 '20 at 15:19
  • 1
    Yes. You can always use `kubectl` programmatically. Or if you want to do it via the REST API, check out that documentation or look at https://stackoverflow.com/questions/36147137/kubernetes-api-add-label-to-pod – Alex Robinson Feb 18 '20 at 16:40
  • beware that changing the label of a running pod will trigger a rollout restart! – George Cimpoies Mar 28 '22 at 15:48
  • @GeorgeCimpoies It won't if the kubectl label is done directly on the pod. It will trigger a rollout if the pod template is changed, but not the pod. You can also change deployment or statefulset labels without restarts because those labels are not the same as the labels in the podTemplates of those resources. – AFP_555 Aug 09 '22 at 03:28
8

The steps are As follows:

  1. Create two deployments with a label for each and mention number of pods you wish to have In them.

Deploytask1.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: task1deploy
spec:
  replicas: 5
  template:
    metadata:
      labels:
        app: task1deploy
    spec:
      containers:
      - name: nodetask1

Deploy2task1.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: task1deploy2
spec:
  replicas: 5
  template:
    metadata:
      labels:
        app: task1deploy2
    spec:
      containers:
      - name: node2task1
        image: nginx
        ports:
        - containerPort: 80

2.Create two services :

kubectl expose deployment task1deploy --namespace=shifali --type=LoadBalancer --name=my-service
kubectl expose deployment task1deploy2 --namespace=shifali --type=LoadBalancer --name=my-service2

3.When you describe these services you will find 5 endpoints to each(that is the pods):

kubectl describe service my-service  --namespace=shifali

Name: task1deploy

Endpoints: 10.32.0.12:80,10.32.0.7:80,10.32.0.8:80 + 3 more...

And similarly for service2

6.Now remove the label of pod new11 and add label “app=task1deploy2”

kubectl label pods new11-68dfd7d4c8-64xhq  --namespace=shifali app-  
kubectl label pods new11-68dfd7d4c8-64xhq "app=task1deploy2" --namespace=Shifali

Now the services will show variation in the number of target ports (my_service=5 and my_service2=7)

kubectl describe service my-service --namespace=Shifali

Endpoints:10.32.0.7:80,10.32.0.8:80,10.32.1.7:80 + 2 more..

kubectl describe service my-service2 --namespace=Shifali

Name: my-service2 Endpoints: 10.32.0.10:80,10.32.0.12:80,10.32.0.9:80 + 4 more...