Google Cloud basics: Activate Cloud Shell

Introduction

Google CLoud like it’s popular contemporary cloud vendors offers a means to manage cloud resources via the command line. The folks at GCP refer to this command line interface as cloud shell and it uses the gcloud sdk. One of the benefits of using cloud shell is that it can be launched directly from the Google Cloud console. In this post, we’ll demonstrate how to familiarize yourself with cloud shell while explaining some Google Cloud terminology along the way.

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources. In the Cloud Console, in the top right tool bar, click the Activate Cloud Shell button and click Continue when prompted. It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID.

What is a project?

Any Google Cloud resources that you allocate and use must belong to a project. You can think of a project as the organizing entity for what you’re building. A project is made up of the settings, permissions, and other metadata that describe your applications. Resources within a single project can work together easily.

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion. You can list the active account name with the following command (Click authorize when prompted):

sahil-linuxnix_01_050d4178878c@cloudshell:~ (labenv-gcp-03-2092e8d44a01)$ gcloud auth list
Credentialed Accounts
ACTIVE ACCOUNT

You can list the project ID with this command:

sahil-linuxnix_01_050d4178878c@cloudshell:~ (labenv-gcp-03-2092e8d44a01)$ gcloud config list project
[core]
project = labenv-gcp-03-2092e8d44a01
Your active configuration is: [cloudshell-18628]
sahil-linuxnix_01_050d4178878c@cloudshell:~ (labenv-gcp-03-2092e8d44a01)$

After Cloud Shell is activated, you can use the command line to invoke the Cloud SDK gcloud tool or other tools available on the virtual machine instance.

Understanding regions and zones

Certain Google Compute Engine resources live in regions or zones. A region is a specific geographical location where you can run your resources. Each region has one or more zones. For example, the us-central1 region denotes a region in the Central United States that has zones us-central1-a, us-central1-b, us-central1-c, and us-central1-f. Resources that live in a zone are referred to as zonal resources. Virtual machine instances and persistent disks live in a zone. If you want to attach a persistent disk to a virtual machine instance, both resources must be in the same zone. Similarly, if you want to assign a static IP address to an instance, the instance must be in the same region as the static IP address.

To see what your default region and zone settings are, run the following commands:

sahil-linuxnix_01_050d4178878c@cloudshell:~ (labenv-gcp-03-2092e8d44a01)$ gcloud config get-value compute/zone
Your active configuration is: [cloudshell-18628]
(unset)
sahil-linuxnix_01_050d4178878c@cloudshell:~ (labenv-gcp-03-2092e8d44a01)$ gcloud config get-value compute/region
Your active configuration is: [cloudshell-18628]
(unset)
sahil-linuxnix_01_050d4178878c@cloudshell:~ (labenv-gcp-03-2092e8d44a01)$

The above output shows that we do not have a region or zone property set for our current session. We could set a region and zone to work with as per our requirement. Although when we use cloud shell a default region and zone are set for the user in the session. these values can be viewed with the gcloud compute project-info describe –project command. For example,

sahil-linuxnix_01_050d4178878c@cloudshell:~ (labenv-gcp-03-2092e8d44a01)$ gcloud compute project-info describe --project labenv-gcp-03-2092e8d44a01
commonInstanceMetadata:
fingerprint: b54dlCjfE88=
items:key: ssh-keys
value: sahil-linuxnix-01-050d4178878c:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqAcF/dvrEVlATCL48HX6Wianye
AxfMCsp+2wqzu38CrHmGEUKg155QWLEETMqY4DLyn3S6K1Ux8r33RwRKvX3WEjG0sql2dS9rq+vcD7ov/gSDbLoIgMBVnoMpNXaX
DmAlR2qcHdasY90WhU6Ay27bLi38hS+FRvyf+mlgwf7VADpdy5mdx00WphSqu6c3VfO2ELMrrky3ac/nzRTCGbmIOv/fZIpuxTj/
oXGagio0One9AQaQ4AztlMiVb31A8Rs6Ji1ix6FG6h+J7gluaFKhOLGpUYhZrOSgHfTlIVe8/d4OYD7V9DdNXV1sDIApofbW4yaw
KrLZ6Na68vcF/dd
sahil-linuxnix-01-050d4178878c@labenv.net
key: enable-oslogin
value: 'true'key: google-compute-default-zone
value: us-central1-a
key: google-compute-default-region
value: us-central1
kind: compute#metadata
creationTimestamp: '2021-05-16T21:03:21.407-07:00'
defaultNetworkTier: PREMIUM
defaultServiceAccount: 23656931543-compute@developer.gserviceaccount.com
id: '7559563518447212006'
kind: compute#project
name: labenv-gcp-03-2092e8d44a01
-------------------------------------output truncated for brevity

The above output shows that our default region is us-central1 and default zone is us-central1-a. If you are unaware of your project id you can retrieve it from the gcloud config list porject command.

Exploring gcloud commands

The gcloud tool offers simple usage guidelines that are available by adding the -h flag (for help) onto the end of any gcloud command. Run the following command:

gcloud -h

You can access more verbose help by appending the –help flag onto a command or running the gcloud help command. For example, to get detailed options on the gcloud config coomand, type

gcloud config –help

Note: Press ENTER or the spacebar to scroll through the help content. To exit the content, type Q.

View the list of configurations in your environment:

sahil-linuxnix_01_050d4178878c@cloudshell:~ (labenv-gcp-03-2092e8d44a01)$ gcloud config list
[component_manager]
disable_update_check = True
[compute]
gce_metadata_read_timeout_sec = 30
[core]
account = sahil-linuxnix-01-050d4178878c@labenv.net
disable_usage_reporting = True
project = labenv-gcp-03-2092e8d44a01
[metrics]
environment = devshell
Your active configuration is: [cloudshell-18628]
sahil-linuxnix_01_050d4178878c@cloudshell:~ (labenv-gcp-03-2092e8d44a01)$

To see all properties and their settings:

gcloud config list –all

If you are using cloud shell for the first time or do not use it frequently then the values of most of these properties will be unset.

List your components:

sahil-linuxnix_01_050d4178878c@cloudshell:~ (labenv-gcp-03-2092e8d44a01)$ gcloud components list
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Components │
├──────────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬──────────┤
│ Status │ Name │ ID │ Size │
├──────────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼──────────┤
│ Update Available │ BigQuery Command Line Tool │ bq │ < 1 MiB │
│ Update Available │ Cloud Pub/Sub Emulator │ pubsub-emulator │ 60.4 MiB │
│ Update Available │ Cloud SDK Core Libraries │ core │ 18.4 MiB │
│ Update Available │ Cloud Storage Command Line Tool │ gsutil │ 3.9 MiB │
│ Deprecated │ Kind │ kind │ │
│ Not Installed │ Appctl │ appctl │ 21.0 MiB │
│ Not Installed │ Cloud Firestore Emulator │ cloud-firestore-emulator │ 40.5 MiB │
│ Not Installed │ Cloud SQL Proxy │ cloud_sql_proxy │ 7.6 MiB │
│ Not Installed │ Cloud Spanner Emulator │ cloud-spanner-emulator │ 21.8 MiB │
│ Not Installed │ Emulator Reverse Proxy │ emulator-reverse-proxy │ 14.5 MiB │
│ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr │ 1.8 MiB │
│ Not Installed │ Kustomize │ kustomize │ 25.9 MiB │
│ Not Installed │ Nomos CLI │ nomos │ 22.9 MiB │
│ Not Installed │ anthos-auth │ anthos-auth │ 16.8 MiB │
│ Not Installed │ config-connector │ config-connector │ 44.3 MiB │
│ Not Installed │ kubectl │ kubectl │ < 1 MiB │
│ Not Installed │ kubectl-oidc │ kubectl-oidc │ 16.8 MiB │
│ Not Installed │ pkg │ pkg │ │
│ Installed │ App Engine Go Extensions │ app-engine-go │ 4.9 MiB │
│ Installed │ Cloud Bigtable Command Line Tool │ cbt │ 7.7 MiB │
│ Installed │ Cloud Bigtable Emulator │ bigtable │ 6.6 MiB │
│ Installed │ Cloud Datalab Command Line Tool │ datalab │ < 1 MiB │
│ Installed │ Cloud Datastore Emulator │ cloud-datastore-emulator │ 18.4 MiB │
│ Installed │ Google Cloud Build Local Builder │ cloud-build-local │ 6.3 MiB │
│ Installed │ Minikube │ minikube │ 50.4 MiB │
│ Installed │ On-Demand Scanning API extraction helper │ local-extract │ 13.6 MiB │
│ Installed │ Skaffold │ skaffold │ 16.7 MiB │
│ Installed │ gcloud Alpha Commands │ alpha │ < 1 MiB │
│ Installed │ gcloud Beta Commands │ beta │ < 1 MiB │
│ Installed │ gcloud app Java Extensions │ app-engine-java │ 53.1 MiB │
│ Installed │ gcloud app Python Extensions │ app-engine-python │ 6.1 MiB │
│ Installed │ gcloud app Python Extensions (Extra Libraries) │ app-engine-python-extras │ 27.1 MiB │
│ Installed │ kpt │ kpt │ 11.7 MiB │
└──────────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴──────────┘

This command displays the gcloud components that are ready for you to use in your current environment. Not all components would be installed by default.

Conclusion

We hope that you found this introductory article to google cloud shell useful. In the next post, we will see how we could provision a virtual machine using only the google cloud shell and the gcloud sdk.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he’s gained over the years with the rest of the community.

Latest posts by Sahil Suri (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *