How to use the Command-Line Interface for Google Cloud: Gcloud

Google Cloud Platform (GCP) provides an efficient and robust way to manage cloud resources. GCloud, the command-line interface (CLI) that allows users to interact with a wide range of Google Cloud services. This blog is part of the series on earning the Google Cloud Associate Certificate and focuses on understanding and using the Gcloud CLI effectively.

gcloud

What is Gcloud?

Gcloud is a command-line tool used to manage Google Cloud resources. From managing Compute Engine Virtual Machines (VMs) to databases and beyond, Gcloud provides a comprehensive interface to create, update, delete, and query existing resources. Here’s what makes Gcloud an important tool:

  • Manage most GCP services, including Compute Engine, Kubernetes clusters, Cloud Storage, and more.
  • Extensibility: Supports service-specific CLI tools such as:
    • gsutil for Cloud Storage
    • bq for BigQuery
    • kubectl for Kubernetes
  • Automation-Friendly: Use Gcloud in scripts for automation, deployments, and batch operations.

Getting Started with Gcloud

Installation

Gcloud is part of the Google Cloud SDK, which requires Python. You can find detailed installation instructions at GCP’s official documentation. If you prefer a ready-to-use environment, Gcloud is pre-installed in Cloud Shell.

Initial Setup

To start using Gcloud, initialize the tool with:

gcloud init

This command helps you:

  • Authenticate your Google Cloud account.
  • Configure default project, region, and zone settings.

You can review the active configuration with:

gcloud config list

Configuring Properties

You can customize Gcloud settings using the config command:

gcloud config set SECTION/PROPERTY VALUE

For example:

gcloud config set core/project my-project-id
gcloud config set compute/region us-central1
gcloud config set compute/zone us-central1-a

These settings define your active configuration. To remove a configuration, use gcloud config unset.


Managing Multiple Configurations

When working on multiple projects, managing separate configurations simplifies resource management.

  • To Create a configuration: gcloud config configurations create dev-config
  • To Activate a configuration: gcloud config configurations activate dev-config
  • To List configurations: gcloud config configurations list

Command Structure: Gcloud Fundamentals

Gcloud commands follow a structured syntax:

gcloud GROUP SUBGROUP ACTION [OPTIONS]
  • GROUP: Service group (e.g., compute, container, storage).
  • SUBGROUP: Specific resource within the service (e.g., instances, zones).
  • ACTION: Operation to perform (e.g., create, list, delete).

Examples:

gcloud compute instances list
gcloud compute zones list
gcloud compute machine-types list --filter="zone:us-central1-b"

Working with Compute Engine Instances

Creating Compute Instances

To create a new virtual machine, use:

gcloud compute instances create INSTANCE_NAME \
  --machine-type MACHINE_TYPE \
  --image-family IMAGE_FAMILY \
  --zone ZONE

Options:

  • --machine-type: Specifies the VM type (e.g., n1-standard-1).
  • --image-family: Chooses the OS image family (e.g., debian-11).
  • --tags: Assigns tags for network firewall rules.
  • --metadata: Adds key-value metadata, such as startup scripts.

Managing Instances

  • Start an instance: gcloud compute instances start INSTANCE_NAME
  • Stop an instance: gcloud compute instances stop INSTANCE_NAME
  • Delete an instance: gcloud compute instances delete INSTANCE_NAME

Moving Instances Between Zones

Unfortunately, there is no direct way to move instances between zones or regions like before.

You need to use a combination of creating a snapshot, creating a new instance from that snapshot in the destination zone, and then deleting the original instance if needed.

Refer to this link for the updated stepshttps://cloud.google.com/compute/docs/instances/moving-instance-across-zones

Using Instance Templates

Instance templates simplify the creation of consistent VM instances:

gcloud compute instance-templates create TEMPLATE_NAME \
  --machine-type MACHINE_TYPE \
  --image-family IMAGE_FAMILY

You can create VMs from an instance template:

gcloud compute instances create INSTANCE_NAME \
  --source-instance-template TEMPLATE_NAME

Optimizing Your Workflow with Gcloud

List and Describe Commands

Use list to view resources and describe for details:

gcloud compute instances list
gcloud compute regions describe us-central1

Options:

  • --filter: Filter results (e.g., zone:us-central1-a).
  • --sort-by: Sort results (e.g., --sort-by NAME).

Automating Deployments

Incorporate Gcloud commands into scripts for repetitive tasks such as deployments, scaling, or monitoring.

Best Practices for Using Gcloud

  1. Use Multiple Configurations: Separate environments for dev, test, and production.
  2. Automate with Scripts: Combine Gcloud with shell scripts to streamline workflows.
  3. Monitor Costs: Use Gcloud to track and control spending on GCP resources.
  4. Leverage Pre-Built Tools: Use gsutil, bq, and kubectl for service-specific needs.

Gcloud is an essential tool for anyone working with Google Cloud. Its comprehensive command-line functionality, combined with ease of use, makes it ideal for managing cloud resources efficiently. By mastering Gcloud, you’re one step closer to earning the Google Cloud Associate Certificate and becoming proficient in cloud resource management.

Stay tuned for the next blog in this series!

Leave a Comment

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

Scroll to Top