Description
A Network Load Balancer (NLB) in Google Cloud Platform (GCP) distributes TCP/UDP traffic across backend instances at Layer 4 (transport layer). Unlike the Application Load Balancer, which operates at Layer 7 (HTTP/HTTPS), the NLB provides low-latency, pass-through load balancing for applications such as gaming servers, databases, or custom TCP/UDP services.
In this guided lab, you will configure a regional external TCP load balancer that balances traffic between two VM instances running simple TCP echo servers on port 9000. By the end, you’ll understand how GCP’s NLB ensures scalability and fault tolerance for non-HTTP workloads
Prerequisites
To ensure the successful completion of this lab, we highly recommend taking the following guided labs to gain the necessary understanding:
- Guided Lab: How to Launch a GCP Compute Engine Linux Instance
- Guided Lab: Setting up a Web Server on a VM Instance
- Guided Lab: Using Startup Scripts in GCP VM Instances
- Guided Lab: Reserving or Promoting a Static IP Address for a VM Instance
- Guided Lab: Creating and Managing Instance Groups in Compute Engine
Objectives
In this lab, you will:
- Launch two VM instances running a TCP echo service on port 9000.
- Create a firewall rule to allow TCP traffic on port 9000.
- Group the VMs into an Unmanaged Instance Group.
- Configure a regional external TCP load balancer.
- Create and apply a TCP health check.
- Verify that traffic is distributed between the backend servers
Lab Steps
Create a Compute Engine VM Instance
1. Navigate to Compute Engine → VM instances → Create Instance.
2. Configure:
- Name: tcp-server-1 and tcp-server-2
- Machine type: e2-micro
- Boot disk: Ubuntu 24.04 LTS Minimal (x86/64, amd64 noble minimal image built on 2025‑12‑17)

- Firewall: Leave default (we’ll add a custom rule later)
3. Navigate to the Advanced section.
4. In the Startup script field, paste the following:
#!/bin/bash
sudo apt update -y
sudo apt install -y netcat-openbsd
# Create the echo server script
cat << 'EOF' > /home/tcpecho.sh
#!/bin/bash
nohup nc -lk 9000 > /tmp/nc_input &
tail -f /tmp/nc_input | while read line; do
echo "Hello from $(hostname): $line"
done
EOF
5. Once done, click the Create button.
Wait for the VMs to finish provisioning.
Create Firewall Rule for Port 9000
1. Navigate to VPC network → Select the Default VPC.

2. Navigate to Firewalls → Create VPC firewall rule → Configure:
- Name: allow-tcp-rule
- Direction of traffic: Ingress
- Targets: All instances in the network
- Source filter: IP ranges → 0.0.0.0/0
- Protocols and ports: TCP → 9000

3. Click Create.
Create an Unmanaged Instance Group
1. Go to Compute Engine → Instance groups → Create Instance Group.
2. Select Unmanaged instance group.
3. Name the group (e.g., instance-group-1).
4. Under Location, change the Zone based on the zone of the created VMs.

5. Add both VM instances (tcp-server-1 and tcp-server-2) to the group.

6. Click the Create button.
Create the Network Load Balancer
1. Navigate to Network services → Load balancing → Create Load Balancer.
2. Choose Network Load Balancer → Next → Leave other configurations at their default values → Start configuration.

Backend Configuration
- Backend type: Instance group
- Health check: Create a new TCP health check (port 9000)


- Named port: tcp9000

- Instance group: Select the created instance group (instance-group-1)

Frontend Configuration
- Protocol: TCP
- IP: Create a new static IP (recommended)


- Port: 9000
Review and Create
1. Review the configuration summary.
2. Add the desired Load Balancer name.
3. Click Create to provision the load balancer.
Verify the Network Load Balancer
1. Once provisioning completes, copy the frontend IP address.

2. Connect to the two servers via SSH, then run the shell script.
sudo bash /home/tcpecho.sh
3. Open the terminal on your local machine.
4. Run the following loop to generate multiple connections:
Windows:
1..20 | ForEach-Object { "test $_" | ncat <LOAD_BALANCER_IP> 9000 }
macOS/ Linux:
for i in {1..20}; do
echo "test $i" | ncat <LOAD_BALANCER_IP> 9000
done
5. Expected output (alternating between servers):

Congratulations! You have successfully created a Network Load Balancer in GCP using an Unmanaged Instance Group. This setup demonstrates how GCP distributes TCP traffic at Layer 4, ensuring scalability and reliability. You also learned how to configure firewall rules, health checks, and confirm traffic distribution with a clear echo verification loop across Windows, macOS, and Linux using ncat.