How to change contexts in Kubernetes

Prev Next

WHAT TO EXPECT

In this article, users will learn how to change the working context in a Kubernetes environment to use a different default.

To Change K8s Context in Your Preferred Cloud

If there is more than one K8s cluster in your preferred cloud, then you may need to switch between them to run commands in the CloudShell Bash. Below are the steps to switch between K8s clusters.

  1. Get a list of all K8s Contexts by using the following command:

    kubectl config get-contexts

    Example in Azure:

    $ kubectl config get-contexts
    CURRENT   NAME                 CLUSTER              AUTHINFO                                          NAMESPACE
              cilium-sample        cilium-sample        clusterUser_saDevNetwork_cilium-sample            
              cilium-sample-200    cilium-sample-200    clusterUser_test-donna-200-rg_cilium-sample-200   
              cilium-sample2       cilium-sample2       clusterUser_saDevNetwork_cilium-sample2           
    *         cilium-sample300     cilium-sample300     clusterUser_test-donna-300-rg_cilium-sample300    
              dsd-k8-cluster-100   dsd-k8-cluster-100   clusterUser_saDevNetwork_dsd-k8-cluster-100  

    Notice in the above list, there are multiple contexts, but only one has the asterisks (*). The asterisks mark the default context.

  2. To change context, run the following command. The example is changing to cilium-sample2.

    kubectl config use-context cilium-sample2
  3. Re-run the get-context command:

    kubectl config get-contexts

    Example in Azure:

    $ kubectl config get-contexts
    CURRENT   NAME                 CLUSTER              AUTHINFO                                          NAMESPACE
              cilium-sample        cilium-sample        clusterUser_saDevNetwork_cilium-sample            
              cilium-sample-200    cilium-sample-200    clusterUser_test-donna-200-rg_cilium-sample-200   
    *         cilium-sample2       cilium-sample2       clusterUser_saDevNetwork_cilium-sample2           
              cilium-sample300     cilium-sample300     clusterUser_test-donna-300-rg_cilium-sample300    
              dsd-k8-cluster-100   dsd-k8-cluster-100   clusterUser_saDevNetwork_dsd-k8-cluster-100

    As you can see above, the asterisk (*) has changed positions to the desired context, cilium-sample2.