Change kubectl context to use another k8s cluster

2 min read | by Jordi Prats

When you have on your .kube/config several clusters, you can choose to which cluster you want to connect by setting the appropriate context.

Using kubectl config get-contexts we will be able to list all the available context and to know which one are we currently using:

$ kubectl config get-contexts
CURRENT   NAME                                                   CLUSTER                                                AUTHINFO                                               NAMESPACE
          arn:aws:eks:us-west-2:615418998474:cluster/demoeks     arn:aws:eks:us-west-2:615418998474:cluster/demoeks     arn:aws:eks:us-west-2:615418998474:cluster/demoeks   
*         minikube                                               minikube                                               minikube                                               default

If we get the list of nodes we can see that we are indeed using the minikube cluster:

$ kubectl get nodes
NAME       STATUS   ROLES    AGE   VERSION
minikube   Ready    master   21d   v1.19.4

To start using another cluster we just need to change context using kubectl config use-context, for example:

$ kubectl config use-context arn:aws:eks:us-west-2:615418998474:cluster/demoeks
Switched to context "arn:aws:eks:us-west-2:615418998474:cluster/demoeks".

If we repeat the kubectl config get-contexts we will be able to see that we are no longer using the minikube cluster, now we are connected to a EKS cluster:

$ kubectl config get-contexts
CURRENT   NAME                                                   CLUSTER                                                AUTHINFO                                               NAMESPACE
*         arn:aws:eks:us-west-2:615418998474:cluster/demoeks     arn:aws:eks:us-west-2:615418998474:cluster/demoeks     arn:aws:eks:us-west-2:615418998474:cluster/demoeks   
          minikube                                               minikube                                               minikube                                               default

By getting the list of nodes again we'll be able see the difference with the minikube we were connected to:

$ kubectl get nodes
NAME                                           STATUS   ROLES    AGE   VERSION
ip-10-12-204-167.us-west-2.compute.internal   Ready    <none>   99d   v1.17.11-eks-cfdc40
ip-10-12-205-119.us-west-2.compute.internal   Ready    <none>   99d   v1.17.11-eks-cfdc40
ip-10-12-206-206.us-west-2.compute.internal   Ready    <none>   99d   v1.17.11-eks-cfdc40

Posted on 22/12/2020