* Create custom image (optional)
* Create Instance Template
* Create Managed Instance Group
* Distribute instances using regional managed instance groups
* Setup Load Balancing
gcloud beta compute images create my-vm-image \
--source-snapshot=mss-with-apache2 \
--storage-location=europe-west1
glcoud compute images describe my-vm-image
gcloud compute instance-templates create my-vm-template \
--machine-type=f1-micro \
--image=my-vm-image \
--image-project=$(gcloud config get-value project) \
--boot-disk-size 200GB \
--tags=http-server # firewall allows traffic to port 80
- Now, see your instance template with
gcloud compute instance-templates describe my-vm-template
gcloud compute instance-groups managed create my-mig \
--template=my-vm-template \
--size=2 \
--zones=europe-west1-b,europe-west1-c\
--region=europe-west1 \
--base-instance-name=my-test-vm
- Set managed instance group to be auto-scaled, such that at least one (and at most one) instance is available
gcloud compute instance-groups managed set-autoscaling my-mig \
--max-num-replicas=1 \
--min-num-replicas=1 \
--region=europe-west1
- One of the instances should shut down. Now, shut down the other instance
gcloud compute instance-groups managed delete-instances my-mig
--instances=my-test-vm-qddm \
--region=europe-west1
- List instances
gcloud compute instances list
You should see another instance being provisioned!
- Delete the managed instance group
gcloud compute instance-groups managed delete my-mig \
--region=europe-west1
- Make sure there are no instances running
gcloud compute instances list
First, read up on load balancing https://cloud.google.com/load-balancing/docs/network/
Based on https://cloud.google.com/load-balancing/docs/network/setting-up-network
- First, spin up managed instance group of two instances again
gcloud compute instances-groups managed create my-mig \
--template=my-vm-template \
--size=2 \
--zones=europe-west1-b,europe-west1-c\
--region=europe-west1 \
--base-instance-name=my-test-vm
- I have made my instance template with the "http-server" tag, but another way to open port 80 on your machines is to set a firewall rule
gcloud compute firewall-rules create www-firewall-network-lb \
--target-tags network-lb-tag \
--allow tcp:80
- Next, create an external IP for your load balancer to route from
gcloud compute addresses create network-my-vm \
--region=europe-west1
- Check it is there by running
gcloud compute addresses list
- Create an HTTP health check for the load balancer to determine if instances are running or not
gcloud compute http-health-checks create basic-check
- Create a target pool
gcloud compute target-pools create my-vm-pool \
--region=europe-west1 \
--http-health-check=basic-check
- Add my managed group to target pool
gcloud compute instance-groups managed set-target-pools my-mig \
--target-pools my-vm-pool \
--region=europe-west1
- Finally, add a forwarding rule to your external IP address so it sends requests to your target pull, full of your managed instances
gcloud compute forwarding-rules create my-vm-rule \
--region=europe-west1 \
--ports=80 \
--address network-my-vm \
--target-pool my-vm-pool
- After you're done testing, DELETE EVERYTHING
gcloud compute instance-groups delete my-mig
gcloud compute addresses delete network-my-vm \
--region=europe-west1
gcloud compute forwarding-rules delete my-vm-rule \
--region=europe-west1
gcloud compute target-pools delete my-vm-pool \
--region=europe-west1
gcloud compute http-health-checks delete basic-check