📖 Scenario: You are setting up a simple web application on Google Cloud Platform (GCP). You want to route incoming web traffic to different backend services based on the URL path. For example, requests to /app1 should go to one backend, and requests to /app2 should go to another.
🎯 Goal: Create a URL map configuration that routes traffic to two backend services based on URL paths /app1/* and /app2/*. This URL map will be used by a load balancer to direct traffic correctly.
📋 What You'll Learn
Create a URL map resource named my-url-map.
Define a path matcher with rules for /app1/* and /app2/*.
Route /app1/* traffic to backend service backend-service-app1.
Route /app2/* traffic to backend service backend-service-app2.
Set a default backend service backend-service-default for all other traffic.
💡 Why This Matters
🌍 Real World
URL maps are used in GCP load balancers to route web traffic to different backend services based on URL paths or hostnames. This helps organize and scale web applications.
💼 Career
Understanding URL maps is essential for cloud engineers and architects managing traffic routing and load balancing in GCP environments.
Progress0 / 4 steps
1
Create the basic URL map structure
Create a YAML file named url-map.yaml with a resource of kind compute.v1.urlMap and name it my-url-map. Include an empty pathMatchers list and a defaultService field set to backend-service-default.
GCP
Hint
Start by defining the URL map resource with the required name and default backend service.
2
Add path matcher for /app1/*
Add a path matcher to the pathMatchers list with name main-matcher. Set defaultService to backend-service-default and add a pathRules entry that matches /app1/* and routes to backend-service-app1.
GCP
Hint
Define a path matcher with the correct name, default service backend-service-default, and a path rule for /app1/* to backend-service-app1.
3
Add path rule for /app2/*
Add another pathRules entry to the main-matcher that matches /app2/* and routes to backend-service-app2.
GCP
Hint
Add a second path rule for /app2/* to the pathRules list in main-matcher.
4
Set host rules to use path matcher
Add a hostRules section to the URL map spec. Create one host rule with hosts set to ['*'] and pathMatcher set to main-matcher. This will route all hosts to the path matcher.
GCP
Hint
Host rules connect hosts to path matchers. Use wildcard host '*' to route all traffic to main-matcher.
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
Step 1: Understand URL map role
A URL map directs web traffic to backend services depending on URL patterns.
Step 2: Identify correct function
Routing based on URL paths or hosts matches the description of URL maps.
Final Answer:
To route incoming web traffic to different backend services based on URL paths or hosts -> Option C
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
Step 1: Identify correct key for multiple path matchers
The key for multiple path matchers is plural: "pathMatchers" with an array value.
Step 2: Confirm value type
The value must be an array of objects, so square brackets [] are needed.
Final Answer:
"pathMatchers": [{"name": "pm1", "defaultService": "backend1"}] -> Option B
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?
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?