Cheat Sheet

Google Cloud Platform tips and tricks.

gcloud

Other Cheat Sheets

https://cloud.google.com/sdk/gcloud/reference

http://cheat.sh/gcloud

https://gist.github.com/pydevops/cffbd3c694d599c6ca18342d3625af97#file-gcloud_cheat_sheet-md

Install

Download the latest version: https://cloud.google.com/sdk/docs/install

echo "export CLOUDSDK_PYTHON=$(which python3)" >> ~/.zshrc
source ~/.zshrc
./install.sh

Install/remove components

gcloud components install COMPONENT_ID
gcloud components remove COMPONENT_ID

Config

Change project default Project

gcloud config set project my-project

You may also set the environment variable CLOUDSDK_CORE_PROJECT.

Change project default Compute Engine Region

gcloud config set compute/region NAME

Change project default Compute Engine Zone

gcloud config set compute/zone NAME

Env vars

CLOUDSDK_COMPUTE_REGION=us-central1
CLOUDSDK_COMPUTE_ZONE=us-central1-a
CLOUDSDK_CORE_PROJECT=my-project-123456
CLOUDSDK_CONTAINER_CLUSTER=cluster-1

Show current project

gcloud config list --format 'value(core.project)'

List config

gcloud config configurations list

Compute

SSH to VM

gcloud compute --project "proj-id-253611" ssh --zone us-central1-a instance-1

Transfer files to VM

https://cloud.google.com/compute/docs/instances/transfer-files/

Choosing a load balancer

https://cloud.google.com/load-balancing/docs/choosing-load-balancer

https://cloud.google.com/load-balancing/docs/https/setting-up-https

Delete VM

# Make sure you are using the correct account
gcloud config list

# Display instance info
gcloud compute instances describe instance-1 --zone=us-central1-a --project=proj-id-253611gcloud compute instances describe instance-1 --zone=us-central1-a --project=proj-id-253611

# Delete instanace and ALL DISKS
gcloud compute instances delete instance-1 --zone=us-central1-a --project=proj-id-253611 --delete-disks=all

Kubernetes

Get credentials

gcloud container clusters get-credentials cluster-1 --zone us-central1-c --project proj-id-253611

Deploy

git clone \
    https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
    
cd \
    kubernetes-engine-samples/hello-app
    

docker build -t \
    gcr.io/united-blend-253611/hello-app:v1 \
    $PWD
    
gcloud docker -- push \
    gcr.io/united-blend-253611/hello-app:v1
    
kubectl create deployment \
    hello-app \
    --image=gcr.io/united-blend-253611/hello-app:v1
    
kubectl expose deployment \
    hello-app \
    --type="LoadBalancer" --port \
    8080
    
kubectl get service hello-app \
    --watch

Mount file system over SSH

# Create SSH tunnel
gcloud compute ssh your-instance-name --project=your-project-name -- -NL 9022:127.0.0.1:22

# Mount remote folder locally
sshfs -odebug,sshfs_debug,loglevel=debug,ServerAliveInterval=30,IdentityFile=~/.ssh/google_compute_engine your-username@localhost:/remote/folder/path /local/folder/path -p 9022

# Mount local folder remotelly
sshfs -odebug,sshfs_debug,loglevel=debug,ServerAliveInterval=30,IdentityFile=~/.ssh/google_compute_engine /local/folder/path your-username@localhost:/remote/folder/path -p 9022

rsync

# Openn SSH tunnel
gcloud compute ssh your-instance --project=your-project -- -NL 9022:127.0.0.1:22

# Rsync from local to remote
rsync --progress --checksum -a -e "ssh -p 9022 -o IdentityFile=~/.ssh/google_compute_engine" /local/path your-user@localhost:/remote/path/

Last updated