Using a Global Load Balancer with Cloud Run
Configure the Environment
- Enable Cloud Run API
gcloud services enable run.googleapis.com
- Set the compute and run regions
gcloud config set compute/region us-central1 gcloud config set run/region us-central1
- Create a LOCATION environment variable
LOCATION="us-central1"
Write a simple application
- In Cloud Shell create a new directory namedÂ
helloworld
, then move your view into that directory:
mkdir helloworld && cd helloworld - Next you’ll be creating and editing files. To edit files, useÂ
vi
,Âemac
,Ânano
 or the Cloud Shell Editor by clicking on the pencil icon in Cloud Shell (“Open Editor”).
- Create aÂ
main.py
 file, then add the following content to it:import os from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): name = os.environ.get("NAME", "World") return "Hello {}!".format(name) if __name__ == "__main__": app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
Containerize your app and upload it to Container Registry (Artifact Registry)
- To containerize the sample app you just made, create a new file namedÂ
Dockerfile
 in the same directory as theÂmain.py
 file, and add the following content :# Use the official lightweight Python image. # https://hub.docker.com/_/python FROM python:3.9-slim # Allow statements and log messages to immediately appear in the Knative logs ENV PYTHONUNBUFFERED True # Copy local code to the container image. ENV APP_HOME /app WORKDIR $APP_HOME COPY . ./ # Install production dependencies. RUN pip install Flask gunicorn # Run the web service on container startup. Here we use the gunicorn # webserver, with one worker process and 8 threads. # For environments with multiple CPU cores, increase the number of workers # to be equal to the cores available. # Timeout is set to 0 to disable the timeouts of the workers to allow Cloud Run to handle instance scaling. CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
- Now, build your container image using Cloud Build, by running the following command from the directory containing the Dockerfile:
gcloud builds submit --tag gcr.io/$GOOGLE_CLOUD_PROJECT/helloworld
Deploy your container to Cloud Run
- Now that we have our containerized image, we will need to use theÂ
gcloud
 command below to deploy it to Cloud Run. Replace PROJECT-ID with your GCP project ID. You can view your project ID by running the commandÂgcloud config get-value project.
gcloud run deploy --image gcr.io/$GOOGLE_CLOUD_PROJECT/helloworld
- You will then be prompted for the service name: press Enter to accept the default name,Â
helloworld
. - If prompted for region: in this case, select us-central1.
- Visit your deployed container by opening the service URL in a web browser.
Reserve an external IP address
- Use the following command to reserve your static IP address:
gcloud compute addresses create example-ip \ --ip-version=IPV4 \ --global
- To display the IP address that was just reserved, use:
gcloud compute addresses describe example-ip \ --format="get(address)" \ --global
Create the external HTTP load balancer
- To create a serverless NEG with a Cloud Run service, enter the following in Cloud Shell:
gcloud compute network-endpoint-groups create myneg \ --region=$LOCATION \ --network-endpoint-type=serverless \ --cloud-run-service=helloworld
- Now, we need to create a backend service:
gcloud compute backend-services create mybackendservice \ --global
- And then add the serverless NEG as a backend to this backend service:
gcloud compute backend-services add-backend mybackendservice \ --global \ --network-endpoint-group=myneg \ --network-endpoint-group-region=$LOCATION
- Finally, create a URL map to route incoming requests to the backend service:
gcloud compute url-maps create myurlmap \ --default-service mybackendservice
- Now, create a target HTTP(S) proxy to route requests to your URL map:
gcloud compute target-http-proxies create mytargetproxy \ --url-map=myurlmap
- Create a global forwarding rule to route incoming requests to the proxy:
gcloud compute forwarding-rules create myforwardingrule \ --address=example-ip \ --target-http-proxy=mytargetproxy \ --global \ --ports=80
- You can test your HTTP load balancer by going toÂ
http://IP_ADDRESS
, whereÂIP_ADDRESS
 is the load balancer’s IP address you reserved earlier in this lab. When you open this URL, you should see the helloworld service homepage.
Tag:Google Cloud