How to Setup Network Load balancer in Google Cloud


 


 

                                    Network Load Balancer



Set the default region and zone for all resources

gcloud config set compute/zone us-central1-a
gcloud config set compute/region us-central1


Create multiple Loadbalancer web server instances

Create three new virtual machines in your default zone and give them all the same tag. The code provided sets the zone to us-central1-a. Setting the tags field lets you reference these instances all at once, such as with a firewall rule. These commands also install Apache on each instance and give each instance a unique home page.

gcloud compute instances create www1 \
  --image-family debian-9 \
  --image-project debian-cloud \
  --zone us-central1-a \
  --tags network-lb-tag \
  --metadata startup-script="#! /bin/bash
    sudo apt-get update
    sudo apt-get install apache2 -y
    sudo service apache2 restart
    echo '<!doctype html><html><body><h1>www1</h1></body></html>' | tee /var/www/html/index.html"



gcloud compute instances create www2 \
  --image-family debian-9 \
  --image-project debian-cloud \
  --zone us-central1-a \
  --tags network-lb-tag \
  --metadata startup-script="#! /bin/bash
    sudo apt-get update
    sudo apt-get install apache2 -y
    sudo service apache2 restart
    echo '<!doctype html><html><body><h1>www2</h1></body></html>' | tee /var/www/html/index.html"




gcloud compute instances create www3 \
  --image-family debian-9 \
  --image-project debian-cloud \
  --zone us-central1-a \
  --tags network-lb-tag \
  --metadata startup-script="#! /bin/bash
    sudo apt-get update
    sudo apt-get install apache2 -y
    sudo service apache2 restart
    echo '<!doctype html><html><body><h1>www3</h1></body></html>' | tee /var/www/html/index.html"


Create a firewall rule to allow external traffic to the VM instances:


gcloud compute firewall-rules create www-firewall-network-lb \
    --target-tags network-lb-tag --allow tcp:80


Show Instances

gcloud compute instances list


Configure the load balancing service

Create a static external IP address for your load balancer

gcloud compute addresses create network-lb-ip-1 \
 --region us-central1


Add a legacy HTTP health check resource:

gcloud compute http-health-checks create basic-check


Add a target pool in the same region as your instances. Run the following to create the target pool and use the health check, which is required for the service to function:



gcloud compute target-pools create www-pool \
    --region us-central1 --http-health-check basic-check


Add the instances to the pool


gcloud compute target-pools add-instances www-pool \
    --instances www1,www2,www3


Add a forwarding rule:

gcloud compute forwarding-rules create www-rule \
    --region us-central1 \
    --ports 80 \
    --address network-lb-ip-1 \
    --target-pool www-pool


Enter the following command to view the external IP address of the www-rule forwarding rule used by the load balancer:

gcloud compute forwarding-rules describe www-rule --region us-central1

Access Load Balancer

while true; do curl -m1 Write Your IP_ADDRESS; done


Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment