Working with multiple gcloud configurations

Rationale TL;DR

Imagine that you have multiple Google cloud accounts and you want to switch among them easily. You would expect kubectl to work seamlessly. This is usually not the case.

Needed dependencies

To get started you would need to have installed gcloud and optionally kubectl.

Let’s list and configure a new account

To get started you need an account configured. To list the accounts:

gcloud config configurations list

If you want to configure a new account:

gcloud init

This will ask you some questions and finally open the browser to finish the process. Once you have that, you would be able to see the new config:

NAME           IS_ACTIVE  ACCOUNT                    PROJECT     DEFAULT_ZONE    DEFAULT_REGION
default        True       gustau.perez@lalala.com    default     europe-west1-b  europe-west1
gusi           False      gustau.perez@gmail.com     gusitest    europe-west3-c  europe-west3

Now, to switch from the default configuration to the gusi configuration you would need to first activate the gusi config. To do so:

gcloud config configurations activate gusi

Now, you would expect kubectl and other commands to work out of the box. That is not the case. You would be able to list the clusters in the account with gcloud container clusters list:

NAME          LOCATION        MASTER_VERSION  MASTER_IP      MACHINE_TYPE  NODE_VERSION   NUM_NODES  STATUS
gusicluster2  europe-west3-b  1.13.7-gke.19   XX.XX.XX.XX    g1-small      1.13.7-gke.19  3          RUNNING

However, kubectl won’t still work with the new cluster. You need to fetch the cluster credentials:

$ ▶  gcloud container clusters get-credentials --zone europe-west3-c gusi
Fetching cluster endpoint and auth data.
kubeconfig entry generated for gusi.

Now you would be able to list the nodes of the gusi cluster:

$ ▶ kubectl get nodes
NAME                                          STATUS   ROLES    AGE     VERSION
gke-gusicluster2-default-pool-06044af2-djcv   Ready    <none>   3d18h   v1.13.7-gke.19
gke-gusicluster2-default-pool-06044af2-fdb4   Ready    <none>   3d18h   v1.13.7-gke.19
gke-gusicluster2-default-pool-06044af2-tf8j   Ready    <none>   3d18h   v1.13.7-gke.19