DEV Community

Jonathan Aseh
Jonathan Aseh

Posted on

Implement Load Balancing on Compute Engine

Task 1. Configure the region and Zone:

Visual Basic

gcloud config set compute/region us-east1 gcloud config set compute/zone us-east1-d

Task 2. Create the instance:

Visual Basic

gcloud compute instances create nucleus-jumphost-397 \ --zone=us-east1-d \ --machine-type=e2-micro \ --image-family=debian-11 \ --image-project=debian-cloud \ --tags=nucleus-network

Task 3. Configure the web servers using nginx

Visual Basic

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

Task 4. Create an instance template.

Mermaid

Code

gcloud compute instance-templates create nucleus-server-template \ --region=us-east1 \ --machine-type=e2-medium \ --image-family=debian-11 \ --image-project=debian-cloud \ --tags=nucleus-network \ --metadata-from-file startup-scri

--metadata-from-file startup-script=startup.sh 
Enter fullscreen mode Exit fullscreen mode

Task 5. Create a managed instance group based on the template.

gcloud compute instance-groups managed create nucleus-webserver-group \ --template=nucleus-server-template \ --size=2 \ --base-instance-name=nucleus-webserver \ --zone=us-east1-d

Task 6. Create a firewall rule.

Visual Basic

gcloud compute firewall-rules create accept-tcp-rule-843 --allow tcp:80 --target-tags=nucleus-network

Task 7. Create a health check.

Visual Basic

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

Task 8. Create a backend service and add your instance group as the backend to the backend service group with named port (http:80)

Visual Basic

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

Task 8.b. Add your instance group as the backend to the backend service group with named port (http:80)

Visual Basic

gcloud compute backend-services add-backend web-backend-service --instance-group=nucleus-webserver-group --instance-group-zone=us-east1-d --global (This corrects the ports fail error) gcloud compute instance-groups managed set-named-ports nucleus-webserver-group --named-ports http:80 --zone=us-east1-d

Task 9.b Target the HTTP proxy to route the incoming requests to the default backend service.

Visual Basic

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

Task 10. Create a forwarding rule.

Visual Basic

gcloud compute forwarding-rules create http-content-rule --target-http-proxy=web-http-proxy --ports=80 --global

Note: Wait for 5 to 7 minutes, then check the external IP to see the congratulatory NGINX Server display.

Top comments (0)