0
0
GCPcloud~7 mins

HTTP(S) Load Balancer (Layer 7) in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When many users visit your website, their requests can slow down or crash your server. An HTTP(S) Load Balancer helps by spreading these requests across multiple servers, making your site faster and more reliable.
When your website gets more visitors than one server can handle smoothly.
When you want to keep your website running even if one server stops working.
When you want to direct users to the closest server to reduce loading time.
When you want to secure your website with HTTPS and manage certificates easily.
When you want to balance traffic based on URL paths or hostnames.
Config File - load-balancer.yaml
load-balancer.yaml
apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeBackendService
metadata:
  name: example-backend-service
spec:
  protocol: HTTP
  backends:
  - group: projects/my-project/zones/us-central1-a/instanceGroups/example-instance-group
  healthChecks:
  - projects/my-project/global/healthChecks/example-health-check
---
apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeUrlMap
metadata:
  name: example-url-map
spec:
  defaultService: example-backend-service
---
apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeTargetHttpProxy
metadata:
  name: example-http-proxy
spec:
  urlMap: example-url-map
---
apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeGlobalForwardingRule
metadata:
  name: example-forwarding-rule
spec:
  ipAddress: example-lb-ip
  ipProtocol: TCP
  portRange: "80"
  target: example-http-proxy

This file creates the main parts of an HTTP Load Balancer on GCP:

  • ComputeBackendService: Defines the group of servers that will handle the traffic.
  • ComputeUrlMap: Routes incoming requests to the backend service.
  • ComputeTargetHttpProxy: Connects the URL map to the forwarding rule.
  • ComputeGlobalForwardingRule: Listens for traffic on port 80 and sends it to the proxy.

The IP address is a static external IP assigned to the load balancer.

Commands
Reserve a global static IP address for the load balancer to use. This IP will be the public address users connect to.
Terminal
gcloud compute addresses create example-lb-ip --global
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/my-project/global/addresses/example-lb-ip].
--global - Reserve the IP address globally for global load balancers.
Check the reserved IP address details to use it in the load balancer configuration.
Terminal
gcloud compute addresses describe example-lb-ip --global
Expected OutputExpected
address: 34.68.194.64 addressType: EXTERNAL creationTimestamp: '2024-06-01T12:00:00.000-07:00' description: '' id: '1234567890123456789' name: example-lb-ip networkTier: PREMIUM selfLink: https://www.googleapis.com/compute/v1/projects/my-project/global/addresses/example-lb-ip status: RESERVING
--global - Show details for the global IP address.
Create the load balancer resources in GCP using the configuration file.
Terminal
kubectl apply -f load-balancer.yaml
Expected OutputExpected
computebackendservice.compute.cnrm.cloud.google.com/example-backend-service created computeurlmap.compute.cnrm.cloud.google.com/example-url-map created computetargethttpproxy.compute.cnrm.cloud.google.com/example-http-proxy created computeglobalforwardingrule.compute.cnrm.cloud.google.com/example-forwarding-rule created
Verify that the forwarding rule for the load balancer is active and listening on the correct IP and port.
Terminal
gcloud compute forwarding-rules list --global
Expected OutputExpected
NAME REGION IP_ADDRESS IP_PROTOCOL PORT_RANGE TARGET example-forwarding-rule global 34.68.194.64 TCP 80 example-http-proxy
--global - List global forwarding rules.
Key Concept

If you remember nothing else from this pattern, remember: a Layer 7 load balancer directs web traffic by inspecting URLs and spreads requests across multiple servers to keep your site fast and reliable.

Common Mistakes
Not reserving a global static IP before creating the forwarding rule.
The forwarding rule needs a fixed IP to receive traffic; without it, the load balancer won't have a reachable address.
Always create and note the global static IP address first, then use it in your load balancer configuration.
Applying the configuration without the backend instance group ready.
The backend service will fail to connect to servers, causing the load balancer to not route traffic properly.
Ensure your instance group with running servers exists before applying the load balancer config.
Using regional forwarding rules instead of global for HTTP(S) load balancers.
HTTP(S) load balancers in GCP require global forwarding rules to work across regions.
Use --global flag and global forwarding rules for HTTP(S) load balancers.
Summary
Reserve a global static IP address for your load balancer to have a fixed public address.
Create backend services and URL maps to define how traffic is routed to your servers.
Set up target HTTP proxy and global forwarding rule to listen for web traffic and send it to your backend.
Verify the forwarding rule is active and using the correct IP and port.