<p>I have two clusters and the config files are stored in <code>.kube</code>. I am exporting <code>KUBECONFIG</code> as below</p>
<pre><code>export KUBECONFIG=/home/vagrant/.kube/config-cluster1:/home/vagrant/.kube/config-cluster2
</code></pre>
<p>checking the contexts</p>
<pre><code>kubectl config get-contexts
CURRENT   NAME        CLUSTER     AUTHINFO           NAMESPACE
*         cluster-1   cluster-1   kubernetes-admin   
          cluster-2   cluster-2   kubernetes-admin   
</code></pre>
<p>But when I choose cluster-2 as my current context I get an error</p>
<pre><code>kubectl config get-contexts
CURRENT   NAME        CLUSTER     AUTHINFO           NAMESPACE
*         cluster-1   cluster-1   kubernetes-admin   
          cluster-2   cluster-2   kubernetes-admin   

kubectl config use-context cluster-2
Switched to context &quot;cluster-2&quot;.


kubectl get pods -A
error: You must be logged in to the server (Unauthorized)

</code></pre>
<p>If I export only the config for cluster-2 and try running <code>kubectl</code> it works fine.</p>
<p>My question is whether I am exporting the config files properly or should I be doing something more.</p>

Shash
<p>You need to separate the <code>AUTHINFO</code> (<code>context.user</code> on config file) for each cluster with the respective credentials.</p>
<p>For example:</p>
<pre><code>apiVersion: v1
clusters:
- cluster:
    server: https://192.168.10.190:6443
  name: cluster-1
- cluster:
    server: https://192.168.99.101:8443
  name: cluster-2
contexts:
- context:
    cluster: cluster-1
    user: kubernetes-admin-1
  name: cluster-1
- context:
    cluster: cluster-2
    user: kubernetes-admin-2
  name: cluster-2
kind: Config
preferences: {}
users:
- name: kubernetes-admin-1
  user:
    client-certificate: /home/user/.minikube/credential-for-cluster-1.crt
    client-key: /home/user/.minikube/credential-for-cluster-1.key
- name: kubernetes-admin-2
  user:
    client-certificate: /home/user/.minikube/credential-for-cluster-2.crt
    client-key: /home/user/.minikube/credential-for-cluster-2.key
</code></pre>
<hr />
<p>You can find more useful tips in the following article:</p>
<p><a href="https://medium.com/@eduardobaitello/using-different-kubectl-versions-with-multiple-kubernetes-clusters-a3ad8707b87b" rel="noreferrer">Using different kubectl versions with multiple Kubernetes clusters</a>:</p>
<blockquote>
<p>When you are working with multiple Kubernetes clusters, it’s easy to
mess up with contexts and run <code>kubectl</code> in the wrong cluster. Beyond
that, Kubernetes has <a href="https://github.com/kubernetes/community/blob/master/contributors/design-proposals/release/versioning.md#supported-releases-and-component-skew" rel="noreferrer">restrictions</a> for versioning mismatch between the
client (<code>kubectl</code>) and server (kubernetes master), so running commands
in the right context does not mean running the right client version.</p>
</blockquote>
<p>To overcome this:</p>
<ul>
<li>Use <a href="https://github.com/asdf-vm/asdf" rel="noreferrer"><code>asdf</code></a> to manage multiple <code>kubectl</code> versions</li>
<li><a href="https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#set-the-kubeconfig-environment-variable" rel="noreferrer">Set the <code>KUBECONFIG</code></a> env var to change between multiple <code>kubeconfig</code> files</li>
<li>Use <a href="https://github.com/jonmosco/kube-ps1" rel="noreferrer"><code>kube-ps1</code></a> to keep track of your current context/namespace</li>
<li>Use <a href="https://github.com/ahmetb/kubectx" rel="noreferrer"><code>kubectx</code> and <code>kubens</code></a> to change fast between clusters/namespaces</li>
<li>Use aliases to combine them all together</li>
</ul>
<p>I also recommend the following reads:</p>
<ul>
<li><a href="https://medium.com/@ahmetb/mastering-kubeconfig-4e447aa32c75" rel="noreferrer">Mastering the KUBECONFIG file</a> by Ahmet Alp Balkan (<em>Google Engineer</em>)</li>
<li><a href="https://srcco.de/posts/how-zalando-manages-140-kubernetes-clusters.html" rel="noreferrer">How Zalando Manages 140+ Kubernetes Clusters</a> by Henning Jacobs (<em>Zalando Tech</em>)</li>
</ul>
