<p>Pretty much my question is in the title. I would like to know if there is a way to apply an annotation while creating an object in Kubernetes, through kubectl.</p>

<p>I'll put you more in the context. I am creating an internal load balancer on GKE. To make a load balancer internal, on GKE you would add the following annotation to the service: <code>cloud.google.com/load-balancer-type: "Internal"</code>.</p>

<p>But when exposing the deployment it gives me the option to make it LoadBalancer type only:</p>

<p><code>$ kubectl expose deploy nginx --port 80 --type LoadBalancer</code></p>

<p>Is there a way to apply the annotation as well on the fly, while creating the service?</p>

<p><strong>EDIT</strong></p>

<p>I tried the following command, that seems to be neat:</p>

<pre><code>kubectl expose deploy nginx --port 80 --type LoadBalancer --overrides '{ "metadata": { "annotations": { "cloud.google.com/load-balancer-type": "Internal" } } }'
</code></pre>

<p>... but I am getting the follwoing error, that I don't know how to interpret:</p>

<pre><code>error: unable to convert the internal object type *core.Service to Unstructured without providing a preferred version to convert to.
</code></pre>

<p>This is where is comes from: <a href="https://github.com/kubernetes/kubernetes/blob/d1c713f3bc0aad7b602127b5b53ce3aa7a7ae58a/staging/src/k8s.io/apimachinery/pkg/conversion/unstructured/unstructured_conversion_test.go#L47" rel="nofollow noreferrer">source</a></p>

suren
<p>I'm not exactly sure if this is what you are looking for but it works and it's one-liner</p>

<pre><code>kubectl expose deploy nginx --port 80 --type LoadBalancer -oyaml --dry-run &gt; file; sed -i 's/creationTimestamp: null/annotations:\n    cloud.google.comload-balancer-type: "Internal"/g' file
</code></pre>

<p>I'm aware this is not the nicest and cleanest way.</p>

<p><strong>Edit:</strong></p>

<p>To put a bit more details into the command.</p>

<p>It's exposing a deployment <code>nginx</code> with port and type as you specified. <code>-oyaml</code> is output in yaml format and <code>--dry-run</code> is only printing without executing.</p>

<p>Then <code>sed</code> is replacing <code>creationTimestamp: null</code> with <code>annotations</code> and in new line adding <code>cloud.google.com/load-balancer-type: "Internal"</code>.</p>
