Label deployment pod templates

5/2/2018

Is there any way to add labels in .spec.template after a deployment has been created? So, I know this can be done

kubectl label deployment myDeployment myLabelKey=myLabelValue

But this would only add the label to .metadata.labels. I would like to add a label to .spec.template.metadata.labels.

-- bitscuit
kubernetes

2 Answers

12/21/2018

the solution by @helmbert is correct but it's missing double quotes after myLabelValue.

$ kubectl patch deployment myDeployment --patch '{"spec": {"template": {"metadata": {"labels": {"myLabelKey": "myLabelValue"}}}}}'
-- Chris
Source: StackOverflow

5/2/2018

This should be possible using the kubectl patch command. The following patch file would add a new label to the spec.template.metadata.labels property:

spec:
  template:
    metadata:
      labels:
        myLabelKey: myLabelValue

Then apply with:

$ kubectl patch deployment myDeployment --patch "$(cat patchfile.yaml)" 

Alternatively, with inline JSON instead of a separate file:

$ kubectl patch deployment myDeployment --patch '{"spec": {"template": {"metadata": {"labels": {"myLabelKey": "myLabelValue}}}}}'
-- helmbert
Source: StackOverflow