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

<p><code>kubectl label deployment myDeployment myLabelKey=myLabelValue</code></p>

<p>But this would only add the label to <code>.metadata.labels</code>. I would like to add a label to <code>.spec.template.metadata.labels</code>.</p>

bitscuit
<p>This should be possible using the <a href="https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/" rel="nofollow noreferrer"><code>kubectl patch</code></a> command. The following patch file would add a new label to the <code>spec.template.metadata.labels</code> property:</p>
<pre><code>spec:
  template:
    metadata:
      labels:
        myLabelKey: myLabelValue
</code></pre>
<p>Then apply with:</p>
<pre><code>$ kubectl patch deployment myDeployment --patch &quot;$(cat patchfile.yaml)&quot; 
</code></pre>
<p>Alternatively, with inline JSON instead of a separate file:</p>
<pre><code>$ kubectl patch deployment myDeployment --patch '{&quot;spec&quot;: {&quot;template&quot;: {&quot;metadata&quot;: {&quot;labels&quot;: {&quot;myLabelKey&quot;: &quot;myLabelValue&quot;}}}}}'
</code></pre>
