creating a nlb in gcp

this is my first time ever creating a network load balancer, well, i know the gist of it but i havent really created one. i got the opportunity to create one from a google cloud labs, so maybe these commands are specifically for gcp but yeah, you will get the idea of it eventually.

basically what we need are:

create 3 isntances with the same tags

well yeah, basically just go ahead and create some similar instances with a tags so that yall can use all this three instances in a “group”. the gcloud command is something like this

Terminal window
gcloud compute instances create www1 \
--zone=Zone \
--tags=network-lb-tag \
--machine-type=e2-small \
--image-family=Debian image ID \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install apache2 -y
service apache2 restart
echo "
<h3>Web Server: www1</h3>" | tee /var/www/html/index.html'

well basically just create 2 more and youre done

create a firewall rule to allow tcp:80 to those instances

since we are using gcp, i think by default the 80 port is disabled? so we have to enable it so that our vms can communicate with the lb itself

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

create static external ip for the load balancer

well, a load balancer needs an ip so that they can communicate with the user and the vms itself right? so lets create one

Terminal window
gcloud compute addresses create network-lb-ip-1 \
--region Region

create target pool and forwarding pool

the last thing you need to do is to create pool and forwarding pool

Terminal window
gcloud compute target-pools create www-pool \
--region Region --http-health-check basic-check

and then add the instances to the pool

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

lastly create a forwarding pool so that the traffic can be directed to the backend services

Terminal window
gcloud compute forwarding-rules create www-rule \
--region Region \
--ports 80 \
--address network-lb-ip-1 \
--target-pool www-pool

and voila, a basic load balancer is created. obviously, in production, it will not be as simple as this me thinks. but by creating this, you will get the big idea on how load balancer really works rather than just says “oh it distributes traffic”. you know what really needed to create one and whats going on the background.