你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用用于容器的应用程序网关进行多站点托管 - 网关 API

本文档帮助你设置一个示例应用程序,该应用程序使用网关 API 中的资源来演示如何在同一 Kubernetes 网关资源/用于容器前端的应用程序网关上托管多个站点。 下面是步骤:

  • 使用一个 HTTP 侦听器创建 网关 资源。
  • 创建两个 HTTPRoute 资源,每个资源引用唯一的后端服务。

背景

容器应用程序网关允许在同一端口上配置多个 Web 应用程序,从而启用多站点托管。 可以使用唯一后端服务托管两个或多个唯一站点。 请参阅以下示例场景:

图片显示了使用适用于容器的应用程序网关进行多站点托管。

先决条件

  1. 如果使用 BYO 部署策略,请确保为容器资源和 ALB 控制器设置应用程序网关。

  2. 如果使用了 ALB 托管部署策略,请确保通过 ApplicationLoadBalancer 自定义资源预配 ALB 控制器和用于容器资源的应用程序网关。

  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-v2test-infra 的两个服务
    • backend-v1 命名空间中名为 backend-v2test-infra 的两个部署

部署所需的网关 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 资源管理器中为容器资源创建应用程序网关时,它将对前端资源使用以下命名约定: 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 域名创建两个 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 资源都显示为 “已接受” ,并且对容器资源的应用程序网关 进行编程

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

验证是否已为每个 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 命令指定服务器名称指示器, contoso.com 则前端 FQDN 会从后端-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 命令指定服务器名称指示器, fabrikam.com 则前端 FQDN 会从后端-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 控制器,部署了后端应用程序,并通过应用程序容器网关上的网关 API 通过不同主机名将流量路由到两个不同的后端服务。