你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
适用于容器的 Azure 应用程序网关的 URL 重写 - 网关 API
使用适用于容器的应用程序网关可以重写客户端请求的 URL,包括请求的主机名和/或路径。 当适用于容器的应用程序网关向后端目标发起请求时,请求将包含新重写的 URL 来启动请求。
使用情况详细信息
URL 重写利用 Kubernetes 网关 API 定义的筛选器。
背景
通过 URL 重写,可以在启动到后端目标时将传入请求转换为其他 URL。
下图演示了一个请求示例,用于 contoso.com/shop 的请求被重写为 contoso.com/ecommerce。 请求由用于容器的应用程序网关启动到后端目标:
先决条件
如果遵循 BYO 部署策略,请确保设置适用于容器的应用程序网关资源和 ALB 控制器。
如果遵循 ALB 托管部署策略,请确保预配 ALB 控制器,并通过 ApplicationLoadBalancer 自定义资源预配适用于容器的应用程序网关资源。
部署示例 HTTP 应用程序:
在群集上应用以下deployment.yaml 文件来创建示例 Web 应用程序,以演示流量拆分/加权轮循机制支持。
kubectl apply -f https://raw.githubusercontent.com/MicrosoftDocs/azure-docs/refs/heads/main/articles/application-gateway/for-containers/examples/traffic-split-scenario/deployment.yaml
此命令在群集上创建以下内容:
- 名为
test-infra
的命名空间 test-infra
命名空间中名为backend-v1
和backend-v2
的两个服务test-infra
命名空间中名为backend-v1
和backend-v2
的两个部署
- 名为
部署所需的网关 API 资源
创建网关
kubectl apply -f - <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: gateway-01 namespace: test-infra annotations: alb.networking.azure.io/alb-namespace: alb-test-infra alb.networking.azure.io/alb-name: alb-test spec: gatewayClassName: azure-alb-external listeners: - name: http-listener port: 80 protocol: HTTP allowedRoutes: namespaces: from: Same EOF
注意
当 ALB 控制器在 ARM 中创建适用于容器的应用程序网关时,它将对前端资源使用以下命名约定:fe-<8 个随机生成的字符>
如果要更改在 Azure 中创建的前端的名称,请考虑按照创建自己的部署策略中的说明进行操作。
创建网关资源后,请确保状态有效,侦听器已编程,并地址已分配给网关。
kubectl get gateway gateway-01 -n test-infra -o yaml
成功创建了网关时的示例输出。
status:
addresses:
- type: Hostname
value: xxxx.yyyy.alb.azure.com
conditions:
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Valid Gateway
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Application Gateway For Containers resource has been successfully updated.
observedGeneration: 1
reason: Programmed
status: "True"
type: Programmed
listeners:
- attachedRoutes: 0
conditions:
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: ""
observedGeneration: 1
reason: ResolvedRefs
status: "True"
type: ResolvedRefs
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Listener is accepted
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Application Gateway For Containers resource has been successfully updated.
observedGeneration: 1
reason: Programmed
status: "True"
type: Programmed
name: https-listener
supportedKinds:
- group: gateway.networking.k8s.io
kind: HTTPRoute
创建网关后,为 contoso.com
创建 HTTPRoute 资源。 此示例确保发送到 contoso.com/shop
的流量作为后端目标的 contoso.com/ecommerce
启动。
kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: rewrite-example
namespace: test-infra
spec:
parentRefs:
- name: gateway-01
hostnames:
- "contoso.com"
rules:
- matches:
- path:
type: PathPrefix
value: /shop
filters:
- type: URLRewrite
urlRewrite:
path:
type: ReplacePrefixMatch
replacePrefixMatch: /ecommerce
backendRefs:
- name: backend-v1
port: 8080
- backendRefs:
- name: backend-v2
port: 8080
EOF
创建 HTTPRoute 资源后,请确保 HTTPRoute 资源显示为“已接受”,并且适用于容器的应用程序网关为“已编程”。
kubectl get httproute rewrite-example -n test-infra -o yaml
验证是否已为每个 HTTPRoute 成功更新适用于容器的应用程序网关资源。
status:
parents:
- conditions:
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: ""
observedGeneration: 1
reason: ResolvedRefs
status: "True"
type: ResolvedRefs
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: Route is Accepted
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: Application Gateway For Containers resource has been successfully updated.
observedGeneration: 1
reason: Programmed
status: "True"
type: Programmed
controllerName: alb.networking.azure.io/alb-controller
parentRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-01
namespace: test-infra
测试对应用程序的访问
现在,我们已准备好通过分配给前端的 FQDN 将一些流量发送到示例应用程序。 使用以下命令获取 FQDN。
fqdn=$(kubectl get gateway gateway-01 -n test-infra -o jsonpath='{.status.addresses[0].value}')
如果你使用 curl 命令为前端 FQDN 指定了服务器名称指示器,则 contoso.com/shop
应当返回来自 backend-v1 服务的响应,其中显示 contoso.com/ecommerce
。
fqdnIp=$(dig +short $fqdn)
curl -k --resolve contoso.com:80:$fqdnIp http://contoso.com/shop
通过响应,应会看到:
{
"path": "/ecommerce",
"host": "contoso.com",
"method": "GET",
"proto": "HTTP/1.1",
"headers": {
"Accept": [
"*/*"
],
"User-Agent": [
"curl/7.81.0"
],
"X-Forwarded-For": [
"xxx.xxx.xxx.xxx"
],
"X-Forwarded-Proto": [
"http"
],
"X-Request-Id": [
"dcd4bcad-ea43-4fb6-948e-a906380dcd6d"
]
},
"namespace": "test-infra",
"ingress": "",
"service": "",
"pod": "backend-v1-5b8fd96959-f59mm"
}
接下来,使用 curl 命令指定服务器名称指示器,contoso.com
应从 backend-v2 服务返回响应。
fqdnIp=$(dig +short $fqdn)
curl -k --resolve contoso.com:80:$fqdnIp http://contoso.com
通过响应,应会看到:
{
"path": "/",
"host": "contoso.com",
"method": "GET",
"proto": "HTTP/1.1",
"headers": {
"Accept": [
"*/*"
],
"User-Agent": [
"curl/7.81.0"
],
"X-Forwarded-For": [
"xxx.xxx.xxx.xxx"
],
"X-Forwarded-Proto": [
"http"
],
"X-Request-Id": [
"adae8cc1-8030-4d95-9e05-237dd4e3941b"
]
},
"namespace": "test-infra",
"ingress": "",
"service": "",
"pod": "backend-v2-594bd59865-ppv9w"
}
恭喜,你已安装 ALB 控制器并部署了一个后端应用程序,其中包括可以重写客户端请求的 URL 的筛选功能。 适用于容器的应用程序网关上的目标已准备好接收流量。