次の方法で共有


Application Gateway for Containers を使用したトラフィックの分割 - ゲートウェイ API

このドキュメントは、Gateway API から次のリソースを使用するサンプル アプリケーションを設定するのに役立ちます。

  • ゲートウェイ - 1 つの http リスナーを持つゲートウェイの作成
  • HTTPRoute - 重みが異なる 2 つのバックエンド サービスを参照する HTTP ルートの作成

バックグラウンド

Application Gateway for Containers を使用すると、重みを設定し、異なるバックエンド ターゲット間でトラフィックをシフトできます。 次のシナリオ例を見てみましょう。

Application Gateway for Containers でのトラフィックの分割を示す図。

[前提条件]

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

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

  3. サンプル 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 という名前空間
    • backend-v1 名前空間の backend-v2 および test-infra と呼ばれる 2 つのサービス
    • backend-v1 名前空間の backend-v2 および test-infra と呼ばれる 2 つの展開

必要な Gateway 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
    port: 80
    protocol: HTTP
    allowedRoutes:
      namespaces:
        from: Same
EOF

ALB コントローラーは、Azure Resource Manager で Application Gateway for Containers リソースを作成するときに、フロントエンド リソースに対して次の名前付け規則 ( fe-<eight randomly generated characters>) を使用します。

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: gateway-01-http
    supportedKinds:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute

ゲートウェイが作成されたら、HTTPRoute を作成します。

kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: traffic-split-route
  namespace: test-infra
spec:
  parentRefs:
  - name: gateway-01
  rules:
  - backendRefs:
    - name: backend-v1
      port: 8080
      weight: 50
    - name: backend-v2
      port: 8080
      weight: 50
EOF

HTTPRoute リソースが作成されたら、ルートが [承認済み] になり、Application Gateway for Containers リソースが [プログラム済み] になっていることを確かめます。

kubectl get httproute traffic-split-route -n test-infra -o yaml

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}')

この FQDN をカーリングすると、HTTPRoute で構成されているバックエンド/ポッドからの応答が返されます。

# this curl command will return 50% of the responses from backend-v1
# and the remaining 50% of the responses from backend-v2
watch -n 1 curl http://$fqdn

これで、ALB コントローラーをインストールし、バックエンド アプリケーションをデプロイして、Application Gateway for Containers のイングレス経由でアプリケーションにトラフィックをルーティングすることができました。