简体   繁体   中英

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

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?

For me works this:

  1. Login
gcloud auth application-default login
  1. Switch to the project
gcloud config set project <your-project-name>
  1. List available clusters
gcloud container clusters list
  1. Get the cluster credentials
gcloud container clusters get-credentials <cluster-name> --region <cluster-region>

Then I was able to switch the context of kubectl

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
  1. To check what cluster you are currently using:

     kubectl config current-context
  2. To check what clusters you have configured:

     kubectl config get-contexts
  3. To switch, you have two options:

    (Option 1) If you don't have the cluster you to switch to configured, run:

     gcloud container clusters get-credentials <cluster-name> --region <cluster-region> --project <project>

    (Option 2) Use kubectl to switch context, get <name> from step 2:

     kubectl config use-context <name>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM