Table of Content
Google cloud hands-on guide: Network and HTTP Load Balancers
qwiklabs GCP notes.
two type of load balancer
- L3 Network Load Balancer
- L7 HTTP(s) Load Balancer
Create multiple web server instances
create start script to setup nginx on each vm
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.html EOF
then create instance template using startup script
gcloud compute instance-templates create nginx-template \ --metadata-from-file startup-script=startup.sh Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-909dc6889861df15/global/instanceTemplates/nginx-template]. NAME MACHINE_TYPE PREEMPTIBLE CREATION_TIMESTAMP nginx-template n1-standard-1 2017-12-03T05:45:28.191-08:00
create target pool
allow to have a single access point to all instances in a group
gcloud compute target-pools create nginx-pool Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-909dc6889861df15/regions/us-central1/targetPools/nginx-pool]. NAME REGION SESSION_AFFINITY BACKUP HEALTH_CHECKS nginx-pool us-central1 NONE
create instance group using instance template
gcloud compute instance-groups managed create nginx-group \ --base-instance-name nginx \ --size 2 \ --template nginx-template \ --target-pool nginx-pool Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-909dc6889861df15/zones/us-central1-a/instanceGroupManagers/nginx-group]. NAME LOCATION SCOPE BASE_INSTANCE_NAME SIZE TARGET_SIZE INSTANCE_TEMPLATE AUTOSCALED nginx-group us-central1-a zone nginx 0 2 nginx-template no
verify by
gcloud compute instances list NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS nginx-c144 us-central1-a n1-standard-1 10.128.0.3 35.193.32.114 RUNNING nginx-z1mr us-central1-a n1-standard-1 10.128.0.2 35.184.53.44 RUNNING
config firewall rule
to open port 80 via external ip
gcloud compute firewall-rules create www-firewall --allow tcp:80 Creating firewall...\Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-909dc6889861df15/global/firewalls/www-firewall]. Creating firewall...done. NAME NETWORK DIRECTION PRIORITY ALLOW DENY www-firewall default INGRESS 1000 tcp:80
create a network load balancer
Network load balancing:
- balance the load of your systems based on incoming IP protocol data, such as address, port, and protocol type
- provide more protocol load balancer than HTTP(S) load balancer, such as TCP/UDP, to support SMTP traffic etc
steps:
create L3 network load balancer
gcloud compute forwarding-rules create nginx-lb \ --region us-central1 \ --ports=80 \ --target-pool nginx-pool Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-909dc6889861df15/regions/us-central1/forwardingRules/nginx-lb]. gcloud compute forwarding-rules list NAME REGION IP_ADDRESS IP_PROTOCOL TARGET nginx-lb us-central1 35.193.58.130 TCP us-central1/targetPools/nginx-pool
launch network load balancer
in browser http://35.193.58.130 Welcome to Google Cloud Platform - nginx-c144! If you see this page, the Google Cloud Platform - nginx-c144 web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com. Thank you for using Google Cloud Platform - nginx-c144.
create a HTTP(s) load balancer
- 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
steps:
create a health check
gcloud compute http-health-checks create http-basic-check Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-909dc6889861df15/global/httpHealthChecks/http-basic-check]. NAME HOST PORT REQUEST_PATH http-basic-check 80 /
define a HTTP service
map port name to relevant port for the instance group
gcloud compute instance-groups managed \ set-named-ports nginx-group \ --named-ports http:80 \ Updated [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-909dc6889861df15/zones/us-central1-a/instanceGroups/nginx-group].
create a backend service
gcloud compute backend-services create nginx-backend \ --protocol HTTP --http-health-checks http-basic-check --global Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-909dc6889861df15/global/backendServices/nginx-backend]. NAME BACKENDS PROTOCOL nginx-backend HTTP
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 Updated [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-909dc6889861df15/global/backendServices/nginx-backend].
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 Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-909dc6889861df15/global/urlMaps/web-map]. NAME DEFAULT_SERVICE web-map backendServices/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 Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-909dc6889861df15/global/targetHttpProxies/http-lb-proxy]. NAME URL_MAP http-lb-proxy 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 Created [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-909dc6889861df15/global/forwardingRules/http-content-rule]. gcloud compute forwarding-rules list NAME REGION IP_ADDRESS IP_PROTOCOL TARGET http-content-rule 35.227.248.142 TCP http-lb-proxy nginx-lb us-central1 35.193.58.130 TCP us-central1/targetPools/nginx-pool
launch http load balancer
http://35.227.248.142/ Welcome to Google Cloud Platform - nginx-c144! If you see this page, the Google Cloud Platform - nginx-c144 web server is successfully installed and working. Further configuration is required. For online documentation and support please refer to nginx.org. Commercial support is available at nginx.com. Thank you for using Google Cloud Platform - nginx-c144.