This blog shows you how to create instances or virtual machines in Google Compute Engine (GCE) using the command line interface.

# Authenticate – Please authenticate first before you go any further

gcloud auth login

# Set project

gcloud config set project PROJECT

# Get a list of images we can use

gcloud compute images list

# Get a list of machine types we can use

gcloud compute machine-types list

# Get a list of networks we can use

gcloud compute networks list

# Get a list of regions we can use

gcloud compute regions list

# Get a list of zones we can use

gcloud compute zones list

# Get a list of diskt types

gcloud compute disk-types list

# Create a startup script to install Apache

echo "yum -y install httpd && hostname > /var/www/html/index.html" >  $HOME/lb_startup.sh

# Finally create the instance

gcloud compute instances create web1 --image centos-6 --zone us-central1-a --boot-disk-size 200GB --boot-disk-type pd-ssd --description 'web server1' --machine-type n1-standard-4 --network default  --tags fe-web --metadata-from-file startup-script=$HOME/lb_startup.sh

# Create another instance in another zone

gcloud compute instances create web2 --image centos-6 --zone us-central1-b --boot-disk-size 200GB --boot-disk-type pd-ssd --description 'web server2' --machine-type n1-standard-4 --network default  --tags fe-web --metadata-from-file startup-script=$HOME/lb_startup.sh

# Verify instances have been created

gcloud compute instances list

# Get details about web1 instance

gcloud compute instances describe web1 --zone us-central1-a

# Check the SSH connection to it

gcloud compute ssh web1 --zone us-central1-a
gcloud compute ssh web2 --zone us-central1-b

# Check the firewall rules

gcloud compute firewall-rules list

# Allow HTTP into the instances

gcloud compute firewall-rules create http --allow tcp:80 tcp:443 --target-tags fe-web --network default

# Check to make sure you can access index.html


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s