GSP007
GSP007
(GSP007)
Overview
In this hands-on lab, you’ll learn the differences between a network load balancer
and a HTTP load balancer, and how to set them up for your applications running
on Google Compute Engine virtual machines.
There are several ways you can load balance in Google Cloud Platform. This
lab takes you through the setup of the following load balancers:
• L3 Network Load Balancer
• L7 HTTP(S) Load Balancer
What you’ll do
• Setup a network load balancer.
• Setup a HTTP(s) load balancer.
• Get hands-on experience learning the differences between network load
balancers and HTTP load balancers.
1
- An instance template to use the startup script - A target pool - A managed
instance group using the instance template
cat << EOF > startup.sh
#! /bin/bash
apt-get update
apt-get install -y nginx
service nginx start
sed -i -- 's/nginx/Google Cloud Platform - '"\$HOSTNAME"'/' /var/www/html/index.nginx-debian
EOF
gcloud compute instance-templates create nginx-template \
--metadata-from-file startup-script=startup.sh
gcloud compute target-pools create nginx-pool
gcloud compute instance-groups managed create nginx-group \
--base-instance-name nginx \
--size 2 \
--template nginx-template \
--target-pool nginx-pool
gcloud compute instances list
Now configure a firewall so that you can connect to the machines on port 80 via
EXTERNAL_IP addresses.
2
Create a HTTP(s) Load Balancer
HTTP(S) load balancing provides global load balancing for HTTP(S) requests
destined for your instances. You can configure URL rules that route some URLs
to one set of instances and route other URLs to other instances. Requests are
always routed to the instance group that is closest to the user, provided that
group has enough capaicty and is appropriate for the request. If the closest
group dosen not have enough capacity, the request is sent to the closest group
that does have capacity.
Health check:
gcloud compute http-health-checks create http-basic-check
Define an HTTP service and map a port name to the relevant port for the
instance group.
gcloud compute instance-groups managed \
set-named-ports nginx-group \
--named-ports http:80
Create a backend service:
gcloud compute backend-services create nginx-backend \
--protocol HTTP --http-health-checks http-basic-check --global
Add the instance group into the backend service:
gcloud compute backend-services add-backend nginx-backend \
--instance-group nginx-group \
--instance-group-zone us-central1-a \
--global
Create a default URL map that directs all incoming requests to all your instances:
gcloud compute url-maps create web-map \
--default-service nginx-backend
Create a target HTTP proxy to route requests to your URL map:
gcloud compute target-http-proxies create http-lb-proxy \
--url-map web-map
Create a global forwarding rule to handle and route incoming requests.
gcloud compute forwarding-rules create http-content-rule \
--global \
--target-http-proxy http-lb-proxy \
--ports 80
After creating the global forwarding rule, it can take several minutes for your
configuration to propagate.
gcloud compute forwarding-rules list