Google Cloud gcloud Commands Explained with Examples (gcloud CLI)

This article covers the full gcloud CLI list to help you navigate Google Cloud with ease. We will explore all important gcloud commands and explain how to use them in your daily work.

The tutorial might be big, but the knowledge you gain from it can make a big difference in your career.

Getting Started and Configuration

We begin our gcloud CLI list with the basics. These Cloud gcloud commands help us set up our environment and ensure the tool is ready to manage resources. We use these configuration commands every time we start a new project or switch working environments.

gcloud init

This command starts the initial configuration wizard for the gcloud CLI. It walks us through logging in and selecting a default project. We use this when we run gcloud for the first time on a machine. It results in a fully configured workspace ready to execute commands.

Example: gcloud init

gcloud info

This command displays detailed information about our current gcloud installation. It shows the installed version, the Python path, and the active configuration. We run this to check our setup or when troubleshooting errors. It produces a summary of system properties.

Example: gcloud info

gcloud config list

This command lists the properties of our current active configuration. It shows values like the default project, region, and zone. We use it to verify our current settings before running other tasks. The output is a list of key-value pairs.

Example: gcloud config list

gcloud config set project

This command sets the default project ID for all future commands in this session. We use it to avoid typing the project flag repeatedly. It changes the active project context immediately.

Example: gcloud config set project my-project-id

gcloud config set compute/region

This command sets the default compute region for resources like virtual machines. We use it to define where our resources live. It saves us from specifying the region for every single command.

Example: gcloud config set compute/region us-central1

gcloud config set compute/zone

This command sets the default compute zone within a region. We use it when working with specific zonal resources. It ensures our commands target the correct location automatically.

Example: gcloud config set compute/zone us-central1-a

gcloud config configurations create

This command creates a new named configuration for our workspace. We use this to manage multiple different setups or accounts. It allows us to switch between different projects easily.

Example: gcloud config configurations create my-config

gcloud config configurations activate

This command switches the active configuration to the one we specify. We use it when we need to change our working context. It makes the specified configuration the current one for all commands.

Example: gcloud config configurations activate my-config

Authentication and Account Management

We must secure our access properly. The following gcloud commands manage our login state and permissions. These Cloud gcloud commands ensure that we have the right credentials to interact with our Google Cloud resources safely.

gcloud auth login

This command authorizes the gcloud CLI to access Google Cloud using our user account. We use it when we need to log in for the first time. It opens a browser window for us to approve access.

Example: gcloud auth login

gcloud auth list

This command lists all authenticated accounts currently stored on the system. We use it to see which accounts are available and which one is active. It displays a list of email addresses with an asterisk for the active one.

Example: gcloud auth list

gcloud auth application-default login

This command authorizes Application Default Credentials for our development tools. We use it when running local applications that need to call Google Cloud APIs. It stores a token that libraries use automatically.

Example: gcloud auth application-default login

gcloud auth revoke

This command revokes the authorization for a specific account or all accounts. We use this when we want to log out or protect our security. It makes the credentials invalid for future API calls.

Example: gcloud auth revoke

gcloud services enable

This command enables one or more Google Cloud APIs for our project. We use it when our code needs a specific service like BigQuery or Compute Engine. It activates the service so we can use it.

Example: gcloud services enable compute.googleapis.com

gcloud services disable

This command disables a Google Cloud API for our project. We use it to turn off services we no longer need to improve security or reduce costs. It restricts access to that specific service.

Example: gcloud services disable dataproc.googleapis.com

Compute Engine Virtual Machines

Compute Engine is a core part of Google Cloud. These gcloud commands help us manage our virtual machine instances. We use these Cloud gcloud commands to create, start, stop, and connect to our servers effectively.

gcloud compute instances create

This command creates a new virtual machine instance in Google Compute Engine. We use it to provision a new server with specific settings like machine type and image. It results in a running VM ready for use.

Example: gcloud compute instances create my-vm --machine-type=e2-medium

gcloud compute instances list

This command lists all virtual machine instances in our project or a specific zone. We use it to see the status and details of our servers. It produces a table of instance names, statuses, and IPs.

Example: gcloud compute instances list

gcloud compute instances describe

This command shows detailed information about a specific virtual machine. We use it to view all properties and metadata of an instance. It returns a comprehensive list of configuration details.

Example: gcloud compute instances describe my-vm --zone=us-central1-a

gcloud compute instances start

This command starts a virtual machine that is currently stopped. We use it to bring a server back online. It changes the status of the instance to RUNNING.

Example: gcloud compute instances start my-vm --zone=us-central1-a

gcloud compute instances stop

This command stops a running virtual machine gracefully. We use it to save resources when a server is not needed. It shuts down the VM and changes its status to TERMINATED.

Example: gcloud compute instances stop my-vm --zone=us-central1-a

gcloud compute instances reset

This command performs a hard reset on a virtual machine. We use it when a server is unresponsive and needs a restart. It forces the VM to reboot immediately.

Example: gcloud compute instances reset my-vm --zone=us-central1-a

gcloud compute instances delete

This command deletes a virtual machine from our project. We use it when we no longer need the server to save costs. It permanently removes the VM and its resources.

Example: gcloud compute instances delete my-vm --zone=us-central1-a

gcloud compute ssh

This command opens an SSH connection to a specific virtual machine. We use it to log in and run commands directly on the server. It gives us terminal access to the instance.

Example: gcloud compute ssh my-vm --zone=us-central1-a

gcloud compute scp

This command copies files between our local machine and a virtual machine using SCP. We use it to transfer data or scripts to our server. It securely moves files across the network.

Example: gcloud compute scp local-file.txt my-vm:~/ --zone=us-central1-a

gcloud compute disks create

This command creates a new persistent disk for storage. We use it to add extra storage space for our virtual machines. It results in a blank disk we can attach to an instance.

Example: gcloud compute disks create my-disk --size=100GB --zone=us-central1-a

gcloud compute disks delete

This command deletes a persistent disk. We use it to remove storage that is no longer needed. It permanently deletes the data and frees up space.

Example: gcloud compute disks delete my-disk --zone=us-central1-a

gcloud compute machine-types list

This command lists all available machine types in a specific zone. We use it to find the right hardware configuration for our VMs. It shows options for CPU and memory combinations.

Example: gcloud compute machine-types list --zone=us-central1-a

gcloud compute images list

This command lists all available operating system images. We use it to choose the OS for our new virtual machines. It displays public and custom images we can use.

Example: gcloud compute images list

Cloud Storage Management

Handling data is crucial. These gcloud commands allow us to work with Cloud Storage buckets and objects. We use these Cloud gcloud commands to upload, download, and organise our files efficiently.

gcloud storage ls

This command lists buckets or objects in Google Cloud Storage. We use it to browse the contents of our storage. It shows the names and sizes of our files and buckets.

Example: gcloud storage ls

gcloud storage cp

This command copies files between our local machine and Cloud Storage. We use it to upload data to the cloud or download it locally. It handles the transfer of single files or directories.

Example: gcloud storage cp local-file.txt gs://my-bucket/

gcloud storage rsync

This command synchronizes a local directory with a Cloud Storage bucket. We use it to make sure the local and remote data match exactly. It only copies changed files to save time.

Example: gcloud storage rsync ./local-dir gs://my-bucket/remote-dir

gcloud storage rm

This command removes objects or buckets from Cloud Storage. We use it to delete files we no longer need. It permanently removes the specified items.

Example: gcloud storage rm gs://my-bucket/old-file.txt

gcloud storage buckets create

This command creates a new storage bucket in Google Cloud. We use it to create a container for our data and files. It sets up a bucket with a globally unique name.

Example: gcloud storage buckets create gs://my-unique-bucket-name

gcloud storage buckets list

This command lists all storage buckets in our current project. We use it to see where our data is stored. It displays a list of bucket names and locations.

Example: gcloud storage buckets list

gcloud storage buckets update

This command updates the properties of an existing storage bucket. We use it to change settings like lifecycle rules or labels. It applies the new configuration to the bucket.

Example: gcloud storage buckets update gs://my-bucket --uniform-bucket-level-access

gcloud storage buckets delete

This command deletes a storage bucket and its contents. We use it to remove a bucket permanently. It will fail if the bucket still contains files unless we force it.

Example: gcloud storage buckets delete gs://my-old-bucket

gcloud storage objects list

This command lists all objects inside a specific bucket. We use it to see all files stored in a particular location. It provides a detailed view of our data.

Example: gcloud storage objects list gs://my-bucket

Kubernetes Engine (GKE)

Managing containers is easy with the right tools. These gcloud commands help us control our Kubernetes clusters. These Cloud gcloud commands are essential for deploying and scaling containerised applications.

gcloud container clusters create

This command creates a new Google Kubernetes Engine cluster. We use it to set up the environment for our container applications. It provisions the control plane and node instances.

Example: gcloud container clusters create my-cluster --num-nodes=3

gcloud container clusters list

This command lists all GKE clusters in our project. We use it to see the status and versions of our clusters. It displays details like the zone and node count.

Example: gcloud container clusters list

gcloud container clusters get-credentials

This command fetches the credentials for a specific cluster and configures kubectl. We use it to connect our local machine to the cluster. It allows us to manage the cluster with standard Kubernetes tools.

Example: gcloud container clusters get-credentials my-cluster --zone=us-central1-a

gcloud container clusters resize

This command changes the number of nodes in a specific cluster. We use it to scale our cluster up or down based on demand. It adds or removes compute resources.

Example: gcloud container clusters resize my-cluster --size=5 --zone=us-central1-a

gcloud container clusters delete

This command deletes a Kubernetes Engine cluster. We use it to remove a cluster we no longer need. It deletes the control plane and all node resources.

Example: gcloud container clusters delete my-cluster --zone=us-central1-a

gcloud container node-pools create

This command creates a new node pool within an existing cluster. We use it to run different types of workloads in the same cluster. It adds a set of nodes with specific configurations.

Example: gcloud container node-pools create my-pool --cluster=my-cluster

gcloud container node-pools delete

This command deletes a node pool from a cluster. We use it to remove a specific group of nodes. It drains the nodes and removes them from the cluster.

Example: gcloud container node-pools delete my-pool --cluster=my-cluster

Cloud SQL Databases

Databases are the heart of many applications. These gcloud commands help us manage Cloud SQL instances. We use these Cloud gcloud commands to create and maintain our database servers.

gcloud sql instances create

This command creates a new Cloud SQL instance. We use it to set up a managed database server like MySQL or PostgreSQL. It provisions a fully managed database service.

Example: gcloud sql instances create my-db --database-version=POSTGRES_14 --tier=db-f1-micro

gcloud sql instances list

This command lists all Cloud SQL instances in the project. We use it to check the status and settings of our databases. It shows instance names and their current states.

Example: gcloud sql instances list

gcloud sql instances patch

This command updates the settings of an existing Cloud SQL instance. We use it to change properties like the machine type or activation policies. It applies the changes to the database server.

Example: gcloud sql instances patch my-db --tier=db-n1-standard-2

gcloud sql instances delete

This command deletes a Cloud SQL instance. We use it to remove a database server permanently. It also deletes all automated backups associated with the instance.

Example: gcloud sql instances delete my-db

gcloud sql databases create

This command creates a new database inside a Cloud SQL instance. We use it to add a specific database schema to our server. It prepares the space for our data tables.

Example: gcloud sql databases create my-data-db --instance=my-db

gcloud sql users create

This command creates a new user for a Cloud SQL instance. We use it to grant access to developers or applications. It sets up a username and password for authentication.

Example: gcloud sql users create my-user --instance=my-db --password=pass123

gcloud sql backups delete

This command deletes a specific backup of a Cloud SQL instance. We use it to clean up old manual backups. It removes the backup file from storage.

Example: gcloud sql backups delete 20230101 --instance=my-db

gcloud sql connect

This command opens a client connection to a Cloud SQL instance. We use it to run SQL queries directly against the database. It provides an interactive prompt.

Example: gcloud sql connect my-db --user=root

App Engine and Cloud Functions

Serverless computing allows us to focus on code. These gcloud commands help us deploy applications and functions. These Cloud gcloud commands are vital for modern, event-driven development.

gcloud app deploy

This command deploys an application to App Engine. We use it to upload our code and make it live on the web. It builds and deploys the application to the specified service.

Example: gcloud app deploy app.yaml

gcloud app browse

This command opens the default web browser to our application URL. We use it to quickly view the running application. It launches the site in a new tab.

Example: gcloud app browse

gcloud app describe

This command displays details about the current App Engine deployment. We use it to see the configuration and serving status. It shows information about the app service.

Example: gcloud app describe

gcloud app versions list

This command lists all versions of the App Engine application. We use it to see the deployment history and active versions. It displays version IDs and traffic splits.

Example: gcloud app versions list

gcloud app services delete

This command deletes a specific service from App Engine. We use it to remove a microservice that is no longer needed. It stops the service and removes its versions.

Example: gcloud app services delete my-service

gcloud functions deploy

This command deploys a function to Cloud Functions. We use it to upload code that triggers in response to events. It sets up the runtime and trigger for the function.

Example: gcloud functions deploy my-function --runtime=nodejs16 --trigger-http

gcloud functions describe

This command shows the configuration and status of a deployed function. We use it to verify settings or find the trigger URL. It outputs details like the runtime and memory allocation.

Example: gcloud functions describe my-function --region=us-central1

gcloud functions call

This command invokes a deployed Cloud Function directly. We use it to test the function or trigger it manually. It sends a data payload to the function.

Example: gcloud functions call my-function --data='{"message":"Hello"}'

gcloud functions delete

This command deletes a Cloud Function. We use it to remove functions we no longer need. It cleans up the resources used by the function.

Example: gcloud functions delete my-function --region=us-central1

gcloud functions list

This command lists all Cloud Functions in the project. We use it to get an overview of all deployed functions. It shows their names, regions, and status.

Example: gcloud functions list

Networking and VPC

Connecting resources securely is a priority. These gcloud commands manage our virtual networks and firewalls. We use these Cloud gcloud commands to control traffic flow and network access.

gcloud compute networks create

This command creates a new Virtual Private Cloud network. We use it to define an isolated network environment for our resources. It sets up the network where our VMs will communicate.

Example: gcloud compute networks create my-vpc --subnet-mode=custom

gcloud compute networks subnets create

This command creates a subnet within a VPC network. We use it to define IP ranges for specific regions. It partitions the network into smaller, manageable segments.

Example: gcloud compute networks subnets create my-subnet --network=my-vpc --region=us-central1 --range=10.0.0.0/24

gcloud compute firewall-rules create

This command creates a firewall rule to allow or deny traffic. We use it to control which ports and protocols are accessible. It secures our instances by restricting network access.

Example: gcloud compute firewall-rules create allow-ssh --allow=tcp:22

gcloud compute firewall-rules list

This command lists all firewall rules in the project or network. We use it to audit our current security settings. It displays allowed ports and source IP ranges.

Example: gcloud compute firewall-rules list

gcloud compute router-nats create

This command creates a Cloud NAT gateway. We use it to allow private instances to reach the internet. It provides outbound connectivity without giving instances public IPs.

Example: gcloud compute router-nats create my-nat --router=my-router --auto-allocate-nat-external-ips

gcloud compute addresses create

This command reserves a static external or internal IP address. We use it when we need a permanent IP for our service. It ensures the IP address does not change.

Example: gcloud compute addresses create my-ip --region=us-central1

gcloud compute routers create

This command creates a Cloud Router. We use it for dynamic routing and to manage NAT gateways. It exchanges routing information with our network.

Example: gcloud compute routers create my-router --network=my-vpc --region=us-central1

IAM and Security

Security is paramount in the cloud. These gcloud commands manage permissions and policies. These Cloud gcloud commands help us control who can access our resources and what they can do.

gcloud projects get-iam-policy

This command gets the Identity and Access Management policy for a project. We use it to see who has access to the project. It outputs a list of roles and members.

Example: gcloud projects get-iam-policy my-project

gcloud projects add-iam-policy-binding

This command adds a permission binding to the project IAM policy. We use it to grant a specific role to a user or service account. It updates the access permissions.

Example: gcloud projects add-iam-policy-binding my-project --member='user:[email protected]' --role='roles/editor'

gcloud projects remove-iam-policy-binding

This command removes a permission binding from the project IAM policy. We use it to revoke access for a specific member. It removes the specified role.

Example: gcloud projects remove-iam-policy-binding my-project --member='user:[email protected]' --role='roles/editor'

gcloud iam service-accounts create

This command creates a new service account. We use it to establish an identity for applications or services. It provides a way for apps to authenticate with Google Cloud.

Example: gcloud iam service-accounts create my-sa --display-name="My Service Account"

gcloud iam service-accounts list

This command lists all service accounts in the project. We use it to find the account we need to work with. It shows email addresses and unique IDs.

Example: gcloud iam service-accounts list

gcloud iam service-accounts keys create

This command creates a key for a service account. We use it to generate a JSON key file for authentication. It allows external applications to act as the service account.

Example: gcloud iam service-accounts keys create key.json [email protected]

gcloud iam service-accounts delete

This command deletes a service account. We use it to remove an account that is no longer in use. It permanently revokes access associated with that account.

Example: gcloud iam service-accounts delete [email protected]

gcloud iam roles list

This command lists all custom or predefined IAM roles. We use it to find the correct role to assign to users. It displays the role IDs and titles.

Example: gcloud iam roles list

gcloud kms keys create

This command creates a new key in Cloud Key Management Service. We use it to manage cryptographic keys for encryption. It sets up a key to protect our sensitive data.

Example: gcloud kms keys create my-key --keyring=my-keyring --location=global --purpose=encryption

gcloud kms keys list

This command lists keys within a specific key ring. We use it to see the encryption keys available to us. It shows key names and their states.

Example: gcloud kms keys list --keyring=my-keyring --location=global

Logging, Monitoring, and Debugging

Observability helps us maintain healthy systems. These gcloud commands give us insight into our applications. These Cloud gcloud commands are essential for troubleshooting and performance monitoring.

gcloud logging read

This command reads log entries from Cloud Logging. We use it to search through our application and system logs. It displays log lines that match our filter criteria.

Example: gcloud logging read "resource.type=gce_instance"

gcloud logging buckets create

This command creates a log sink bucket. We use it to route logs to specific storage locations. It organizes log data for retention or analysis.

Example: gcloud logging buckets create my-logs-bucket --location=global

gcloud monitoring policies create

This command creates an alerting policy for Cloud Monitoring. We use it to get notified when specific conditions occur. It defines the rules for triggering alerts.

Example: gcloud monitoring policies create --policy-from-file=policy.yaml

gcloud debug logpoints create

This command inserts a dynamic logpoint into our running application. We use it to add logging without redeploying code. It captures local variables when the code reaches that point.

Example: gcloud debug logpoints create my-module:my-line --expression="x"

gcloud app logs tail

This command streams the application logs to the console. We use it to watch log events in real time. It helps us debug issues as they happen.

Example: gcloud app logs tail

gcloud logging metrics create

This command creates a log-based metric. We use it to turn log entries into numeric data points. It allows us to chart and alert on log content.

Example: gcloud logging metrics create my-metric --log-filter='severity>=ERROR'

Utility and Component Management

These commands help us maintain the gcloud tool itself. These gcloud commands ensure our CLI is up to date and has the necessary components. We use these Cloud gcloud commands to keep our environment healthy.

gcloud topic formats

This command displays information about output formats like JSON or YAML. We use it to understand how to format command output. It helps us parse the results of other commands.

Example: gcloud topic formats

gcloud help

This command displays help information about gcloud or specific commands. We use it when we forget how to use a feature. It provides usage details and available flags.

Example: gcloud help compute instances create

gcloud version

This command reports the version of the Google Cloud SDK. We use it to check if we need to update the tool. It prints the current version number.

Example: gcloud version

gcloud components list

This command lists all installed and available components of the SDK. We use it to see what tools we have and what we can add. It shows the status of each component.

Example: gcloud components list

gcloud components update

This command updates the Google Cloud SDK to the latest version. We use it to ensure we have the newest features and fixes. It downloads and installs the latest release.

Example: gcloud components update

gcloud components install

This command installs a specific component like the kubectl or docker emulator. We use it to add functionality to our existing SDK. It downloads the required packages.

Example: gcloud components install kubectl

gcloud source repos clone

This command clones a Cloud Source Repository to our local machine. We use it to download the source code for our project. It creates a local git repository.

Example: gcloud source repos clone my-repo

gcloud builds submit

This command submits a build to Cloud Build. We use it to compile and test our code on Google Cloud. It uploads the source code and starts the build process.

Example: gcloud builds submit --tag gcr.io/my-project/my-image

gcloud scheduler jobs create

This command creates a scheduled job using Cloud Scheduler. We use it to run tasks at specific times, like cron jobs. It sets up the schedule and target for the job.

Example: gcloud scheduler jobs create http my-job --schedule="* * * * *" --uri="https://example.com/webhook"

gcloud tasks queues create

This command creates a task queue in Cloud Tasks. We use it to manage asynchronous workloads. It defines how tasks are processed and the rate of execution.

Example: gcloud tasks queues create my-queue

Conclusion

I hope that the way I’ve explained each gcloud CLI command with details and examples helps you remember them, build strong real-world cloud management skills, and manage your infrastructure with ease.

You can also check out this article for the best cloud deployment practices:
5 Cloud Deployment Best Practices You Should Know About

Reference:
https://docs.cloud.google.com/sdk/gcloud

Aditya Gupta
Aditya Gupta
Articles: 485
Review Your Cart
0
Add Coupon Code
Subtotal