Use forwarding rules

A forwarding rule and its corresponding IP address represent the frontend configuration of a Google Cloud load balancer. For a general understanding of forwarding rules, see Forwarding rule concepts.

Before you begin

Before adding a forwarding rule, reserve an IP address for your forwarding rule. This isn't a requirement, but it is a best practice.

For internal load balancers, reserve a static internal IP address.

For external load balancers, reserve a static external IP address.

Permissions

To follow this guide, you should have the necessary permissions.

For more information, see the following guides:

Create a forwarding rule

You can create a forwarding rule when you're creating or updating a load balancer as shown in the following basic setup documentation:

Delete a forwarding rule

You might want to delete a forwarding rule for one of the following reasons:

  • To replace it with a new forwarding rule
  • To stop a load balancer for a limited time, without deleting the load balancer entirely.

These are two of the reasons you might need to stop a load balancer without deleting it:

  • To temporarily suspend charges for the load balancer
  • To temporarily pause incoming requests to your backends

If the forwarding rule points to a reserved IP address (as recommended), you can delete the forwarding rule to stop a load balancer. This stops traffic to the forwarding rule destination.

To delete a global forwarding rule:

gcloud compute forwarding-rules delete FORWARDING_RULE_NAME \
    --global

To delete a regional forwarding rule:

gcloud compute forwarding-rules delete FORWARDING_RULE_NAME \
    --region=REGION

To restart the load balancer, re-create the forwarding rule and keep the same IP address, as described in Adding a forwarding rule.

Configure a forwarding rule with Service Directory

You can register load balancer forwarding rules with Service Directory.

When you configure your internal passthrough Network Load Balancer, regional internal Application Load Balancer, or external passthrough Network Load Balancer, you can register it as an endpoint in an existing Service Directory namespace and service of your choice. Client applications can then use Service Directory using HTTP, gRPC, and/or DNS (if you have created a Service Directory DNS zone) to resolve the address of the load balancer service and connect to it directly.

Registering an internal passthrough Network Load Balancer

To register an internal passthrough Network Load Balancer, run the gcloud compute forwarding-rules create command and set the service-directory-registration flag:

gcloud compute forwarding-rules create FORWARDING_RULE_NAME \
    --region=REGION \
    --load-balancing-scheme=INTERNAL \
    --network=NETWORK_NAME \
    --subnet=SUBNET_NAME \
    --address=RESERVED_IP_ADDRESS \
    --ip-protocol=PROTOCOL_TYPE \
    --ports=PORT_NUMBER \
    --backend-service=BACKEND_SERVICE_NAME \
    --backend-service-region=REGION \
    --service-directory-registration=SD_SERVICE_NAME

Replace the following:

  • FORWARDING_RULE_NAME: a name for the forwarding rule that you want to create
  • REGION: the region to create the forwarding rule in
  • NETWORK_NAME: the network that this forwarding rule applies to
  • SUBNET_NAME: the subnetwork that this forwarding rule applies to
  • RESERVED_IP_ADDRESS: the IP address that the forwarding rule serves
  • PROTOCOL_TYPE: the IP protocol that the rule will serve
  • PORT_NUMBER: a list of comma-separated ports
  • BACKEND_SERVICE_NAME: target backend service that receives the traffic
  • SD_SERVICE_NAME: the fully qualified name of the Service Directory service where you want to register the endpoint. It must live in the same project and region as the forwarding rule being created. For example: projects/PROJECT/locations/REGION/namespaces/NAMESPACE_NAME/services/SERVICE_NAME.

Registering a regional internal Application Load Balancer

To register a regional internal Application Load Balancer, run the gcloud compute forwarding-rules create command and set the service-directory-registration flag:

gcloud beta compute forwarding-rules create FORWARDING_RULE_NAME \
    --region=REGION \
    --load-balancing-scheme=INTERNAL_MANAGED \
    --network=NETWORK_NAME \
    --address=RESERVED_IP_ADDRESS \
    --target-https-proxy=PROXY_NAME \
    --target-https-proxy-region=PROXY_REGION \
    --ports=PORT_NUMBER \
    --service-directory-registration=SD_SERVICE_NAME

Replace the following:

  • FORWARDING_RULE_NAME: a name for the forwarding rule that you want to create
  • REGION: the region to create the forwarding rule in
  • NETWORK_NAME: the network that this forwarding rule applies to
  • RESERVED_IP_ADDRESS: the IP address that the forwarding rule serves
  • PROXY_NAME: target proxy that receives the traffic
  • PROXY_REGION: region of the proxy to operate on
  • PORT_NUMBER: a list of comma-separated ports
  • SD_SERVICE_NAME: the fully qualified name of the Service Directory service where you want to register the endpoint. It must live in the same project and region as the forwarding rule being created. For example: projects/PROJECT/locations/REGION/namespaces/NAMESPACE_NAME/services/SERVICE_NAME.

Registering an external passthrough Network Load Balancer

To register an external passthrough Network Load Balancer, run the gcloud compute forwarding-rules create command and set the service-directory-registration flag:

gcloud beta compute forwarding-rules create FORWARDING_RULE_NAME \
    --region=REGION \
    --load-balancing-scheme=EXTERNAL \
    --address=RESERVED_IP_ADDRESS \
    --ip-protocol=PROTOCOL_TYPE \
    --ports=PORT_NUMBER \
    --backend-service=BACKEND_SERVICE_NAME \
    --backend-service-region=REGION \
    --service-directory-registration=SD_SERVICE_NAME

Replace the following:

  • FORWARDING_RULE_NAME: a name for the forwarding rule that you want to create
  • REGION: the region to create the forwarding rule in
  • RESERVED_IP_ADDRESS: the IP address that the forwarding rule serves
  • PROTOCOL_TYPE: the IP protocol that the rule will serve
  • PORT_NUMBER: a list of comma-separated ports
  • BACKEND_SERVICE_NAME: target backend service that receives the traffic
  • SD_SERVICE_NAME: the fully qualified name of the Service Directory service where you want to register the endpoint. It must live in the same project and region as the forwarding rule being created. For example: projects/PROJECT/locations/REGION/namespaces/NAMESPACE_NAME/services/SERVICE_NAME.

For detailed information about how to configure load balancers in Service Directory, see the following sections:

What's next