Bird
Raised Fist0
GCPcloud~5 mins

URL maps for routing in GCP - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a URL map in Google Cloud Platform's load balancing?
easy
A. To store user data for web applications
B. To create virtual machines automatically
C. To route incoming web traffic to different backend services based on URL paths or hosts
D. To monitor network traffic usage

Solution

  1. Step 1: Understand URL map role

    A URL map directs web traffic to backend services depending on URL patterns.
  2. Step 2: Identify correct function

    Routing based on URL paths or hosts matches the description of URL maps.
  3. Final Answer:

    To route incoming web traffic to different backend services based on URL paths or hosts -> Option C
  4. Quick Check:

    URL maps route traffic = B [OK]
Hint: URL maps decide backend by URL pattern [OK]
Common Mistakes:
  • Confusing URL maps with storage or VM services
  • Thinking URL maps monitor traffic instead of routing
  • Assuming URL maps create resources automatically
2. Which of the following is the correct syntax to define a path matcher in a GCP URL map configuration?
easy
A. "pathMatcher": {"name": "pm1", "defaultService": "backend1"}
B. "pathMatchers": [{"name": "pm1", "defaultService": "backend1"}]
C. "pathMatchers": {"name": "pm1", "defaultService": "backend1"}
D. "pathMatcher": [{"name": "pm1", "defaultService": "backend1"}]

Solution

  1. Step 1: Identify correct key for multiple path matchers

    The key for multiple path matchers is plural: "pathMatchers" with an array value.
  2. Step 2: Confirm value type

    The value must be an array of objects, so square brackets [] are needed.
  3. Final Answer:

    "pathMatchers": [{"name": "pm1", "defaultService": "backend1"}] -> Option B
  4. Quick Check:

    Multiple path matchers use "pathMatchers" array = A [OK]
Hint: Use plural "pathMatchers" with array brackets [OK]
Common Mistakes:
  • Using singular "pathMatcher" for multiple entries
  • Using object instead of array for pathMatchers
  • Mixing singular/plural keys incorrectly
3. Given this URL map snippet, which backend service will handle the request to https://example.com/images/logo.png?
{
  "defaultService": "backend-default",
  "pathMatchers": [
    {
      "name": "pm-images",
      "defaultService": "backend-images",
      "pathRules": [
        {"paths": ["/images/*"], "service": "backend-images"}
      ]
    }
  ]
}
medium
A. backend-images
B. No backend matches, request fails
C. backend-logo
D. backend-default

Solution

  1. Step 1: Check path rules for "/images/*"

    The path matcher "pm-images" has a path rule matching "/images/*" directing to "backend-images".
  2. Step 2: Match request URL path

    The request path "/images/logo.png" matches the "/images/*" pattern, so it routes to "backend-images".
  3. Final Answer:

    backend-images -> Option A
  4. Quick Check:

    Path "/images/*" matches request = backend-images [OK]
Hint: Match URL path to pathRules first, else defaultService [OK]
Common Mistakes:
  • Choosing defaultService without checking pathRules
  • Assuming no match causes failure instead of default routing
  • Confusing path patterns with exact matches
4. You have this URL map configuration snippet:
{
  "defaultService": "backend-default",
  "pathMatchers": [
    {
      "name": "pm1",
      "defaultService": "backend1",
      "pathRules": [
        {"paths": ["/app/*"], "service": "backend-app"}
      ]
    }
  ]
}
Why does a request to https://example.com/app go to backend-default instead of backend-app?
medium
A. The defaultService overrides all pathRules
B. The path matcher name "pm1" is invalid
C. The backend service "backend-app" is not defined
D. The path "/app" does not match the pattern "/app/*" which requires a trailing slash

Solution

  1. Step 1: Understand path pattern matching

    The pattern "/app/*" matches paths starting with "/app/" but not "/app" exactly.
  2. Step 2: Check request path against pattern

    The request path "/app" lacks the trailing slash, so it does not match "/app/*" and falls back to defaultService.
  3. Final Answer:

    The path "/app" does not match the pattern "/app/*" which requires a trailing slash -> Option D
  4. Quick Check:

    Trailing slash matters in path patterns = C [OK]
Hint: Path patterns with * need trailing slash to match [OK]
Common Mistakes:
  • Assuming "/app" matches "/app/*" without slash
  • Thinking defaultService always overrides pathRules
  • Believing backend service must be explicitly defined to route
5. You want to route requests to https://example.com/api/v1/users to backend-v1 and requests to https://example.com/api/v2/users to backend-v2. Which URL map configuration correctly achieves this?
hard
A. { "defaultService": "backend-default", "pathMatchers": [ { "name": "pm-api", "defaultService": "backend-default", "pathRules": [ {"paths": ["/api/v1/*"], "service": "backend-v1"}, {"paths": ["/api/v2/*"], "service": "backend-v2"} ] } ] }
B. { "defaultService": "backend-default", "pathMatchers": [ { "name": "pm-api", "defaultService": "backend-v1", "pathRules": [ {"paths": ["/api/v1/*"], "service": "backend-v1"}, {"paths": ["/api/v2/*"], "service": "backend-v2"} ] } ] }
C. { "defaultService": "backend-default", "pathMatchers": [ { "name": "pm-api", "defaultService": "backend-v2", "pathRules": [ {"paths": ["/api/v1/*"], "service": "backend-v1"} ] } ] }
D. { "defaultService": "backend-default", "pathMatchers": [ { "name": "pm-api", "defaultService": "backend-default", "pathRules": [ {"paths": ["/api/*"], "service": "backend-v1"}, {"paths": ["/api/*"], "service": "backend-v2"} ] } ] }

Solution

  1. Step 1: Check path rules for specific version routing

    { "defaultService": "backend-default", "pathMatchers": [ { "name": "pm-api", "defaultService": "backend-default", "pathRules": [ {"paths": ["/api/v1/*"], "service": "backend-v1"}, {"paths": ["/api/v2/*"], "service": "backend-v2"} ] } ] } defines two distinct path rules: "/api/v1/*" to "backend-v1" and "/api/v2/*" to "backend-v2" which matches requirements.
  2. Step 2: Verify defaultService and pathRules consistency

    { "defaultService": "backend-default", "pathMatchers": [ { "name": "pm-api", "defaultService": "backend-default", "pathRules": [ {"paths": ["/api/v1/*"], "service": "backend-v1"}, {"paths": ["/api/v2/*"], "service": "backend-v2"} ] } ] } uses "backend-default" as defaultService, so unmatched paths go there, which is correct.
  3. Step 3: Identify issues in other options

    { "defaultService": "backend-default", "pathMatchers": [ { "name": "pm-api", "defaultService": "backend-v1", "pathRules": [ {"paths": ["/api/v1/*"], "service": "backend-v1"}, {"paths": ["/api/v2/*"], "service": "backend-v2"} ] } ] } sets defaultService to "backend-v1" which may misroute unmatched paths. { "defaultService": "backend-default", "pathMatchers": [ { "name": "pm-api", "defaultService": "backend-v2", "pathRules": [ {"paths": ["/api/v1/*"], "service": "backend-v1"} ] } ] } misses "/api/v2/*" pathRule. { "defaultService": "backend-default", "pathMatchers": [ { "name": "pm-api", "defaultService": "backend-default", "pathRules": [ {"paths": ["/api/*"], "service": "backend-v1"}, {"paths": ["/api/*"], "service": "backend-v2"} ] } ] } duplicates "/api/*" paths causing conflict.
  4. Final Answer:

    Option A correctly routes /api/v1/* and /api/v2/* to respective backends -> Option A
  5. Quick Check:

    Distinct pathRules for versions = A [OK]
Hint: Use distinct pathRules for each version prefix [OK]
Common Mistakes:
  • Setting defaultService to one version backend
  • Missing pathRule for one version
  • Duplicating same path pattern causing conflicts
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard