Step 3: Create a load balancer

In this step, you create an Application Load Balancer to distribute the traffic to backend instances hosted on Cloud Run. In the load balancer configuration, you define the routing rules, which determine how the load balancer directs the traffic. Routing rules include host rules and path matches, which constitute the configuration components of an external Application Load Balancer's URL map.

Note that to complete this step, you must have a valid domain name or a valid self-managed certificate.

Create a load balancer for the Cloud Run service

  1. In the Google Cloud console, go to the Load balancing page.

    Go to Load balancing

  2. Click Create load balancer.

  3. In the Type of load balancer section, select Application Load Balancer (HTTP/HTTPS), and then click Next.

  4. In the Public facing or internal section, select Public facing (external), and then click Next.

  5. In the Global or single region deployment section, select Best for global workloads, and then click Next.

  6. In the Load balancer generation section, select Global external Application Load Balancer, and then click Next.

  7. Click Configure.

  8. In the Load Balancer name field, enter gemini-streamlit-app-lb.

To configure the load balancer, complete the Frontend configuration, Backend configuration, and Routing rules sections.

Frontend configuration

  1. In the Frontend configuration section, enter the following details:

    • Name: gemini-streamlit-app-frontend
    • Protocol: HTTPS (includes HTTP/2 and HTTP/3)
    • Network Service Tier: For the IP address, reserve a new external static IP address. Name the new IP address genai-app-ip.
    • Certificate: To create a new Google-managed certificate, follow these steps:

      1. Click Create a new certificate.
      2. Specify the following details:

        • Name: my-genai-app-certificate
        • Create mode: Create Google-managed certificate
        • Domains: Enter the domain name for provisioning the certificate.
      3. Click Create. For more information, see Use Google-managed SSL certificates.

    • Select the Enable HTTP to HTTPS redirect checkbox.

  2. Click Done.

  3. Click Backend configuration to continue.

Backend configuration

  1. In the Backend configuration section, click Backend services & backend buckets, and then click Create a backend service.
  2. Enter the following details for the backend service:

    • Name: gemini-streamlit-app-backend
    • Backend type: Serverless network endpoint group
  3. In the Backends section, on the New Backend card, create a serverless network endpoint group (NEG):

    1. Click Serverless network endpoint groups > Create serverless network endpoint group.
    2. Enter the following details:

      • Name: streamlit-app-neg
      • Region: us-central1
      • Serverless network endpoint group type: Cloud Run
      • Select service: Select the gemini-streamlit-cloudrun Cloud Run service.
    3. Click Create.

    4. Click Done to add the new backend to the backend service.

  4. Clear the Enable Cloud CDN checkbox.

  5. In the Policy name box, enter default-security-policy-gemini-app-backend.

  6. Click Create, and then click OK.

  7. Click Routing rules to continue.

Routing rules

  1. Click Advanced host and path rule.
  2. In the Host and path rules section, edit the default rule for any unmatched hosts:

    • Action: Select Route traffic to a single backend.
    • Backend: Select gemini-streamlit-app-backend.

  3. Click Done.

  4. Click Add host and path rule to add a new rule.

  5. Under New host and path rule, enter the following details:

    • Hosts: Enter the domain name that you used when creating the Google-managed certificate.
    • Path Matcher: Copy the following URL map configuration:

      defaultService: projects/PROJECT_ID/global/backendServices/gemini-streamlit-app-backend
      name: matcher1
      routeRules:
        - matchRules:
            - prefixMatch: /gemini-streamlit-app
          priority: PRIORITY
          routeAction:
            weightedBackendServices:
              - backendService: projects/PROJECT_ID/global/backendServices/gemini-streamlit-app-backend
                weight: 100
      

      Replace the following:

      • PROJECT_ID: Your Google Cloud project ID.

      • PRIORITY: Specify the priority of the backend service by which the route rules are evaluated. In this scenario, set this to any value because you created only one backend service.

  6. Click Done.

  7. Click Review and finalize to continue.

Review and finalize

  1. Review the load balancer configuration.
  2. To create the load balancer, click Create.

Add the IP address to the domain DNS records

Update your domain's DNS records to point to the IP address that you reserved for the load balancer. You might need to contact your domain administrator to complete this step.