Working with multiple GCP configurations & Kubernetes clusters

The Google Cloud Platform provides a CLI to perform many common platform tasks. It is a part of the Google Cloud SDK. The CLI releases have the same version as the SDK. This post will talk about some of the common commands that will help you manage your way around with the Google Cloud Platform, especially if you have multiple accounts to work with.

Show gcloud information

To display the path of the active configuration along with information about the current gcloud environment, run:

gcloud info

Version details

View version and check if updates are available.

$ gcloud version                                                                                                        
Google Cloud SDK 240.0.0
beta 2019.02.22
bq 2.0.42
core 2019.03.22
gsutil 4.37
kubectl 2019.03.22
pubsub-emulator 2018.02.02
Updates are available for some Cloud SDK components.  To install them,
please run:
  $ gcloud components update

Update

To update your gcloud components use the following:

$ gcloud components update

What is a Configuration?

A configuration is like your profile and you can have as many as you like. By default, there is a configuration called default. If you use multiple Google accounts it makes sense to create different profiles as per your need.

View all Configurations

List all configurations to get the name of the configuration you want to switch to. The configuration that is active will be listed as True under IS_ACTIVE column when you say:

$ gcloud config configurations list

Create a new Configuration

You may want to create a new configuration for every project. This way, you can easily work with and switch to various Google components and Kubernetes clusters as per your requirement.

$ gcloud config configurations create <YOUR_CONFIGURATION_NAME>

Initialize a configuration

To configure your Cloud, start by initializing gcloud, like so...

$ gcloud init

It will open a browser for you and ask you to login. Login with the account you like. If you have projects created in your account, it will list all the projects. You may choose to create a new Project too. Follow the prompts and you should be good.

If you have multiple Google accounts, you can run gcloud init again and choose to create a new configuration. Just ensure that you login with your other Google account and follow the prompts.

Activate configuration you need

$ gcloud config configurations activate <NAME>

Delete configuration you don't need using:

$ gcloud config configurations delete <NAME>

List projects

$ gcloud projects list

List configuration details

To view current configuration details you may simply type:

$ gcloud config list

Working with multiple Kubernetes clusters

If you have multiple Google accounts and Kubernetes clusters, you might want to change your configuration and expect that Kubectl would automatically detect those changes. Unfortunately, it doesn't (from what I know). So, how can you quickly change the context so that you can work with different Kubernetes clusters from the command line? Here is a super simple way...

Step 1 > Activate your configuration and list the clusters

I am assuming that you have already created different configurations with different projects. If not, simply create configurations as discussed above and do a gcloud init. Initialize each configuration with an appropriate project.

$ gcloud config configurations list
NAME      IS_ACTIVE  ACCOUNT                 PROJECT                 DEFAULT_ZONE  DEFAULT_REGION
project1    True       account1@domain1.com       project1-1010101
project2    False      account2@domain2.com       project2-2020220
project3    False      account3@domain3.com       project3-3939393

In this case, project1 is active. You can now activate any other project based on their friendly configuration name.

$ gcloud config configurations activate <CONFIG_NAME>
Step 2 > List & Get credentials for the cluster

List all clusters:

$ gcloud container clusters list

Get the credential of the cluster you want to work with:

$ gcloud container clusters get-credentials --zone <ZONE_NAME> <CLUSTER_NAME>

That's about it... now, fire your kubectl commands and it will be oblige!

© 2024, Attosol Private Ltd. All Rights Reserved.