Google Cloud Platform Cheat Sheet

< Blog

A quick reference to Google Cloud Platform.

Created on: 2019-12-05

Tag: cheat_sheet

Warning

under heavy construction and not well organized

install Google Cloud SDK or gcloud

For Ubuntu we can use apt 1:

echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
sudo apt-get install apt-transport-https ca-certificates gnupg
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
sudo apt-get update && sudo apt-get install google-cloud-sdk

Or we can use Docker 2:

sudo docker pull gcr.io/google.com/cloudsdktool/cloud-sdk:latest

To run like normal tool instead of using docker run, make an alias like:

alias gcloud_docker='sudo docker run gcr.io/google.com/cloudsdktool/cloud-sdk:latest gcloud $@'

See here for more Installation options.

start using the gcloud tool

to start using the gcloud tool:

gcloud init

Now follow alone the instructions to finish the setup.

uninstall Google Cloud SDK or gcloud

to uninstall Google Cloud SDK or gcloud:

sudo apt remove --purge google-cloud-sdk
sudo rm -rf $(gcloud info --format='value(config.paths.global_config_dir)';gcloud info --format='value(installation.sdk_root)')

sudo rm /etc/apt/sources.list.d/google-cloud-sdk.list
sudo rm /usr/share/keyrings/cloud.google.gpg

Google Compute Engine (GCE)

connect with ssh

to connect with ssh first add ssh key with gcloud 3:

gcloud compute --project "$PROJECT" ssh --zone "$ZONE" "$INSTANCE-NAME"

after that we can login with:

ssh $PUBLIC_IP

If we forget this command, we don't need to worry as it is available in the drop down list located beside the SSH button on the VM instances page.

copy file with scp

to copy file with scp from remote to local:

gcloud compute scp "$INSTANCE-NAME":/remote/path /local/path

to copy file with scp from local to remote:

gcloud compute scp /local/path "$INSTANCE-NAME":/remote/path

source: https://cloud.google.com/sdk/gcloud/reference/compute/scp

Google Container Registry (GCR)

push a docker image

to push a docker image, we need to tag an image 4:

sudo docker tag $IMAGE:$STAG $GCR_HOSTNAME/$PROJECT_ID/$IMAGE:$TAG

$GCR_HOSTNAME could be:

  • gcr.io

  • us.gcr.io

  • eu.gcr.io

  • asia.gcr.io

Choose the one closest to you. Now push the image:

sudo docker push $GCR_HOSTNAME/$PROJECT_ID/$IMAGE:$TAG

pull a docker image

To pull a docker image we need to configure our docker auth first 5:

gcloud auth configure-docker

then the pull is done usually 6:

sudo docker pull $GCR_HOSTNAME/$PROJECT_ID/$IMAGE:$TAG

OR:

sudo docker pull $GCR_HOSTNAME/$PROJECT_ID/$IMAGE@$IMAGE_DIGEST

Source

1

Quickstart for Debian and Ubuntu

2

Installing the Cloud SDK Docker image

3

Connecting to instances: Connecting to Linux instances

4

Pushing and pulling images: Pushing an image to a registry

5

Authentication methods: Authentication methods

6

Pushing and pulling images: Pulling images from a registry