次の方法で共有


Application Gateway for Containers を使用したマルチサイト ホスティング - Gateway API

このドキュメントでは、ゲートウェイ API のリソースを使用して、同じ Kubernetes Gateway リソース/Application Gateway for Containers フロントエンドで複数のサイトをホストする方法を示すサンプル アプリケーションを設定するのに役立ちます。 以下を行うための手順を示します。

  • 1 つの HTTP リスナーを使用して ゲートウェイ リソースを作成します。
  • それぞれが一意のバックエンド サービスを参照する 2 つの HTTPRoute リソースを作成します。

バックグラウンド

Application Gateway for Containers を使用することで、同じポートで複数の Web アプリケーションを構成できることにより、複数サイトをホストできるようになります。 一意のバックエンド サービスを使用して、2 つ以上の一意のサイトをホストできます。 次のシナリオ例を見てみましょう。

Application Gateway for Containers でのマルチサイト ホスティングを示す図。

[前提条件]

  1. BYO デプロイ戦略を使用した場合は、Application Gateway for Containers リソースと ALB コントローラーを必ず設定してください。

  2. ALB マネージド デプロイ戦略を使用した場合は、ApplicationLoadBalancer カスタム リソースを使用して、ALB コントローラーをプロビジョニングし、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 リソースを展開する

  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

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

ゲートウェイが作成されたら、 contoso.comfabrikam.com ドメイン名の 2 つの HTTPRoute リソースを作成します。 各ドメインは、トラフィックを異なるバックエンド サービスに転送します。

kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: contoso-route
  namespace: test-infra
spec:
  parentRefs:
  - name: gateway-01
  hostnames:
  - "contoso.com"
  rules:
  - backendRefs:
    - name: backend-v1
      port: 8080
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: fabrikam-route
  namespace: test-infra
spec:
  parentRefs:
  - name: gateway-01
  hostnames:
  - "fabrikam.com"
  rules:
  - backendRefs:
    - name: backend-v2
      port: 8080
EOF

HTTPRoute リソースが作成されたら、両方の HTTPRoute リソースが [Accepted] と表示され、Application Gateway for Containers リソースが プログラミングされていることを確認します。

kubectl get httproute contoso-route -n test-infra -o yaml
kubectl get httproute fabrikam-route -n test-infra -o yaml

Application Gateway for Containers リソースの状態が 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 、バックエンド 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"
}

curl コマンドを使用してサーバー名インジケーターを指定すると、フロントエンド FQDN の fabrikam.com 、バックエンド v1 サービスからの応答が返されます。

fqdnIp=$(dig +short $fqdn)
curl -k --resolve fabrikam.com:80:$fqdnIp http://fabrikam.com

応答を介して、次の内容が表示されます。

{
 "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-594bd59865-ppv9w"
}

これで、ALB コントローラーをインストールし、バックエンド アプリケーションをデプロイし、Application Gateway for Containers のゲートウェイ API を介して異なるホスト名を使用して 2 つの異なるバックエンド サービスにトラフィックをルーティングしました。