Google Cloud - Gcloud commands

 

Basic:

-List images for create VM:

# gcloud compute images list

-List zones for create VM:

# gcloud compute zones list

-Set default zone(europe-north1-a):

# gcloud config set compute/zone europe-north1-a

-Set default region(europe-north1):

# gcloud config set compute/region europe-north1

-List describe instances:

# gcloud compute instances describe test111

-Connect ssh from gcloud environment:

# gcloud compute ssh test111

 

Firewall:

-List all firewall rules:

# gcloud compute firewall-rules list

-Add firewall rule(open 22 port):

# gcloud compute firewall-rules create --network=NETWORK default-allow-ssh --allow tcp:22

 

Create:

-Help in create VM:

# gcloud compute instances create --help

-Interactive create VM:

# gcloud compute instances create test111

 

Machine-types

f1-micro - 1 shared vCPU, 0,6GB RAM

g1-small - 1 shared vCPU, 1,7GB RAM

n1-standart-1 - 1 vCPU, 3,75GB RAM

n1-standart-2 - 2 vCPUs, 7,5GB RAM

-Create server in Finland machine-type f1-micro(1 shared vCPU, 0,6GB RAM):

# gcloud compute instances create test111 --machine-type f1-micro --zone europe-north1-a --image-project ubuntu-os-cloud --image-family ubuntu-1604-lts

-Create 3 servers:

# gcloud compute instances create test111 test222 test333 --machine-type f1-micro --zone europe-north1-a --image-project ubuntu-os-cloud --image-family ubuntu-1604-lts

-Create VM from template “test-min”:

# gcloud compute instances create test111 --source-instance-template=test-min --zone europe-north1-a

-Create VM with settings from remote .sh script:

# gcloud compute instances create test111 --machine-type f1-micro --zone europe-north1-a --image-project ubuntu-os-cloud --image-family ubuntu-1604-lts --metadata startup-script-url=https://raw.githubusercontent.com/cp38510/scripts/master/google_cloud_test_settings.sh --metadata-from-file ssh-keys=/root/.ssh/id_rsa.pub

 

RSA-keys:

-Create VM from template “test-min” and add local rsa key. Before add we need change file with public key(/root/.ssh/id_rsa.pub) to:

# [USERNAME]:ssh-rsa [KEY_VALUE] [USERNAME]
after changes:
# gcloud compute instances create test111 --source-instance-template=test-min --metadata-from-file ssh-keys=/root/.ssh/id_rsa.pub

-Replace existing keys with new ones:

# gcloud compute instances add-metadata test111 --metadata-from-file ssh-keys=/root/.ssh/id_rsa.pub

If you need set few rsa key:

# nano all_keys.txt  # This is where the merged key list goes
# gcloud compute instances add-metadata test111 --metadata-from-file ssh-keys=all_keys.txt

-Add rsa key to all VM in project(new VM will contain it too):

# gcloud compute project-info add-metadata --metadata-from-file ssh-keys=/root/.ssh/id_rsa.pub

-Remove all rsa keys from project and VMs:

# gcloud compute project-info remove-metadata --all

 

Remove:

-Remove instance:

# gcloud compute instances delete test111

 

-Install Google Cloud SDK to local machine:

https://cloud.google.com/sdk/docs/quickstart-linux

https://cloud.google.com/compute/docs/api/how-tos/authorization

 

 

Sources:

https://cloud.google.com/sdk/gcloud/reference/compute/instances/create

https://cloud.google.com/compute/docs/instances/create-start-instance

https://cloud.google.com/compute/docs/instances/creating-instance-with-custom-machine-type

https://cloud.google.com/compute/docs/startupscript

https://cloud.google.com/compute/docs/machine-types

ssh keys:

https://cloud.google.com/compute/docs/instances/adding-removing-ssh-keys

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