How can I switch between Google Kubernetes Engine (GKE) clusters with kubectl?

Viewed 13

From the Google Cloud Shell, when I run kubectl I am running it on the GKE cluster (cluster-1) from my previous project (my-project-1). I would like to run it on the gke cluster (cluster-2) in my new project (my-project-2).

How can I update my context to use the new cluster?

1 Answers

Here are the commands that work for me when I want to switch between clusters:

  1. Check current cluster:

    kubectl config current-context
    

    Example output:

    my-project-1_us-central1_cluster-1
    
  2. Switch to a different cluster (replace $CLUSTER_NAME, $REGION, and $PROJECT_NAME with your values):

    gcloud container clusters get-credentials $CLUSTER_NAME --region $REGION --project $PROJECT_NAME
    

    Example:

    gcloud container clusters get-credentials cluster-2 --region us-central1 --project my-project-2
    
  3. Confirm you have switched clusters by re-running the current-context command

    kubectl config current-context
    

    Example output:

    my-project-2_us-central1_cluster-2
    
Related