Using forwarding rules

A forwarding rule and its corresponding IP address represent the frontend configuration of a Google Cloud Platform 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.

Adding a forwarding rule

Console

Create the load balancer's forwarding rule

  1. Go to the Load balancing page in the Google Cloud Platform Console.
    Go to the Load balancing page
  2. Click Create load balancer.
  3. Select a load balancer type, including the traffic type and whether the load balancer faces the Internet or is internal only.
  4. Click Continue.
  5. Click Frontend configuration. In the New Frontend IP and port section, make the following changes:

    1. Name: forwarding rule name
    2. Subnetwork: subnet of your reserved IP address
    3. From Internal IP or from IP Address, select your pre-reserved IP address.

      Optionally, you can reserve an IP address now in this UI, or you can use an ephemeral IP address.

    4. Select the protocol, port numbers, and IP version.

      Only some load balancer types support IPv6.

    5. Verify that there is a blue check mark next to Frontend configuration before continuing. Review this step if not.

  6. Click Review and finalize. Double-check your settings.

  7. Click Create.

gcloud

Create a forwarding rule for the backend service. When you create the forwarding rule, specify your reserved IP address in the subnet.

gcloud compute forwarding-rules create <var>forwarding rule name \
    --global | --region=region \
    --load-balancing-scheme=scheme \
    --network=network name \
    --subnet=subnet name \
    --address=reserved IP address \
    --ip-protocol=protocol type \
    --ports=port number \
    --backend-service=name of backend service \
    --backend-service-region=region of backend service

api

For a regional load balancer, create a regional forwarding rule by making a POST request to the forwardingRules.insert method.

POST https://www.googleapis.com/compute/v1/projects/[project ID]/regions/us-west1/forwardingRules
{
  "name": "[forwarding rule name]",
  "IPAddress": "[reserved IP address]",
  "IPProtocol": "[protocol type]",
  "ports": [
    "[port number]"
  ],
  "loadBalancingScheme": "[scheme]",
  "subnetwork": "https://www.googleapis.com/compute/v1/projects/[project ID]/regions/[region]/subnetworks/[subnet name]",
  "network": "https://www.googleapis.com/compute/v1/projects/[project ID]/global/networks/[network name]",
  "backendService": "https://www.googleapis.com/compute/v1/projects/[project ID]/regions/[region]/backendServices/[backend service name]",
  "networkTier": "PREMIUM | STANDARD"
}
</code></pre>

For a global load balancer, create a global forwarding rule by making a POST request to the globalForwardingRules.insert method.

POST https://www.googleapis.com/compute/v1/projects/[project ID]/global/forwardingRules
{
  "name": "[forwarding rule name]",
  "IPAddress": "[reserved IP address]",
  "IPProtocol": "[protocol type]",
  "ports": [
    "[port number]"
  ],
  "loadBalancingScheme": "[scheme]",
  "subnetwork": "https://www.googleapis.com/compute/v1/projects/[project ID]/regions/[region]/subnetworks/[subnet name]
  "network": "https://www.googleapis.com/compute/v1/projects/[project ID]/global/networks/[network name]",
  "backendService": "https://www.googleapis.com/compute/v1/projects/[project ID]/regions/[region]/backendServices/[backend service name]
  "networkTier": "PREMIUM | STANDARD"
}

Deleting a forwarding rule

There are times when you might want to delete a forwarding rule so that you can replace it with a new one.

Another reason to delete a forwarding rule is to stop a load balancer for a limited time, without deleting the load balancer entirely. Examples of when this might be useful include the following:

  • Temporarily suspending charges for a load balancer
  • Temporarily pausing incoming requests to your VM instances

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.

Delete your existing forwarding rule:

gcloud compute forwarding-rules delete forwarding rule name \
    --global | --region=region

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

What's next

¿Te sirvió esta página? Envíanos tu opinión:

Enviar comentarios sobre…