How to switch between Kubernetes clusters in kubectl

Last updated 5th September, 2023

When learning Kubernetes and kubectl for the first time I really struggled with understanding how to switch between contexts (or clusters). So here's a <1 minute how to on switching between them.

Listing Your Contexts

First, we can list the current contexts with the get-contexts command

kubectl config get-contexts

# CURRENT NAME            CLUSTER 
#         Cluster1        Cluster1
#         Cluster2        Cluster2
# *       Cluster3        Cluster3

This will show you the available clusters that you are connected to. If you can't see the cluster you're looking to switch to, it's likely that its not been connected yet. The Kubernetes have a good instruction guide on how to connect to a new cluster.

The * indicates the currently active cluster. You can also show the current cluster by running kubectl config current-context.


Switching Between Contexts

To switch contexts, run the following command with one of the available contexts

kubectl config use-context Cluster1


# CURRENT NAME            CLUSTER 
# *       Cluster1        Cluster1
#         Cluster2        Cluster2
#         Cluster3        Cluster3

You should see the * move to the cluster you defined.

Always run kubectl config current-context after switching to make sure you've definitely changed contexts. Deploying changes to the wrong context can have devastating consequences!

Happy Monday!

No comments yet…

DevInTheWild.

Login To Add Comments.

Want More Like This?

Subscribe to occasional updates.