CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Cloud CLIs / gcloud

gcloud

Manage Google Cloud with the gcloud CLI

Intermediategcpcloudgke

gcloud is Google Cloud's CLI: authenticate, choose a project and default zone, then manage Compute Engine VMs, GKE clusters, Cloud Storage, IAM and more. Named configurations switch between projects cleanly, and --filter/--format shape its output.

Syntaxgcloud [GROUP...] COMMAND [--project P] [--zone Z] [--filter EXPR] [--format FORMAT]

Watch it run

You work in a lab project and a production project and never want to run a lab command against production by accident.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
gcloud compute instances list

No credentials in the active configuration. gcloud auth login opens a browser (or prints a URL with --no-launch-browser) and stores a token for the CLI.

Anatomy of a command

$
commandgcloud

The Google Cloud CLI, part of the Cloud SDK.

How it works

gcloud keeps its state in a configuration directory (~/.config/gcloud): named configurations with properties such as core/account, core/project and compute/zone, plus OAuth credentials from gcloud auth login (or a service account, or the VM's attached service account through the metadata server). Each command calls a Google API over HTTPS, for example compute.googleapis.com, with an OAuth access token, and Google checks IAM role bindings on the project, folder or organisation. APIs must be enabled per project first, otherwise calls fail with an 'API has not been used in project' error. Note that gcloud auth login authorises the CLI itself, while gcloud auth application-default login creates separate credentials for SDKs and tools such as Terraform. --filter and --format are gcloud's own projection language, not JMESPath.

Options you'll actually use

auth login / auth listAuthorise the CLI as a user; show credentialed accounts
auth application-default loginCredentials for SDKs and Terraform (ADC), separate from the CLI's
config set KEY VALUESet defaults like project or compute/zone in the active configuration
config configurationscreate, activate, list: named configurations per project/environment
--project / --zone / --regionPer-command overrides
--filter EXPRFilter resources, e.g. status=RUNNING AND labels.env=prod
--format FORMATOutput shape: json, yaml, table(...), value(...)
compute ssh VM --zone ZSSH with automatic key management (or IAP tunnelling)
clusters get-credentialscontainer clusters get-credentials: add a GKE cluster to ~/.kube/config
services enable APIEnable an API (compute, container...) in the project

Common mistakes

Relying on gcloud auth login for Terraform or an SDK

That login authorises only the gcloud CLI; libraries look for Application Default Credentials.

gcloud auth application-default login locally; a service account or workload identity in automation
Downloading service account JSON keys for CI

Long-lived private keys that are copied around and rarely rotated.

Workload Identity Federation (OIDC) for CI, attached service accounts for VMs, Workload Identity for GKE
Switching projects with gcloud config set project back and forth

Easy to forget which project is active and run a command in the wrong one.

Named configurations per environment, or --project on every command in scripts

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    No gcloud in the sandbox. gcloud's value() format prints tab-separated fields; produce the same shape from ~/data/servers.txt: name and IP of running servers, tab-separated.

  2. 2

    Write the command that lists only the names of RUNNING instances labelled env=prod.

  3. 3

    Write the commands to create and switch to a configuration named lab using project devops-devops-lab and zone us-central1-a.

Related commands

azManage Azure resources with the Azure CLIaws ec2Find, start and stop EC2 instances from the CLIkubectl configManage clusters, users and contexts in kubeconfigaws s3Copy, sync and list files in Amazon S3
PreviousazNextterraform init
Linux / Cloud CLIs / gcloud

gcloud

Manage Google Cloud with the gcloud CLI

Intermediategcpcloudgke

gcloud is Google Cloud's CLI: authenticate, choose a project and default zone, then manage Compute Engine VMs, GKE clusters, Cloud Storage, IAM and more. Named configurations switch between projects cleanly, and --filter/--format shape its output.

Syntaxgcloud [GROUP...] COMMAND [--project P] [--zone Z] [--filter EXPR] [--format FORMAT]

Watch it run

You work in a lab project and a production project and never want to run a lab command against production by accident.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
gcloud compute instances list

No credentials in the active configuration. gcloud auth login opens a browser (or prints a URL with --no-launch-browser) and stores a token for the CLI.

Anatomy of a command

$
commandgcloud

The Google Cloud CLI, part of the Cloud SDK.

How it works

gcloud keeps its state in a configuration directory (~/.config/gcloud): named configurations with properties such as core/account, core/project and compute/zone, plus OAuth credentials from gcloud auth login (or a service account, or the VM's attached service account through the metadata server). Each command calls a Google API over HTTPS, for example compute.googleapis.com, with an OAuth access token, and Google checks IAM role bindings on the project, folder or organisation. APIs must be enabled per project first, otherwise calls fail with an 'API has not been used in project' error. Note that gcloud auth login authorises the CLI itself, while gcloud auth application-default login creates separate credentials for SDKs and tools such as Terraform. --filter and --format are gcloud's own projection language, not JMESPath.

Options you'll actually use

auth login / auth listAuthorise the CLI as a user; show credentialed accounts
auth application-default loginCredentials for SDKs and Terraform (ADC), separate from the CLI's
config set KEY VALUESet defaults like project or compute/zone in the active configuration
config configurationscreate, activate, list: named configurations per project/environment
--project / --zone / --regionPer-command overrides
--filter EXPRFilter resources, e.g. status=RUNNING AND labels.env=prod
--format FORMATOutput shape: json, yaml, table(...), value(...)
compute ssh VM --zone ZSSH with automatic key management (or IAP tunnelling)
clusters get-credentialscontainer clusters get-credentials: add a GKE cluster to ~/.kube/config
services enable APIEnable an API (compute, container...) in the project

Common mistakes

Relying on gcloud auth login for Terraform or an SDK

That login authorises only the gcloud CLI; libraries look for Application Default Credentials.

gcloud auth application-default login locally; a service account or workload identity in automation
Downloading service account JSON keys for CI

Long-lived private keys that are copied around and rarely rotated.

Workload Identity Federation (OIDC) for CI, attached service accounts for VMs, Workload Identity for GKE
Switching projects with gcloud config set project back and forth

Easy to forget which project is active and run a command in the wrong one.

Named configurations per environment, or --project on every command in scripts

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    No gcloud in the sandbox. gcloud's value() format prints tab-separated fields; produce the same shape from ~/data/servers.txt: name and IP of running servers, tab-separated.

  2. 2

    Write the command that lists only the names of RUNNING instances labelled env=prod.

  3. 3

    Write the commands to create and switch to a configuration named lab using project devops-devops-lab and zone us-central1-a.

Related commands

azManage Azure resources with the Azure CLIaws ec2Find, start and stop EC2 instances from the CLIkubectl configManage clusters, users and contexts in kubeconfigaws s3Copy, sync and list files in Amazon S3
PreviousazNextterraform init