<p>I am using kops to create / manage kubernetes clusters in AWS. </p>

<p>I have created a pilot / intermediate instance to access all the clusters. </p>

<p>I have noticed that even if we create multiple SSH sessions (using same user), and if I change context to cluster-a, it gets changed to cluster-a in other session.</p>

<p>The problem is we need to switch context every time if we want to manage different cluster simultaneously. It is very hard to maintain context switching if more than two people are using that instance.</p>

<p>Question may arise that why are we using multiple clusters, the things is there are multiple streams and modules being developed on parallel and all go to testing at same time.</p>

<p>Is there any way where I don't have to switch context and <code>kops</code>/<code>kubectl</code> can understand cluster context automatically?</p>

<p>Example: If I am executing command from <code>directory-a</code> then it automatically understand the cluster <code>a.k8s.local</code>. Just thinking this, any other solution is welcomed.</p>

<p>Last solution is to create separate pilot instances for all the clusters, which I am trying to avoid as those instances doesn't provide much value and just increase costing.  </p>

<p>I am using exactly the solution you are searching for: I can manage a specific cluster when I am located in a specific directory.</p>

<p>First of all, let me explain why you cannot work on multiple clusters at the same time even on different SSH sessions.</p>

<p>When you do a <code>kubectl config use-context</code> to switch the current context, you are actually modifiying <code>current-context: your-context</code> in the <code>~/.kube/config</code>. So if one of your team members is switching the context, that also applies to your other team members especially if they connect to the same user.</p>

<p>Now, the following steps can help you workaround this issue:</p>

<ul>
<li>Install <a href="https://direnv.net/" rel="nofollow noreferrer">direnv</a>. This tool will allow you to set custom env vars when you are located in a directory.</li>
<li><p>Next to the kubeconfig files, create a <code>.envrc</code> file:</p>

<pre><code>path_add KUBECONFIG kubeconfig
</code></pre></li>
<li>Run <code>direnv allow</code></li>
<li>Check the content of the <code>KUBECONFIG</code> env var (<code>echo $KUBECONFIG</code>). It should look like <code>/path/to/dir-a/kubeconfig:/home/user/.kube/config</code></li>
<li>Split your current <code>~/.kube/config</code> in multiple <code>kubeconfig</code> files located in different folders: <code>dir-a/kubeconfig</code>, <code>dir-b/kubeconfig</code> and so on. You can also go into dir-a and do a <code>kops export kubecfg your-cluster-name</code>.</li>
<li>Check the current context with the <code>kubectl config view --minify</code></li>
<li>Go to <code>dir-b</code> and repeat from step 2</li>
</ul>

<p>You could also setup other env vars in the <code>.envrc</code> that could help you manage these different clusters (maybe a different kops state store).</p>
