Skip to content

Instantly share code, notes, and snippets.

@stojan211287
Last active February 14, 2020 22:15
Show Gist options
  • Save stojan211287/c1f1c89e8091a8f27cf9b84cbb8daeed to your computer and use it in GitHub Desktop.
Save stojan211287/c1f1c89e8091a8f27cf9b84cbb8daeed to your computer and use it in GitHub Desktop.
Basic Cloud Load balancing using an HTTP proxy

GCP Scaling and Load Balancing

Tutorial

https://codelabs.developers.google.com/codelabs/gcp-aws-scaling-and-balancing/index.html?index=..%2F..index#0

TODO List

* Create custom image (optional)
* Create Instance Template
* Create Managed Instance Group
* Distribute instances using regional managed instance groups
* Setup Load Balancing 

Create custom VM image from previous disk snapshot

gcloud beta compute images create my-vm-image \
            --source-snapshot=mss-with-apache2 \
            --storage-location=europe-west1

glcoud compute images describe my-vm-image

Create Instance Template (from own 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

Create Managed Instance Group from Instance 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

Load balance between two instances

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment