<p>Any suggestions on how track of kubectl configs(~/.kube/config) which allows you to access the kubernetes clusters?  I have clusters running on different environments(local/prod) where i connect to the <strong>same</strong> namespace where project is deployed on and whenever i need to connect  to a particular cluster, i run the below to configure  ( different commands on aws/gcp/ microk8s etc) and the configuration gets attached to ~/.kube/config. Is there any easy way to know where you are connected or track which config is being used? Its a disaster waiting to happen unless you do a explicit check.</p>
<pre><code>aws eks --region region update-kubeconfig --name cluster_name
</code></pre>
<p>Current method used:</p>
<ul>
<li>Either (cat ~/.kube/config) i check the to see what cluster im connecting to.</li>
<li>move the config to some other directory and move the config back once im done.</li>
<li>kubectl get nodes to see where I'm connected.</li>
</ul>

DBSand
<h4>Using kubectl</h4>
<p>Kubectl has built in support for <a href="https://v1-18.docs.kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#config" rel="nofollow noreferrer">managing contexts</a>. After you add a context in <code>~/.kube/config</code> file, manually or, via <code>aws eks update-kubeconfig</code>, you can use the <code>config</code> sub-command to switch between contexts.</p>
<p>To view all saved contexts and highlight the current one:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl config get-contexts
</code></pre>
<p>To just view the current context:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl config current-context
</code></pre>
<p>To switch to another context</p>
<pre class="lang-sh prettyprint-override"><code>kubectl config use-context &lt;context-name&gt;
</code></pre>
<p>To delete a context:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl config delete-context &lt;context-name&gt;
</code></pre>
<h4>Specific configuration file</h4>
<p>Sometimes it might be the case that all the cluster connections cannot be in the same kube config file, but instead, user has separate kube config files per cluster.</p>
<p>To run <code>kubectl</code> with a specific configuration, one can use <code>--kubeconfig</code> argument:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl --kubeconfig ./someConfig -n someNs get pods
</code></pre>
<h4>Shell Aliases</h4>
<p>And when running from Linux shell or windows powershell, one can also use &quot;aliases&quot;.</p>
<p><strong>Linux Bash example:</strong></p>
<p>Use bash <code>alias</code> to define commands as aliases:</p>
<pre class="lang-sh prettyprint-override"><code># Define a kubectl alias for specific cluster
alias myCluster=&quot;kubectl --kubeconfig ./myClusterConfig&quot;

# Define a kubectl alias for specific cluster and specific namespace
alias myClusterNs=&quot;kubectl --kubeconfig ./myClusterConfig -n myNamespace&quot;
</code></pre>
<p>Usage:</p>
<pre class="lang-sh prettyprint-override"><code># Using cluster kubectl alias
myCluster -n myNamespace get pods

# Using cluster kubectl alias with namespace
myClusterNs get pods
</code></pre>
<p>The alias definitions can be saved to <code>~/.profile</code> for permanent usage.</p>
<p><strong>Windows Powershell example:</strong></p>
<p>In Windows Powershell, a function can be defined as follows:</p>
<pre><code>function myCluster { kubectl --kubeconfig .\myClusterConfig $args }
</code></pre>
<p>And used as:</p>
<pre><code>myCluster -n myNamespace get pods
</code></pre>
<p>More arguments like <code>-n &lt;namespace&gt;</code> can also be specified in function definition before <code>$args</code>. Make sure to properly quote (&quot;) the arguments with special characters on windows.</p>
