Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Label deployment pod templates

Tags:

kubernetes

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.

like image 737
bitscuit Avatar asked Apr 25 '26 02:04

bitscuit


1 Answers

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"}}}}}'
like image 161
helmbert Avatar answered Apr 27 '26 16:04

helmbert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!