0
0
GCPcloud~5 mins

URL maps for routing in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to send web traffic to different places based on the web address path, URL maps help by directing requests to the right backend service. This makes your website or app respond correctly to different URLs.
When you have a website with multiple sections and want to send traffic to different servers based on the URL path.
When you want to route traffic to different versions of your app for testing or gradual rollout.
When you want to send requests to different backend services depending on the URL, like sending /images to a storage service and /api to a compute service.
When you want to manage traffic routing rules in one place for your load balancer.
When you want to create simple rules to handle redirects or URL rewrites.
Config File - url-map.yaml
url-map.yaml
apiVersion: compute.googleapis.com/v1
kind: UrlMap
metadata:
  name: example-url-map
  description: "URL map to route traffic based on path"
spec:
  defaultService: https://www.googleapis.com/compute/v1/projects/example-project/global/backendServices/default-backend
  hostRules:
  - hosts:
    - "example.com"
    pathMatcher: path-matcher-1
  pathMatchers:
  - name: path-matcher-1
    defaultService: https://www.googleapis.com/compute/v1/projects/example-project/global/backendServices/default-backend
    pathRules:
    - paths:
      - "/images/*"
      service: https://www.googleapis.com/compute/v1/projects/example-project/global/backendServices/images-backend
    - paths:
      - "/api/*"
      service: https://www.googleapis.com/compute/v1/projects/example-project/global/backendServices/api-backend

This file defines a URL map resource for Google Cloud Load Balancing.

defaultService is where traffic goes if no rules match.

hostRules specify which hostnames this map applies to.

pathMatchers define rules to route traffic based on URL paths to different backend services.

Commands
Create a URL map named example-url-map with a default backend service to handle unmatched requests.
Terminal
gcloud compute url-maps create example-url-map --default-service https://www.googleapis.com/compute/v1/projects/example-project/global/backendServices/default-backend
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/example-project/global/urlMaps/example-url-map].
--default-service - Sets the backend service to use when no path rules match.
Add a path matcher to route /images/* and /api/* paths to specific backend services for the host example.com.
Terminal
gcloud compute url-maps add-path-matcher example-url-map --path-matcher-name=path-matcher-1 --default-service=https://www.googleapis.com/compute/v1/projects/example-project/global/backendServices/default-backend --path-rules=/images/*=https://www.googleapis.com/compute/v1/projects/example-project/global/backendServices/images-backend,/api/*=https://www.googleapis.com/compute/v1/projects/example-project/global/backendServices/api-backend --hosts=example.com
Expected OutputExpected
Updated [https://www.googleapis.com/compute/v1/projects/example-project/global/urlMaps/example-url-map].
--path-matcher-name - Names the set of path rules.
--path-rules - Defines URL path patterns and their backend services.
--hosts - Specifies which hostnames this path matcher applies to.
Check the current configuration of the URL map to verify the routing rules.
Terminal
gcloud compute url-maps describe example-url-map
Expected OutputExpected
creationTimestamp: '2024-06-01T12:00:00.000-07:00' id: '1234567890123456789' kind: compute#urlMap name: example-url-map pathMatchers: - defaultService: https://www.googleapis.com/compute/v1/projects/example-project/global/backendServices/default-backend name: path-matcher-1 pathRules: - paths: - /images/* service: https://www.googleapis.com/compute/v1/projects/example-project/global/backendServices/images-backend - paths: - /api/* service: https://www.googleapis.com/compute/v1/projects/example-project/global/backendServices/api-backend hostRules: - hosts: - example.com pathMatcher: path-matcher-1 defaultService: https://www.googleapis.com/compute/v1/projects/example-project/global/backendServices/default-backend selfLink: https://www.googleapis.com/compute/v1/projects/example-project/global/urlMaps/example-url-map
Key Concept

If you remember nothing else from this pattern, remember: URL maps let you send web requests to different backend services based on the URL path or hostname.

Common Mistakes
Not specifying a default backend service when creating the URL map.
Without a default service, requests that don't match any path rules will fail or be dropped.
Always set a default backend service to handle unmatched requests.
Using incorrect URL path patterns that do not match the intended requests.
Traffic will not be routed correctly if the path patterns do not match the actual URLs users visit.
Use correct wildcard patterns like /images/* to match all paths under /images.
Forgetting to specify the hostnames in host rules when routing by hostname.
The URL map will not apply the path matcher to the intended host, causing routing failures.
Always include the --hosts flag with the correct domain names.
Summary
Create a URL map with a default backend service to catch unmatched requests.
Add path matchers to route specific URL paths to different backend services.
Verify the URL map configuration to ensure routing rules are correct.