google-cloud-platformgoogle-cloud-rungcp-load-balancer

default service for GCP load balancer


I'm sure the solution to my issue is somewhere in the documentation, but I cannot figure out how to configure a default service that gets used when setting up my global external load balancer on GCP. It might be relevant to mention that all my services behind the load balancer are Cloud Run services.

I have my base URL, say example.com, and a bunch of services that are attached to it. For example, I have a example.com/login and a example.com/api. This part works perfectly fine so far. Now I would like to add a "default" service that gets called when the user access the base URL.

What I have (and is working as expected) is something like this:

gcloud compute network-endpoint-groups create $SERVERLESS_NEG_NAME \
 --region=$REGION \
 --network-endpoint-type=serverless \
 --cloud-run-url-mask="${BASE_URL}/<service>"

# create backend service
gcloud compute backend-services create $BACKEND_SERVICE_NAME \
 --load-balancing-scheme=EXTERNAL_MANAGED \
 --global

# add serverless NEG to backend service
gcloud compute backend-services add-backend $BACKEND_SERVICE_NAME \
 --global \
 --network-endpoint-group=$SERVERLESS_NEG_NAME \
 --network-endpoint-group-region=$REGION

# create URL map with just one backend service
gcloud compute url-maps create $URL_MAP_NAME \
 --default-service=$BACKEND_SERVICE_NAME

How would I add a default service (e.g. default-service) which gets forwarded to when the base URL is accessed?


Solution

  • With the help of the support team from Google I managed to resolve the issue. Here's what I did, maybe it'll help somebody in the future:

    For the requests to the base URL, e.g. example.com, to be forwarded to a specific cloud run service, a second backend service and a URL path matcher needs to be created. This could looks something like this:

    gcloud compute network-endpoint-groups create $SERVERLESS_NEG_NAME \
     --region=$REGION \
     --network-endpoint-type=serverless \
     --cloud-run-url-mask="${BASE_URL}/<service>"
    gcloud compute network-endpoint-groups create $SERVERLESS_NEG_DEFAULT_NAME \
     --region=$REGION \
     --network-endpoint-type=serverless \
     --cloud-run-service=$UI_SERVICE_NAME
    
    # create backend service
    gcloud compute backend-services create $BACKEND_SERVICE_NAME \
     --load-balancing-scheme=EXTERNAL_MANAGED \
     --global
    gcloud compute backend-services create $BACKEND_SERVICE_BASE_NAME \
     --load-balancing-scheme=EXTERNAL_MANAGED \
     --global
    
    # add serverless NEG to backend service
    gcloud compute backend-services add-backend $BACKEND_SERVICE_NAME \
     --global \
     --network-endpoint-group=$SERVERLESS_NEG_NAME \
     --network-endpoint-group-region=$REGION
    gcloud compute backend-services add-backend $BACKEND_SERVICE_BASE_NAME \
     --global \
     --network-endpoint-group=$SERVERLESS_NEG_DEFAULT_NAME \
     --network-endpoint-group-region=$REGION
    
    # create URL map with just one backend service
    gcloud compute url-maps create $URL_MAP_NAME \
     --default-service=$BACKEND_SERVICE_NAME
    gcloud compute url-maps create $URL_MAP_BASE_NAME \
     --default-service=$BACKEND_SERVICE_BASE_NAME
    
    # create a path matcher for the URL map
    gcloud compute url-maps add-path-matcher $URL_MAP_NAME \
     --path-matcher-name=$PATH_MATCHER_NAME \
     --default-service=$BACKEND_SERVICE_NAME \
     --path-rules="/=${BACKEND_SERVICE_BASE_NAME}"
    

    This works for me, but it might well be that it's not the best way of doing things.