Google Compute HTTP Load Balancer

HTTP LOAD BALANCING

# Create two instances in Europe

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

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

# Create two instances in US

$ 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

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

# Create an instance group in europe, for one zone

$ gcloud preview instance-groups --zone europe-west1-b create eu-b-resources

# Create an instance group in US, for one zone

$ gcloud preview instance-groups --zone us-central1-f create us-f-resources

# Add VMs to both instance groups

$ gcloud preview instance-groups --zone europe-west1-b instances --group eu-b-resources add web5 web6
$ gcloud preview instance-groups --zone us-central1-f instances --group us-f-resources add web2 web4

# Add service mapping for port 80 to the instance group

$ gcloud preview instance-groups --zone us-central1-f add-service us-f-resources --port 80 --service http
$ gcloud preview instance-groups --zone europe-west1-b add-service eu-b-resources --port 80 --service http

# Create a backend service, with the defaul 80% CPU itilization metric

$ gcloud compute backend-services create web-service --http-health-check basic-check

# Add instance groups to the backend service

$ gcloud compute backend-services add-backend web-service --group eu-b-resources --zone europe-west1-b
$ gcloud compute backend-services add-backend web-service --group us-f-resources --zone us-central1-f

# Create a URL map to direct traffic

$ gcloud compute url-maps create web-map --default-service web-service

# Create target http proxy

$ gcloud compute target-http-proxies create web-proxy --url-map web-map

# Create a global forwarding rule, NOTE THE IP ADDRESS PRINTED

$ gcloud compute forwarding-rules create http-rule --global --target-http-proxy web-proxy --port-range 80

# Check the health of the backend-service, you will have to wait several minutes for the health to show up

$ gcloud compute backend-services get-health

# Once the health shows ok, check the URL, replace $IP with the IP address of the forwarding rule

$ while true; do curl -m1 $IP; done

Leave a comment