Setting Up HTTP(S) Load Balancer in GCP

Disclaimer: This is note from my self learning in GCP and may not be correct or complete. Qwiklabs https://google.qwiklabs.com/focuses/10258?parent=catalog

 

In general these are the steps:

  • Create an instance template.
  • Create a target pool.
  • Create a managed instance group.
  • Create a firewall rule to allow traffic (80/tcp).
  • Create a health check.
  • Create a backend service, and attach the managed instance group.
  • Create a URL map, and target the HTTP proxy to route requests to your URL map.
  • Create a forwarding rule.
#create startup.sh script
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




#create instance template
gcloud compute instance-templates create nginx-template \
   --metadata-from-file startup-script=startup.sh

#create a target pool
gcloud compute target-pools create nginx-pool 

#create a managed instance group
gcloud compute instance-groups managed create nginx-group \
   --template=nginx-template --size=2 --target-pool nginx-pool

#Create a firewall rule to allow traffic (80/tcp)
gcloud compute firewall-rules create www-firewall \
  --allow tcp:80

#Create a forwarding rule
gcloud compute forwarding-rules create nginx-fwd-rule \
        --ports=80 \
        --target-pool nginx-pool

gcloud compute forwarding-rules list

#Create a health check
gcloud compute http-health-checks create basic-check

gcloud compute instance-groups managed set-named-ports nginx-group --named-ports http:80

#Create a backend service, and attach the managed instance group
gcloud compute backend-services create web-backend-service \
        --protocol=HTTP \
        --port-name=http \
        --http-health-checks=basic-check \
        --global

gcloud compute backend-services add-backend web-backend-service \
        --instance-group=nginx-group \
        --instance-group-zone=us-east1-b \
        --global

#Create a URL map, and target the HTTP proxy to route requests to your URL map
gcloud compute url-maps create web-map-http \
        --default-service web-backend-service

gcloud compute target-http-proxies create http-lb-proxy \
        --url-map web-map-http

gcloud compute forwarding-rules create http-content-rule \
--global --target-http-proxy http-lb-proxy --ports 80


No comments:

Post a Comment

Books I Used to Get My Cybersecurity Certifications

Disclaimer: I'm a big fan of Mike Chapple books and I've been using his books to prepare for all my three cybersecurity certificatio...