Share via


Azure Application Gateway for Containers の URL 書き換え - ゲートウェイ API

Application Gateway for Containers を使用すると、要求のホスト名やパスなど、クライアント要求の URL を書き換えることができます。 Application Gateway for Containers でバックエンド ターゲットへの要求を開始すると、要求には、要求を開始するための新しく書き換えられた URL が含められます。

Usage details

URL 書き換えでは、Kubernetes Gateway API で定義されている フィルターを利用します。

背景

URL 書き換えを使用すると、バックエンド ターゲットに対して開始されたときに、受信要求を別の URL に変換できます。

次の図は、contoso.com/shop に宛てられた要求が contoso.com/ecommerce に書き直される例を示しています。 この要求は Application Gateway for Containers によってバックエンド ターゲットに対して開始されます。

A diagram showing the Application Gateway for Containers rewriting a URL to the backend.

前提条件

  1. BYO デプロイ戦略に従う場合は、Application Gateway for Containers リソースと ALB コントローラーを設定していることを確認します。

  2. ALB マネージド デプロイ戦略に従う場合は、ALB コントローラーのプロビジョニングと、ApplicationLoadBalancer カスタム リソースを介した Application Gateway for Containers リソースのプロビジョニングが完了していることを確認します。

  3. サンプル HTTP アプリケーションをデプロイする

    クラスターに次の deployment.yaml ファイルを適用して、リダイレクト機能を示すサンプル TLS 証明書をデプロイします。

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

    このコマンドによって、クラスターに次のものが作成されます。

    • test-infra と呼ばれる名前空間
    • test-infra 名前空間内の echo という 1 つのサービス
    • test-infra 名前空間内の echo という 1 つのデプロイ
    • test-infra 名前空間に listener-tls-secret と呼ばれる 1 つのシークレット

必要な Gateway API リソースをデプロイする

  1. ゲートウェイを作成する

    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
    

Note

ALB コントローラーは、ARM で Application Gateway for Containers リソースを作成するときに、フロントエンド リソースに対して次の名前付け規則を使用します。fe-<ランダムに生成された 8 文字>

Azure で作成されたフロントエンドの名前を変更したい場合は、BYO デプロイ戦略に従うことを検討してください。

ゲートウェイ リソースが作成されたら、状態が有効であること、リスナーが [プログラム済み] であること、ゲートウェイにアドレスが割り当てられていることを確認します。

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 リソースに [承認済み] と表示され、Application Gateway for Containers リソースが [プログラム済み] になっていることを確認します。

kubectl get httproute rewrite-example -n test-infra -o yaml

各 HTTPRoute で Application Gateway for Containers リソースが正常に更新されたことを確認します。

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/shop によって、contoso.com/ecommerce を示すバックエンド ターゲットへの要求されたパスと共に、バックエンド v1 サービスからの応答が返されるはずです。

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 によって、バックエンド 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"
}

これで、トラフィックが Application Gateway for Containers のターゲットに設定される前に、ALB コントローラーがインストールされ、バックエンド アプリケーションがデプロイされ、フィルター処理を使用してクライアントから要求された URL が書き換えられました。