This tutorial shows how you, as a service producer, can create a simple service that uses an internal passthrough Network Load Balancer. By default, an internal passthrough Network Load Balancer is available only from within the VPC network that hosts it.
This tutorial is intended for cloud architects, network architects, network administrators, and IT administrators.
Objectives
- Configure networking for the service producer resources
- Create VM resources for the load balancer backend
- Configure the load balancer components
- Test accessing the load balancer
Costs
In this document, you use the following billable components of Cloud de Confiance by S3NS:
When you finish the tasks that are described in this document, you can avoid continued billing by deleting the resources that you created. For more information, see Clean up.
Before you begin
- Create or select a project to use for the service producer resources.
This project is referred to in the later tutorials as
PRODUCER_PROJECT -
In the Cloud de Confiance console, go to the project selector page.
-
Select or create a Cloud de Confiance project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Enable the Compute Engine API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Make sure that you have the following role or roles on the project: Compute Engine > Compute Network Admin, Compute Engine > Compute Security Admin, Compute Engine > Compute Instance Admin
Check for the roles
-
In the Cloud de Confiance console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Cloud de Confiance console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the identifier for a user in a workforce identity pool. For details, see Represent workforce pool users in IAM policies, or contact your administrator.
- In the Select a role list, select a role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
Configure networking
The following sections explain how to create a network, a subnet, and firewall rules for the service.
Configure a network and subnet
To create the service network and subnet for the load balancer, complete the steps in the following sections.
Console
In the Cloud de Confiance console, go to the VPC networks page.
Click Create VPC network.
For Name, enter
service-network.For Subnet creation mode, select Custom.
In the New subnet section, enter the following information.
- For Name, enter
service-subnet. - For Region, select the region that you want to use.
- For IP stack type, select IPv4 (single-stack).
- For IPv4 range, enter
10.10.10.0/24. - Click Done.
- For Name, enter
Click Create.
gcloud
Create a custom mode VPC network:
gcloud compute networks create service-network --subnet-mode=custom
In the
service-networknetwork, create a subnet.gcloud compute networks subnets create service-subnet \ --network=service-network \ --range=10.10.10.0/24 \ --region=REGIONReplace
REGIONwith the region that you want to use.
Configure firewall rules
Create firewall rules to allow the following traffic:
fw-allow-subnet: lets traffic from10.10.10.0/24reach other resources in10.10.10.0/24.fw-allow-ssh: lets SSH traffic from0.0.0.0/0reach VMs that have theallow-sshnetwork tag.fw-allow-healthcheck: lets traffic from the Cloud de Confiance health checking systems reach VMs that have theallow-healthchecknetwork tag.
Console
In the Cloud de Confiance console, go to the Firewall policies page.
To allow subnet traffic, click Create firewall rule and use the following settings:
- For Name, enter
fw-allow-subnet. - For Network, select
service-network. - For Priority, enter
1000. - For Direction of traffic, select Ingress.
- For Action on match, select Allow.
- For Targets, select All instances in the network.
- For Source filter, select IPv4 ranges.
- For Source IPv4 ranges, enter
10.10.10.0/24. - For Protocols and ports, select Allow all.
- For Name, enter
Click Create.
To allow incoming SSH connections, click Create firewall rule and use the following settings:
- For Name, enter
fw-allow-ssh. - For Network, select
service-network. - For Priority, enter
1000. - For Direction of traffic, select Ingress.
- For Action on match, select Allow.
- For Targets, select Specified target tags.
- For Target tags, enter
allow-ssh. - For Source filter, select IPv4 ranges.
- For Source IPv4 ranges, enter
0.0.0.0/0. - For Protocols and ports, select Specified protocols and ports,
select the TCP checkbox. For Ports, enter
22.
- For Name, enter
Click Create.
To allow Cloud de Confiance health checks, click Create firewall rule and use the following settings:
- For Name, enter
fw-allow-healthcheck. - For Network, select
service-network. - For Priority, enter
1000. - For Direction of traffic, select Ingress.
- For Action on match, select Allow.
- For Targets, select Specified target tags.
- For Target tags, enter
allow-healthcheck. - For Source filter, select IPv4 ranges.
- For Source IPv4 ranges, enter
130.211.0.0/22and35.191.0.0/16. - For Protocols and ports, select Allow all.
- For Name, enter
Click Create.
gcloud
Create the
fw-allow-subnetfirewall rule to allow communication from within the subnet:gcloud compute firewall-rules create fw-allow-subnet \ --network=service-network \ --action=allow \ --direction=ingress \ --source-ranges=10.10.10.0/24 \ --rules=tcp,udp,icmp
Create the
fw-allow-sshfirewall rule to allow SSH connectivity to VMs with the network tagallow-ssh.gcloud compute firewall-rules create fw-allow-ssh \ --network=service-network \ --action=allow \ --direction=ingress \ --source-ranges=0.0.0.0/0 \ --target-tags=allow-ssh \ --rules=tcp:22
Create the
fw-allow-healthcheckrule to allow Cloud de Confiance health checks.gcloud compute firewall-rules create fw-allow-healthcheck \ --network=service-network \ --action=allow \ --direction=ingress \ --target-tags=allow-healthcheck \ --source-ranges=130.211.0.0/22,35.191.0.0/16 \ --rules=tcp,udp,icmp
Create VM resources for the load balancer backend
Create the VMs and the instance group to use with the load balancer.
Create backend VMs
The service that you're creating runs on two VMs for higher availability:
vm-1 and vm-2. The VMs are in the same zone, but in a
production environment, we recommend using multiple zones.
In addition to creating the VMs, following these steps accomplishes the following:
- Configures the VMs to use the network tags that you configured as targets
in the firewall rules and an additional network
tag (
allow-nat) that is used later. Installs, configures, and starts the Apache service.
Console
In the Cloud de Confiance console, go to the VM instances page.
Click Create instance.
For Name, enter
vm-1.For Region and Zone, select a region and a zone within that region.
In the navigation menu, go to OS and storage.
In the Operating system and storage pane, ensure that Debian GNU/Linux 12 (bookworm) is selected for the boot disk. If you need to select a different image, click Change.
In the navigation menu, go to Networking.
For Network tags, enter
allow-ssh,allow-healthcheck, andallow-nat.For Network interfaces, select the following:
- For Network, select
service-network. - For Subnet, select
service-subnet. - For IP stack type, select IPv4 (single-stack).
- For Primary internal IPv4 address, select Ephemeral (automatic).
- For External IPv4 address, select Ephemeral.
- For Network, select
In the navigation menu, go to Advanced.
In the Automation section, in the Startup script field, enter the following script.
#! /bin/bash if [ -f /etc/startup_script_completed ]; then exit 0 fi apt-get update apt-get install apache2 -y a2ensite default-ssl a2enmod ssl file_ports="/etc/apache2/ports.conf" file_http_site="/etc/apache2/sites-available/000-default.conf" file_https_site="/etc/apache2/sites-available/default-ssl.conf" http_listen_prts="Listen 80\nListen 8008\nListen 8080\nListen 8088" http_vh_prts="*:80 *:8008 *:8080 *:8088" https_listen_prts="Listen 443\nListen 8443" https_vh_prts="*:443 *:8443" vm_hostname="$(curl -H "Metadata-Flavor:Google" \ http://metadata.google.internal/computeMetadata/v1/instance/name)" echo "Page served from: $vm_hostname" | \ tee /var/www/html/index.html prt_conf="$(cat "$file_ports")" prt_conf_2="$(echo "$prt_conf" | sed "s|Listen 80|${http_listen_prts}|")" prt_conf="$(echo "$prt_conf_2" | sed "s|Listen 443|${https_listen_prts}|")" echo "$prt_conf" | tee "$file_ports" http_site_conf="$(cat "$file_http_site")" http_site_conf_2="$(echo "$http_site_conf" | sed "s|*:80|${http_vh_prts}|")" echo "$http_site_conf_2" | tee "$file_http_site" https_site_conf="$(cat "$file_https_site")" https_site_conf_2="$(echo "$https_site_conf" | sed "s|_default_:443|${https_vh_prts}|")" echo "$https_site_conf_2" | tee "$file_https_site" systemctl restart apache2 touch /etc/startup_script_completedClick Create.
Repeat these steps to create another VM named
vm-2in the same region and zone asvm-1.
gcloud
Create two VMs by running the following commands:
gcloud compute instances create vm-1 \ --zone=ZONE \ --image-family=debian-12 \ --image-project=debian-cloud \ --tags=allow-ssh,allow-healthcheck,allow-nat \ --subnet=service-subnet \ --metadata=startup-script='#! /bin/bash if [ -f /etc/startup_script_completed ]; then exit 0 fi apt-get update apt-get install apache2 -y a2ensite default-ssl a2enmod ssl file_ports="/etc/apache2/ports.conf" file_http_site="/etc/apache2/sites-available/000-default.conf" file_https_site="/etc/apache2/sites-available/default-ssl.conf" http_listen_prts="Listen 80\nListen 8008\nListen 8080\nListen 8088" http_vh_prts="*:80 *:8008 *:8080 *:8088" https_listen_prts="Listen 443\nListen 8443" https_vh_prts="*:443 *:8443" vm_hostname="$(curl -H "Metadata-Flavor:Google" \ http://metadata.google.internal/computeMetadata/v1/instance/name)" echo "Page served from: $vm_hostname" | \ tee /var/www/html/index.html prt_conf="$(cat "$file_ports")" prt_conf_2="$(echo "$prt_conf" | sed "s|Listen 80|${http_listen_prts}|")" prt_conf="$(echo "$prt_conf_2" | sed "s|Listen 443|${https_listen_prts}|")" echo "$prt_conf" | tee "$file_ports" http_site_conf="$(cat "$file_http_site")" http_site_conf_2="$(echo "$http_site_conf" | sed "s|*:80|${http_vh_prts}|")" echo "$http_site_conf_2" | tee "$file_http_site" https_site_conf="$(cat "$file_https_site")" https_site_conf_2="$(echo "$https_site_conf" | sed "s|_default_:443|${https_vh_prts}|")" echo "$https_site_conf_2" | tee "$file_https_site" systemctl restart apache2 touch /etc/startup_script_completed'gcloud compute instances create vm-2 \ --zone=ZONE \ --image-family=debian-12 \ --image-project=debian-cloud \ --tags=allow-ssh,allow-healthcheck,allow-nati \ --subnet=service-subnet \ --metadata=startup-script='#! /bin/bash if [ -f /etc/startup_script_completed ]; then exit 0 fi apt-get update apt-get install apache2 -y a2ensite default-ssl a2enmod ssl file_ports="/etc/apache2/ports.conf" file_http_site="/etc/apache2/sites-available/000-default.conf" file_https_site="/etc/apache2/sites-available/default-ssl.conf" http_listen_prts="Listen 80\nListen 8008\nListen 8080\nListen 8088" http_vh_prts="*:80 *:8008 *:8080 *:8088" https_listen_prts="Listen 443\nListen 8443" https_vh_prts="*:443 *:8443" vm_hostname="$(curl -H "Metadata-Flavor:Google" \ http://metadata.google.internal/computeMetadata/v1/instance/name)" echo "Page served from: $vm_hostname" | \ tee /var/www/html/index.html prt_conf="$(cat "$file_ports")" prt_conf_2="$(echo "$prt_conf" | sed "s|Listen 80|${http_listen_prts}|")" prt_conf="$(echo "$prt_conf_2" | sed "s|Listen 443|${https_listen_prts}|")" echo "$prt_conf" | tee "$file_ports" http_site_conf="$(cat "$file_http_site")" http_site_conf_2="$(echo "$http_site_conf" | sed "s|*:80|${http_vh_prts}|")" echo "$http_site_conf_2" | tee "$file_http_site" https_site_conf="$(cat "$file_https_site")" https_site_conf_2="$(echo "$https_site_conf" | sed "s|_default_:443|${https_vh_prts}|")" echo "$https_site_conf_2" | tee "$file_https_site" systemctl restart apache2 touch /etc/startup_script_completed'Replace
ZONEwith the zone you want to use.
Create an instance group
To use the VMs in a load balancer backend, you must add the VMs to an instance group.
Console
In the Cloud de Confiance console, go to the Instance groups page.
Click Create instance group.
In the navigation pane, click New unmanaged instance group.
For Name, enter
ig-1.In the Location section, select the same region and zone as your VMs.
For Network, select
service-network.For Subnetwork, select
service-subnet.In the VM instances section, add
vm-1andvm-2to the instance group.Click Create.
gcloud
Create an unmanaged instance group:
gcloud compute instance-groups unmanaged create ig-1 \ --zone=ZONEReplace
ZONEwith the zone that contains the VMs.Add the VMs to the instance group:
gcloud compute instance-groups unmanaged add-instances ig-1 \ --zone=ZONE \ --instances=vm-1,vm-2
Configure load balancer components
Configure all of the internal passthrough Network Load Balancer components starting with the health check and backend service, and then the frontend components:
Health check. You use an HTTP health check that checks for an HTTP
200 OKstatus code.Backend service. Because you need to pass HTTP traffic through the internal load balancer, you need to use TCP, not UDP.
Forwarding rule. You create a single internal forwarding rule,
service-rule.Internal IP address. You specify an internal IP address,
10.10.10.99, when you create the forwarding rule.
Console
Start your configuration
In the Cloud de Confiance console, go to the Load balancing page.
- Click Create load balancer.
- For Type of load balancer, select Network Load Balancer (TCP/UDP/SSL) and click Next.
- For Proxy or passthrough, select Passthrough load balancer and click Next.
- For Public facing or internal, select Internal and click Next.
- Click Configure.
Basic configuration
On the Create internal passthrough Network Load Balancer page, enter the following information:
- For Load balancer name, enter
service-lb. - For Region, select the same region as your VMs.
- For Network, select
service-network.
Configure the backends
- Click Backend configuration.
- In the Health check list, click Create a health check,
and enter the following information:
- For Name:, enter
hc-http-80. - For Protocol:, select
HTTP. - For Port:, enter
80. - For Proxy protocol:, select
NONE. - For Request path:, enter
/.
- For Name:, enter
- Click Create.
- In the New Backend section of Backends, select IPv4 (single-stack).
- In Instance group, select the
ig-1instance group and click Done. - Verify that there is a blue check mark next to Backend configuration before continuing.
Configure the frontend
- Click Frontend configuration.
- In the New Frontend IP and port section, do the following:
- For Name, enter
service-rule. - For Subnetwork, select
service-subnet. - In the Internal IP purpose section, in the IP address list,
select Create IP address, enter the following information:
- For Name, enter
service-rule-ip. - For IP version, select IPv4.
- For Static IP address, select Let me choose.
- For Custom IP address, enter
10.10.10.99.
- For Name, enter
- Click Reserve.
- For Ports, select Single and then in Port numbers, enter
80. - Click Done.
- Verify that there is a blue check mark next to Frontend configuration before continuing.
- For Name, enter
Review the configuration
- Click Review and finalize.
- Review your load balancer configuration settings.
- Click Create.
gcloud
Create a new regional HTTP health check to test HTTP connectivity to the VMs on port 80.
gcloud compute health-checks create http hc-http-80 \ --region=REGION \ --port=80Replace
REGIONwith the same region as the backend VMs.Create the backend service for HTTP traffic:
gcloud compute backend-services create service-lb \ --load-balancing-scheme=internal \ --protocol=tcp \ --region=REGION \ --health-checks=hc-http-80 \ --health-checks-region=REGIONReplace
REGIONwith the same region as the backend VMs.Add the instance group to the backend service:
gcloud compute backend-services add-backend service-lb \ --region=REGION \ --instance-group=ig-1 \ --instance-group-zone=ZONEReplace the following:
REGION: the same region as the backend VMs.ZONE: the same zone as the backend VMs.
Create a forwarding rule for the backend service:
gcloud compute forwarding-rules create service-rule \ --region=REGION \ --load-balancing-scheme=internal \ --network=service-network \ --subnet=service-subnet \ --address=10.10.10.99 \ --ip-protocol=TCP \ --ports=80 \ --backend-service=service-lb \ --backend-service-region=REGIONReplace
REGIONwith the same region as the backend VMs.
Test accessing the load balancer
To test that the load balancer is working, create a test VM and send a request from it to the load balancer.
Create a client VM for testing
Create a client VM in the same region as the backend (server) VMs.
Console
In the Cloud de Confiance console, go to the VM instances page.
Click Create instance.
For Name, enter
producer-test.For Region, select the same region as the backend VMs.
For Zone, select a zone in that region.
Click Networking and configure the following fields:
- For Network tags, enter
allow-ssh. - For Network interfaces, select the following:
- For Network, select
service-network - For Subnet, select
service-subnet
- For Network, select
- For Network tags, enter
Click Create.
gcloud
gcloud compute instances create producer-test \
--zone=ZONE \
--image-family=debian-12 \
--image-project=debian-cloud \
--tags=allow-ssh \
--subnet=service-subnet
Replace ZONE with a zone that's in the same
region as the backend VMs.
Test connectivity
This test contacts the load balancer from a client VM. The expected behavior is for traffic to be distributed across the load balancer's backend VMs.
- Connect to the client VM instance.
Replacegcloud compute ssh producer-test --zone=ZONE
ZONEwith the zone of the client VM. - Make a web request to the load balancer using
curlto contact its IP address. Repeat the request so you can see that responses come from different backend VMs. The name of the VM generating the response is displayed in the text in the HTML response, because of the contents of/var/www/html/index.htmlon each backend VM. For example, expected responses look likePage served from: vm-1andPage served from: vm-2.curl -s http://10.10.10.99