Skip to content

Instantly share code, notes, and snippets.

@chilampoon
Created July 14, 2021 20:51
Show Gist options
  • Save chilampoon/832ffb73592ad0a210d080b5b1c66a39 to your computer and use it in GitHub Desktop.
Save chilampoon/832ffb73592ad0a210d080b5b1c66a39 to your computer and use it in GitHub Desktop.
Set up an HTTP load balancer with a managed instance group of 2 nginx web servers on Google Cloud Platform (Qwiklab)
# setup
gcloud auth list
gcloud config set compute/zone us-east1-b
gcloud config set compute/region us-east1
# create an 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.html
EOF
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 of 2 nginx web servers
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
# create a firewall rule
gcloud compute firewall-rules create www-firewall --allow tcp:80
# create a forwarding rule
gcloud compute forwarding-rules create nginx-lb \
--region us-east1 \
--ports=80 \
--target-pool nginx-pool
gcloud compute forwarding-rules list
# create a health check
gcloud compute http-health-checks create http-basic-check
# create a backend service and attach the managed instasnce group
gcloud compute instance-groups managed \
set-named-ports nginx-group \
--named-ports http:80
gcloud compute backend-services create nginx-backend \
--protocol HTTP \
--http-health-checks http-basic-check \
--global
gcloud compute backend-services add-backend nginx-backend \
--instance-group nginx-group \
--instance-group-zone us-east1-b \
--global
# create a url map and target the HTTP proxy
gcloud compute url-maps create web-map \
--default-service nginx-backend
gcloud compute target-http-proxies create http-lb-proxy \
--url-map web-map
# create a forwarding rule
gcloud compute forwarding-rules create http-content-rule \
--global \
--target-http-proxy http-lb-proxy \
--ports 80
gcloud compute forwarding-rules list
@krishnanunnijs
Copy link

following command need to be corrected

gcloud compute backend-services create nginx-backend
--protocol HTTP
--health-checks http-basic-check
--global

@akshaypilankar
Copy link

Use this to create an instance template

cat << EOF > startup.sh
#! /bin/bash
apt-get update
apt-get install -y nginx
sed -i -- 's/nginx/Google Cloud Platform - '"$HOSTNAME"'/' /var/www/html/index.nginx-debian.html
service nginx start
EOF

@FelipeAlafy
Copy link

FelipeAlafy commented Apr 19, 2024

My solution based on this repository as well as my notes and the general course.

First Task

gcloud config set project PROJECT_ID_CAN_BE_FOUND_ON_YOUR_LAB
gcloud config set compute/region REGION
gcloud config set compute/zone ZONE
gcloud compute instances create nucleus-webserver1 --machine-type=e2-micro

Seccond task - copy and past the following lines into your cloud console to create the startup.sh file which will be the base for your template startup 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

Other commands starts here:

gcloud compute instance-templates create lb-backend-template
--region=REGION
--network=default
--subnet=default
--tags=allow-health-check
--machine-type=e2-medium
--image-family=debian-11
--image-project=debian-cloud
--metadata-from-file startup-script=startup.sh

Creating the pool

gcloud compute target-pools create nginx-pool

gcloud compute instance-groups managed create lb-backend-group --base-instance-name nginx --template=lb-backend-template --size=2 --target-pool nginx-pool --zone=us-east1-b

Creating a firewall for tcp:80

gcloud compute firewall-rules create www-firewall --allow tcp:80

Allowing tcp connection through the firewall

gcloud compute firewall-rules create permit-tcp-rule-586
--network=default
--action=allow
--direction=ingress
--source-ranges=130.211.0.0/22,35.191.0.0/16
--target-tags=allow-health-check
--rules=tcp:80
--target-pool nginx-pool

Creating an external IPV4 address

gcloud compute addresses create lb-ipv4-1
--ip-version=IPV4
--global

Health check for port 80

gcloud compute health-checks create http http-basic-check --port 80

Forwarding Rule to the nginx-pool

gcloud compute forwarding-rules create nginx-lb
--region REGION
--ports=80
--target-pool nginx-pool

Creating a basic http check

gcloud compute http-health-checks create http-basic-check

Defining the ports to http:80 to the mangened group

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

Backend service

gcloud compute backend-services add-backend web-backend-service --instance-group=lb-backend-group --instance-group-zone=ZONE --global

URL Map

gcloud compute url-maps create web-map-http --default-service web-backend-service

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

Forwarding rule to the proxy

gcloud compute forwarding-rules create http-content-rule
--address=lb-ipv4-1
--global
--target-http-proxy=http-lb-proxy
--ports=80

Adding the VM Instances to the firewall, it won't load at the browser if you don't do the followings steps, neither count as done

gcloud compute instances add-tags NAME_OF_INSTANCE_1 --tags http-server,https-server

gcloud compute instances add-tags NAME_OF_INSTANCE_2 --tags http-server,https-server

gcloud compute firewall-rules create default-allow-http --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=tc
p:80 --source-ranges=0.0.0.0/0 --target-tags=http-server

OPTIONAL

  • you can check your instances from the loadbalancer external ip address only http will work, because of the certification need for https
    http://[EXTERNAL_IP]:80

@erickmarx
Copy link

JUST DON'T FORGET CHANGE THE PARAMETERS LIKE PROJECT, REGION, ZONE, INSTANCE NAME

gcloud config set project qwiklabs-gcp-00-eba4ac046480
gcloud config set compute/region europe-west4
gcloud config set compute/zone europe-west4-b
gcloud compute instances create nucleus-jumphost-589 --machine-type=e2-micro

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

gcloud compute instance-templates create lb-backend-template --region=europe-west4 --network=default --subnet=default --tags=allow-health-check --machine-type=e2-medium --image-family=debian-11 --image-project=debian-cloud --metadata-from-file startup-script=startup.sh

gcloud compute target-pools create nginx-pool

gcloud compute instance-groups managed create lb-backend-group --base-instance-name nginx --template=lb-backend-template --size=2 --target-pool nginx-pool --zone=europe-west4-b

gcloud compute firewall-rules create www-firewall --allow tcp:80

gcloud compute firewall-rules create permit-tcp-rule-522 --network=default --action=allow --direction=ingress --source-ranges=130.211.0.0/22,35.191.0.0/16 --target-tags=allow-health-check --rules=tcp:80

gcloud compute addresses create lb-ipv4-1 --ip-version=IPV4 --global

gcloud compute health-checks create http http-basic-check --port 80

gcloud compute forwarding-rules create nginx-lb --region europe-west4 --ports=80 --target-pool nginx-pool

gcloud compute http-health-checks create http-basic-check

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

gcloud compute backend-services create web-backend-service --protocol=HTTP --port-name=http --health-checks=http-basic-check --global

gcloud compute backend-services add-backend web-backend-service --instance-group=lb-backend-group --instance-group-zone=europe-west4-b --global

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 --address=lb-ipv4-1 --global --target-http-proxy=http-lb-proxy --ports=80

@lynexor
Copy link

lynexor commented Oct 8, 2024

Hi, I'm wondering why do you all create a target pool ? I thought that only used on network/global loadbalancing and not on http loadbalancing but now I'm kind of confused..

@marti1125
Copy link

the solutions doesn't work!

@marti1125
Copy link

Please verify the web servers are serving on frontend of HTTP(s) Load Balancer.

@marti1125
Copy link

Please create the managed instance group with 2 nginx web-server.

@SsaylemMurillo
Copy link

2024/november. What worked for me:

REGIONS AND ZONES MUST VARY IN YOUR CASE.

2 STEP.

  1. gcloud compute instance-templates create lb-backend-template
    --region=us-east1 \
    --network=default
    --subnet=default
    --tags=allow-health-check
    --machine-type=e2-medium
    --image-family=debian-11
    --image-project=debian-cloud
    --metadata-from-file=startup-script=startup.sh

  2. gcloud compute target-pools create nginx-pool --region=us-east1

  3. gcloud compute instance-groups managed create lb-backend-group --base-instance-name nginx --template=lb-backend-template --size=2 --target-pool nginx-pool --zone=us-east1-c

  4. gcloud compute firewall-rules create www-firewall --allow tcp:80

change the rule as necessary in your case
5. gcloud compute firewall-rules create permit-tcp-rule-649 --network=default --action=allow --direction=ingress --source-ranges=130.211.0
.0/22,35.191.0.0/16 --target-tags=allow-health-check --rules=tcp:80

  1. gcloud compute addresses create lb-ipv4-1 --ip-version=IPV4 --global

  2. gcloud compute health-checks create http http-basic-check --port 80

  3. gcloud compute forwarding-rules create nginx-lb --region us-east1 --ports=80 --target-pool nginx-pool

  4. gcloud compute http-health-checks create http-basic-check

  5. gcloud compute instance-groups managed set-named-ports lb-backend-group
    --named-ports http:80
    --zone=us-east1-c

  6. gcloud compute backend-services create web-backend-service --protocol=HTTP --port-name=http --health-checks=http-basic-check --global

  7. gcloud compute backend-services add-backend web-backend-service --instance-group=lb-backend-group --instance-group-zone=us-east1-c --global

  8. gcloud compute url-maps create web-map-http --default-service web-backend-service

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

  10. gcloud compute forwarding-rules create http-content-rule --address=lb-ipv4-1 --global --target-http-proxy=http-lb-proxy --ports=80

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment