Overview
The gcloud CLI is the primary command-line tool for interacting with Google Cloud Platform services. This guide covers the most commonly used commands for managing GKE clusters and GCP projects.
Installation
macOS
# Using Homebrew
brew install --cask google-cloud-sdk
Linux
# Download and install
curl https://sdk.cloud.google.com | bash
# Restart your shell
exec -l $SHELL
# Initialize gcloud
gcloud init
Windows
Download and run the Google Cloud SDK installer .
Installation Resources
Install gcloud CLI Official installation guide for all platforms
Install kubectl Set up kubectl for GKE access
Initial Setup
1. Authenticate with Google Cloud
Log in to your Google Cloud account:
This opens your browser to complete the OAuth flow.
You’ll be prompted to authorize gcloud to access your Google Cloud resources. Make sure you’re logged in with the correct Google account.
2. Initialize gcloud
Set up your default configuration:
This interactive command will:
Select an existing configuration or create a new one
Choose your Google account (if you have multiple)
Select or create a project
Set default compute region/zone (recommended: us-central1-a for general use)
Example output:
Your current project is [my-project-123]. Choose a project:
[1] my-project-123
[2] another-project-456
[3] Create a new project
Please enter your numeric choice: 1
Your project default compute zone has been set to [us-central1-a].
3. Verify Setup
Check your current configuration:
Expected output:
[core]
account = [email protected]
project = my-project-123
[compute]
region = us-central1
zone = us-central1-a
Project Management
List All Projects
View all projects you have access to:
Get Current Project
gcloud config get-value project
Switch Projects
Change to a different project:
gcloud config set project [YOUR_PROJECT_ID]
Example:
gcloud config set project exchange-production-001
Pro Tip: Create separate configurations for different projects or environments using gcloud config configurations create [NAME]. This lets you quickly switch entire contexts.
Create a New Project
gcloud projects create [PROJECT_ID] --name = "Display Name"
Example:
gcloud projects create exchange-staging-001 --name= "Exchange Staging"
Kubernetes (GKE) Commands
List Clusters
View all GKE clusters in your current project:
gcloud container clusters list
Example output:
NAME LOCATION MASTER_VERSION NUM_NODES STATUS
exchange-cluster us-central1-a 1.27.3-gke.100 3 RUNNING
Get Cluster Credentials
Configure kubectl to access a GKE cluster:
gcloud container clusters get-credentials [CLUSTER_NAME] --location [LOCATION]
Example:
gcloud container clusters get-credentials exchange-cluster --location us-central1-a
This command:
Fetches cluster endpoint and authentication data
Adds cluster to your kubeconfig (~/.kube/config)
Sets the current kubectl context to this cluster
After running this command, all kubectl commands will target this cluster until you change context.
Describe a Cluster
Get detailed information about a cluster:
gcloud container clusters describe [CLUSTER_NAME] --location [LOCATION]
kubectl Context Management
After getting cluster credentials, manage your Kubernetes contexts:
View All Contexts
List all available Kubernetes contexts:
kubectl config get-contexts
Example output:
CURRENT NAME CLUSTER AUTHINFO
* gke_proj_us-central1-a_prod gke_proj_us-central1-a_prod gke_proj_us-central1-a_prod
kind-local kind-local kind-local
The * indicates your current context.
View Current Context
kubectl config current-context
Switch Contexts
Change to a different cluster:
kubectl config use-context [CONTEXT_NAME]
Examples:
# Switch to Kind local cluster
kubectl config use-context kind-local
# Switch to GKE production cluster
kubectl config use-context gke_my-project_us-central1-a_exchange-cluster
Be Careful: Always verify your current context before running kubectl commands, especially destructive ones. Use kubectl config current-context to confirm you’re targeting the right cluster.
Common Configuration Commands
Set Default Region
gcloud config set compute/region us-central1
Set Default Zone
gcloud config set compute/zone us-central1-a
View All Settings
Unset a Property
gcloud config unset compute/zone
Managing Multiple Configurations
Create a New Configuration
Useful for managing multiple projects/environments:
gcloud config configurations create [CONFIG_NAME]
Example:
gcloud config configurations create production
gcloud config configurations create staging
List All Configurations
gcloud config configurations list
Activate a Configuration
gcloud config configurations activate [CONFIG_NAME]
Example:
gcloud config configurations activate production
Configuration Example Workflow
# Create staging configuration
gcloud config configurations create staging
gcloud config set project exchange-staging-001
gcloud config set compute/zone us-central1-a
# Create production configuration
gcloud config configurations create production
gcloud config set project exchange-production-001
gcloud config set compute/zone us-east1-b
# Switch between environments
gcloud config configurations activate staging # Work on staging
gcloud config configurations activate production # Work on production
Authentication Commands
Login
List Active Account
Set Active Account
gcloud config set account [EMAIL]
Revoke Credentials
gcloud auth revoke [EMAIL]
Application Default Credentials
For applications using GCP APIs:
gcloud auth application-default login
Service Management
Enable APIs
Enable required Google Cloud APIs:
gcloud services enable container.googleapis.com
gcloud services enable compute.googleapis.com
List Enabled Services
gcloud services list --enabled
Disable a Service
gcloud services disable [SERVICE_NAME]
Cluster Operations
Create a Cluster
gcloud container clusters create [CLUSTER_NAME] \
--zone us-central1-a \
--num-nodes 3 \
--machine-type e2-standard-4
Delete a Cluster
gcloud container clusters delete [CLUSTER_NAME] --location [LOCATION]
This permanently deletes the cluster and all its resources. Always double-check before running this command.
Resize a Cluster
gcloud container clusters resize [CLUSTER_NAME] \
--num-nodes 5 \
--location [LOCATION]
Upgrade a Cluster
# Upgrade master
gcloud container clusters upgrade [CLUSTER_NAME] \
--master \
--cluster-version [VERSION] \
--location [LOCATION]
# Upgrade nodes
gcloud container clusters upgrade [CLUSTER_NAME] \
--location [LOCATION]
Node Pool Management
List Node Pools
gcloud container node-pools list --cluster [CLUSTER_NAME] --location [LOCATION]
Create Node Pool
gcloud container node-pools create [POOL_NAME] \
--cluster [CLUSTER_NAME] \
--machine-type e2-standard-4 \
--num-nodes 3 \
--location [LOCATION]
Delete Node Pool
gcloud container node-pools delete [POOL_NAME] \
--cluster [CLUSTER_NAME] \
--location [LOCATION]
Useful Shortcuts and Aliases
Add these to your .bashrc or .zshrc:
# gcloud shortcuts
alias gcl = 'gcloud container clusters list'
alias gcp = 'gcloud config get-value project'
alias gcs = 'gcloud config set project'
# kubectl shortcuts
alias k = 'kubectl'
alias kctx = 'kubectl config use-context'
alias kgc = 'kubectl config get-contexts'
# Combined workflows
function gke-switch () {
gcloud container clusters get-credentials $1 --location $2
}
# Usage: gke-switch exchange-cluster us-central1-a
Troubleshooting
Check gcloud Version
Update gcloud
View gcloud Info
Debug Mode
Run commands with verbose logging:
gcloud [COMMAND] --log-http --verbosity = debug
Clear Cached Credentials
rm -rf ~/.config/gcloud
gcloud auth login
gcloud init
Quick Reference Table
Task Command Login gcloud auth loginInitialize gcloud initSwitch project gcloud config set project [PROJECT_ID]List clusters gcloud container clusters listGet credentials gcloud container clusters get-credentials [CLUSTER] --location [LOCATION]View contexts kubectl config get-contextsSwitch context kubectl config use-context [CONTEXT]Current project gcloud config get-value projectCurrent context kubectl config current-context
Best Practices
Productivity Tips:
Use gcloud config configurations to manage multiple environments
Set up shell aliases for frequently used commands
Always verify your current project and context before operations
Use --project flag to override default project temporarily
Enable command completion: gcloud init --console-only
Safety Checklist:
Always run gcloud config get-value project before destructive operations
Use --dry-run flag when available
Keep gcloud CLI updated for security patches
Never commit service account keys to Git
Use separate projects for production and development
Next Steps
GKE Deployment Deploy a GKE cluster using these commands
Local Development Set up local Kind cluster
Additional Resources