<p>In this case, I have multiple Kubernetes clusters and want to work on different clusters at the same time. (Will keep it as 2 clusters to make it simple)</p>
<p>As described in <a href="https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/" rel="nofollow noreferrer">Kubernetes documentation</a> I have configured two clusters (will call them dc1-main and dc2-main)</p>
<p>I'm logging into a node where kubectl is, with an application support user (e.g. appuser)</p>
<p>At the same time on two sessions to the management server I logged in with appuser.</p>
<p>In this case, I want to use kubectl to manage one context on each session.</p>
<p>But, if I set the active context as below, both sessions to the server reflect get the change as both are referring to the the same config file (which has both contexts)</p>
<pre><code>kubectl config use-context dc1-main
</code></pre>
<p>Or the other option in the document is to pass the context with the command as an argument. Which makes the command quite complicated.</p>
<pre><code>kubectl --context=&quot;dc2-main&quot; get nodes
</code></pre>
<p>I'm looking at an easy way to change this quickly to change the context without affecting the other session. Which could be most likely an environment variable. Not so sure if this is the easiest though.</p>
<p>I went through the kubectl project GitHub and found a change has been requested long time ago for something similar to this and talking about env variables.</p>
<p>Any better suggestions?</p>

<p>The standard Kubernetes client libraries support a <a href="https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#the-kubeconfig-environment-variable" rel="noreferrer"><code>$KUBECONFIG</code> environment variable</a>.  This means that pretty much every tool supports it, including Helm and any locally-built tools you have.  You can set this to a path to a cluster-specific configuration.  Since it's an environment variable, each shell will have its own copy of it.</p>
<pre class="lang-bash prettyprint-override"><code>export KUBECONFIG=&quot;$HOME/.kube/dc1-main.config&quot;
kubectl get nodes
</code></pre>
<p>In your shell dotfiles, you can write a simple shell function to set this</p>
<pre class="lang-bash prettyprint-override"><code>kubecfg() {
  export KUBECONFIG=&quot;$HOME/.kube/$1.config&quot;
}
</code></pre>
<p>In my use I only have one context (user/host/credentials) in each kubeconfig file, so I pretty much never use the <code>kubectl config</code> family of commands.  This does mean that, however you set up the kubeconfig file initially, you either need to repeat those steps for each cluster or split out your existing kubeconfig file by hand (it's YAML so it's fairly doable).</p>
<pre class="lang-bash prettyprint-override"><code># specifically for Amazon Elastic Kubernetes Service
kubecfg dc1-main
aws eks update-kubeconfig --name dc1-main ...
kubecfg dc2-main
aws eks update-kubeconfig --name dc2-main ...
</code></pre>
<p>Tools that want to write the configuration also use this variable, which for me mostly comes up if I want to recreate my minikube environment.  You may find it useful to <code>chmod 0400 &quot;$KUBECONFIG&quot;</code> to protect these files once you've created them.</p>
