你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
适用于容器的 Azure 应用程序网关的标头重写 - 网关 API
使用适用于容器的应用程序网关,你可以重写来自后端目标的客户端请求和响应的 HTTP 标头。
使用情况详细信息
标头重写利用 Kubernetes 网关 API 定义的筛选器。
背景
标头重写使你能够修改发往和来自后端目标的请求和响应标头。
下图展示了当适用于容器的应用程序网关向后端目标发起请求时,具有特定用户代理的请求被重写为名为 SearchEngine BingBot 的简化值:
先决条件
如果遵循 BYO 部署策略,请确保设置适用于容器的应用程序网关资源和 ALB 控制器
如果你遵循 ALB 托管部署策略,请确保通过 ApplicationLoadBalancer 自定义资源预配 ALB 控制器和适用于容器的应用程序网关资源。
部署示例 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: IPAddress
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
创建网关后,创建一个 HTTPRoute,用于侦听主机名 contoso.com 并将用户代理值重写为 SearchEngine-BingBot。
在此示例中,我们将查找必应搜索引擎使用的用户代理,并将标头简化为 SearchEngine-BingBot,以便更轻松地进行后端分析。
此示例还演示了添加名为 AGC-Header-Add
且值为 AGC-value
的新标头,并移除名为 client-custom-header
的请求标头。
提示
对于此示例,虽然我们可以使用“Exact”作为 HTTPHeaderMatch 的值来进行字符串匹配,但演示使用了正则表达式来展示更多功能。
kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: header-rewrite-route
namespace: test-infra
spec:
parentRefs:
- name: gateway-01
namespace: test-infra
hostnames:
- "contoso.com"
rules:
- matches:
- headers:
- name: user-agent
value: Mozilla/5\.0 AppleWebKit/537\.36 \(KHTML, like Gecko; compatible; bingbot/2\.0; \+http://www\.bing\.com/bingbot\.htm\) Chrome/
type: RegularExpression
filters:
- type: RequestHeaderModifier
requestHeaderModifier:
set:
- name: user-agent
value: SearchEngine-BingBot
add:
- name: AGC-Header-Add
value: AGC-value
remove: ["client-custom-header"]
backendRefs:
- name: backend-v2
port: 8080
- backendRefs:
- name: backend-v1
port: 8080
EOF
创建 HTTPRoute 资源后,请确保路由被接受,并且适用于容器的应用程序网关已编程。
kubectl get httproute header-rewrite-route -n test-infra -o yaml
验证是否已成功更新适用于容器的应用程序网关资源的状态。
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
,则输出应当返回来自 backend-v1 服务的响应。
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": [
"dcd4bcad-ea43-4fb6-948e-a906380dcd6d"
]
},
"namespace": "test-infra",
"ingress": "",
"service": "",
"pod": "backend-v1-5b8fd96959-f59mm"
}
使用值“”指定用户代理标头应当返回来自 SearchEngine-BingBot 的后端服务的响应:
fqdnIp=$(dig +short $fqdn)
curl -k --resolve contoso.com:80:$fqdnIp http://contoso.com -H "user-agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm) Chrome/"
通过响应,应会看到:
{
"path": "/",
"host": "fabrikam.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-5b8fd96959-f59mm"
}
当适用于容器的应用程序网关启动与后端服务的连接时,应从请求中去除具有值 moo
的 client-custom-header
标头:
fqdnIp=$(dig +short $fqdn)
curl -k --resolve contoso.com:80:$fqdnIp http://contoso.com -H "client-custom-header: moo"
通过响应,应会看到:
{
"path": "/",
"host": "fabrikam.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": [
"kd83nc84-4325-5d22-3d23-237dd4e3941b"
]
},
"namespace": "test-infra",
"ingress": "",
"service": "",
"pod": "backend-v2-5b8fd96959-f59mm"
}
恭喜,你已安装 ALB 控制器,部署了后端应用程序,并通过适用于容器的应用程序网关上的网关 API 修改了标头值。