<p>I have multiple kubernetes clusters and want to ensure that when I <code>kubectl apply</code> a deployment, I'm targeting the correct cluster.</p>
<p>I have all my clusters configured in contexts in the root <code>/.kube/config</code> file but I don't want to rely on statefully switching my current context to the correct one before running each <code>apply</code> command.</p>
<p>i.e. This is not satisfactory</p>
<pre><code>kubectl config use-context cluster-1-context
kubectl apply ./deploy-to-cluster-1.yml

kubectl config use-context cluster-2-context
kubectl apply ./deploy-to-cluster-2.yml
</code></pre>
<p>I read the docs on <a href="https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/" rel="noreferrer">config for multiple clusters</a> and the only way I can find to do this is by copy/pasting the config for a particular cluster into a custom config file and specifying that with the <code>--kubeconfig</code> option on the <code>apply</code> command.</p>
<pre><code>kubectl apply ./deploy-to-cluster-1.yml --kubeconfig ./config-cluster-1
kubectl apply ./deploy-to-cluster-2.yml --kubeconfig ./config-cluster-2
</code></pre>
<p><strong>This works</strong>, but it seems really cumbersome.</p>
<p>For such a common requirement I'd expect there to just be a simple option on <code>apply</code>, or perhaps even a field in the deployment <code>yml</code>, that lets you specify (or restrict) the deployment to a particular context/cluster name, but I've read through a lot of the relevant documentation and can't find any such option.</p>
<p><strong>Is there a better way to do this?</strong></p>

<p>There seems to be <code>--context=...</code> option.</p>
<pre><code>kubectl options

The following options can be passed to any command:
...
      --context='': The name of the kubeconfig context to use
</code></pre>
<p>at least in version <code>v1.18.6</code></p>
