Share via


適用於容器的 Azure 應用程式閘道標頭重寫 - 閘道 API

適用於容器的應用程式閘道可用於重寫來自後端目標的用戶端要求和回應的 HTTP 標頭。

使用方式詳細資料

標頭重寫會利用 Kubernetes 閘道 API 所定義的篩選條件 (英文)。

背景

透過標頭重寫,即可修改往返於後端目標的要求和回應標頭。

下圖示範當適用於容器的應用程式閘道向後端目標發起要求時,特定使用者代理程式的要求重寫為 SearchEngine-BingBot 的簡化值:

此圖顯示容器的 應用程式閘道,將要求標頭重寫至後端。

必要條件

  1. 如果遵循 BYO 部署策略,則請確定您已設定適用於容器的應用程式閘道資源和 ALB 控制器

  2. 如果您遵循 ALB 受控部署策略,則請確定透過 ApplicationLoadBalancer 自訂資源來佈建 ALB 控制器和適用於容器的應用程式閘道資源。

  3. 部署範例 HTTP 應用程式:在叢集上套用下列 deployment.yaml 檔案,以建立範例 Web 應用程式來示範標頭重寫。

    kubectl apply -f https://trafficcontrollerdocs.blob.core.windows.net/examples/traffic-split-scenario/deployment.yaml
    

    此指令會在您的叢集上建立下列物件:

    • 名為 test-infra 的命名空間
    • test-infra 命名空間中的兩個服務,分別名為 backend-v1backend-v2
    • test-infra 命名空間中的兩個部署,分別名為 backend-v1backend-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 中建立適用於容器的應用程式閘道資源時,ALB 控制器會針對前端資源使用下列命名慣例: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。

在此範例中,我們會尋找 Bing 搜尋引擎所使用的使用者代理程式,並簡化 SearchEngine-BingBot 的標頭,以方便後端剖析。

此範例還演示了如何新增一個名為 AGC-Header-Add、值為 AGC-value 的新標頭並移除名為 client-custom-header 的要求標頭。

提示

在此範例中,雖然我們可以使用 HTTPHeaderMatch 的 "Exact" 參數進行字符串比對,但為了展示其他功能,我們以規則運算式進行演示。

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 命令來指定伺服器名稱指標,並指定 contoso.com 作為前端 FQDN,輸出應該會傳回來自 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"
}

client-custom-header當容器 應用程式閘道 起始後端服務的連線時,應從要求中移除具有 值的moo標頭:

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 修改了標頭值。