Getting started with Google Cloud SDK

pradeepbhadani

Pradeep Bhadani

Posted on December 8, 2019

Getting started with Google Cloud SDK

Originally published at pbhadani.com


Learn to setup Google Cloud SDK on your workstation and some operations in this step-by-step guide.

What is Cloud SDK?

The Cloud SDK is a collection of tools to interact with the Google Cloud Platform (GCP). It includes bq, kubectl, gcloud and gsutil command-line tools that can interact with various GCP Services using CLI or in automation scripts.
Examples:

  • Create/manage a Google Cloud Storage (GCS) bucket.
  • Create/manage Google Compute Engine (GCE) instance.
  • Create/manage Google Datalab.
  • Create a BigQuery Dataset.
  • Submit a job to BigQuery.
  • Create/manage firewall rules.

Let's get it up and running

Google provides a script to download and install the Cloud SDK quickly and interactively.

Install Google Cloud SDK

1. Run the following command in your terminal.

  $> curl https://sdk.cloud.google.com | bash

This will download the Cloud SDK package and run the installation script.

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                   Dload  Upload   Total   Spent    Left  Speed
  100   443  100   443    0     0   3475      0 --:--:-- --:--:-- --:--:--  3488
  Downloading Google Cloud SDK install script: https://dl.google.com/dl/cloudsdk/channels/rapid/install_google_cloud_sdk.bash
  ######################################################################## 100.0%
  Running install script from: /tmp/tmp.GZI3OtObH9/install_google_cloud_sdk.bash
  which curl
  curl -# -f https://dl.google.com/dl/cloudsdk/channels/rapid/google-cloud-sdk.tar.gz
  ######################################################################## 100.0%

2. Provide the directory path on prompt

  Installation directory (this will create a google-cloud-sdk subdirectory) (/home/vagrant):

3. Once you provide the directory, the script will prompt for reporting. Choose one of the options.

  Welcome to the Google Cloud SDK!

  To help improve the quality of this product, we collect anonymized usage data and anonymized stacktraces when crashes are encountered; additional information is available at <https://cloud.google.com/sdk/usage-statistics>. This data is handled in accordance with our privacy policy <https://policies.google.com/privacy>. You may choose to opt in this collection now (by choosing 'Y' at the below prompt), or at any time in the future by running the following command:

  gcloud config set disable_usage_reporting false

  Do you want to help improve the Google Cloud SDK (y/N)?

4. This will start installing the package and ask to update the $PATH and enable the auto-complete feature.

  This will install all the core command line tools necessary for working with the Google Cloud Platform.

  Your current Cloud SDK version is: 272.0.0
  Installing components from version: 272.0.0

  ┌────────────────────────────────────────────────────────────────────────────┐
  │                    These components will be installed.                     │
  ├─────────────────────────────────────────────────────┬────────────┬─────────┤
  │                         Name                        │  Version   │   Size  │
  ├─────────────────────────────────────────────────────┼────────────┼─────────┤
  │ BigQuery Command Line Tool                          │     2.0.50 │ < 1 MiB │
  │ BigQuery Command Line Tool (Platform Specific)      │     2.0.50 │ < 1 MiB │
  │ Cloud SDK Core Libraries (Platform Specific)        │ 2019.11.08 │ < 1 MiB │
  │ Cloud Storage Command Line Tool                     │       4.46 │ 3.6 MiB │
  │ Cloud Storage Command Line Tool (Platform Specific) │       4.46 │ < 1 MiB │
  │ Default set of gcloud commands                      │            │         │
  │ gcloud cli dependencies                             │ 2018.08.03 │ 8.6 MiB │
  └─────────────────────────────────────────────────────┴────────────┴─────────┘

  For the latest full release notes, please visit:
    https://cloud.google.com/sdk/release_notes

  ╔════════════════════════════════════════════════════════════╗
  ╠═ Creating update staging area                             ═╣
  ╠════════════════════════════════════════════════════════════╣
  ╠═ Installing: BigQuery Command Line Tool                   ═╣
  ╠════════════════════════════════════════════════════════════╣
  ╠═ Installing: BigQuery Command Line Tool (Platform Spec... ═╣
  ╠════════════════════════════════════════════════════════════╣
  ╠═ Installing: Cloud SDK Core Libraries (Platform Specific) ═╣
  ╠════════════════════════════════════════════════════════════╣
  ╠═ Installing: Cloud Storage Command Line Tool              ═╣
  ╠════════════════════════════════════════════════════════════╣
  ╠═ Installing: Cloud Storage Command Line Tool (Platform... ═╣
  ╠════════════════════════════════════════════════════════════╣
  ╠═ Installing: Default set of gcloud commands               ═╣
  ╠════════════════════════════════════════════════════════════╣
  ╠═ Installing: gcloud cli dependencies                      ═╣
  ╠════════════════════════════════════════════════════════════╣
  ╠═ Creating backup and activating new installation          ═╣
  ╚════════════════════════════════════════════════════════════╝

  Performing post processing steps...done.

  Update done!

  Modify profile to update your $PATH and enable shell command
  completion?

  Do you want to continue (Y/n)? Y

5. Provide the bashrc path.

The Google Cloud SDK installer will now prompt you to update an rc file to bring the Google Cloud CLIs into your environment.

Enter a path to an rc file to update, or leave blank to use
[/home/vagrant/.bashrc]:

6. Now, open a new terminal or reload the current session by running below command.

  $> exec -l $SHELL

7. Verify installation.

  $> gcloud version

This should list the current version of gcloud utility.

  Google Cloud SDK 272.0.0
  bq 2.0.50
  core 2019.11.16
  gsutil 4.46

8. The above script only install core packages and all we can list all the available packages.

  $> gcloud components list
  Your current Cloud SDK version is: 272.0.0
  The latest available version is: 272.0.0

  ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
  │                                                 Components                                                 │
  ├───────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬──────────┤
  │     Status    │                         Name                         │            ID            │   Size   │
  ├───────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼──────────┤
  │ Not Installed │ App Engine Go Extensions                             │ app-engine-go            │  4.9 MiB │
  │ Not Installed │ Appctl                                               │ appctl                   │ 21.0 MiB │
  │ Not Installed │ Cloud Bigtable Command Line Tool                     │ cbt                      │  7.5 MiB │
  │ Not Installed │ Cloud Bigtable Emulator                              │ bigtable                 │  6.6 MiB │
  │ Not Installed │ Cloud Datalab Command Line Tool                      │ datalab                  │  < 1 MiB │
  │ Not Installed │ Cloud Datastore Emulator                             │ cloud-datastore-emulator │ 18.4 MiB │
  │ Not Installed │ Cloud Firestore Emulator                             │ cloud-firestore-emulator │ 40.0 MiB │
  │ Not Installed │ Cloud Pub/Sub Emulator                               │ pubsub-emulator          │ 34.9 MiB │
  │ Not Installed │ Cloud SQL Proxy                                      │ cloud_sql_proxy          │  3.8 MiB │
  │ Not Installed │ Emulator Reverse Proxy                               │ emulator-reverse-proxy   │ 14.5 MiB │
  │ Not Installed │ Google Cloud Build Local Builder                     │ cloud-build-local        │  6.0 MiB │
  │ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr    │  1.8 MiB │
  │ Not Installed │ Skaffold                                             │ skaffold                 │ 22.1 MiB │
  │ Not Installed │ gcloud Alpha Commands                                │ alpha                    │  < 1 MiB │
  │ Not Installed │ gcloud Beta Commands                                 │ beta                     │  < 1 MiB │
  │ Not Installed │ gcloud app Java Extensions                           │ app-engine-java          │ 62.0 MiB │
  │ Not Installed │ gcloud app PHP Extensions                            │ app-engine-php           │          │
  │ Not Installed │ gcloud app Python Extensions                         │ app-engine-python        │  6.0 MiB │
  │ Not Installed │ gcloud app Python Extensions (Extra Libraries)       │ app-engine-python-extras │ 27.1 MiB │
  │ Not Installed │ kubectl                                              │ kubectl                  │  < 1 MiB │
  │ Installed     │ BigQuery Command Line Tool                           │ bq                       │  < 1 MiB │
  │ Installed     │ Cloud SDK Core Libraries                             │ core                     │ 12.5 MiB │
  │ Installed     │ Cloud Storage Command Line Tool                      │ gsutil                   │  3.6 MiB │
  └───────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴──────────┘
  To install or remove components at your current SDK version [272.0.0], run:
    $ gcloud components install COMPONENT_ID
    $ gcloud components remove COMPONENT_ID

  To update your SDK installation to the latest version [272.0.0], run:
    $ gcloud components update

9. Install additional component, for e.g. kubectl

  $> gcloud components install kubectl

10. To install, beta functionality

  $> gcloud components install beta

Initialize Google Cloud SDK

Now to interact with GCP services, we must initialize SDK and setup authentication.

1. Initialize the SDK.

  $> gcloud init

2. Accept to login to your Google account.

  To continue, you must login. Would you like to log in (Y/n)? Y

3. This will open your browser and prompt for permissions. Login with your Google account credentials and Allow.

4. Select the preferred project and zone.

5. You should now see the following message for successful initialization.

  gcloud has now been configured!
  You can use [gcloud config] to change more gcloud settings.

  Your active configuration is: [default]

6. Verify initialization.

  $> gcloud auth list

This will display the active account.

Let's do something with gcloud

Google Cloud Storage (GCS) Bucket Operations

1. Create a GCS Bucket

  $> gsutil mb gs://my-bucket-34678945/

2. Create a file locally and upload to GCS Bucket.

  $> echo "Hello GCS" > hello-gcs.txt
  $> gsutil cp hello-gcs.txt gs://my-bucket-34678945/

3. List objects in a GCS Bucket.

  $> gsutil ls -r gs://my-bucket-34678945/

4. Get the GCS Bucket size.

  $> gsutil du -sh gs://my-bucket-34678945/

5. Delete a GCS Bucket.

warning: Following command deletes all the objects stored in the specified bucket and cannot be recovered.

  $> gsutil rm -r gs://my-bucket-34678945

Google Compute Engine (GCE) Operation

1. Create a VM instance.

  $> gcloud compute instances create my-instance

This will create a VM instance will default configuration.

Output will be similar as below:

  Created [https://www.googleapis.com/compute/v1/projects/your-project-id/zones/europe-west1-d/instances/my-instance].
  NAME         ZONE            MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP    STATUS
  my-instance  europe-west1-d  n1-standard-1               10.132.0.3   IP.IP.IP.IP  RUNNING

2. Update VM instance and add some labels.

  $> gcloud compute instances update my-instance \
        --update-labels=key1=value1,key2=value2

3. Delete a VM instance.

  $> gcloud compute instances delete my-instance

Below is the terminal recording:

To revoke gcloud access from terminal

  $> gcloud auth revoke

Hope this blog helps you get familiar with Google Cloud SDK.

💖 💪 🙅 🚩
pradeepbhadani
Pradeep Bhadani

Posted on December 8, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Getting started with Google Cloud SDK
googlecloudplatform Getting started with Google Cloud SDK

December 8, 2019