Share via


具有容器 應用程式閘道 的多網站裝載 - 閘道 API

本文件可協助您設定使用閘道 API 資源的範例應用程式,以示範如何在相同的 Kubernetes 閘道資源/適用於容器的應用程式閘道前端上裝載多個網站。 其中提供下列作業的步驟:

  • 使用一個 HTTP 接聽程式建立閘道資源。
  • 建立兩個 HTTPRoute 資源,每個資源都會參考唯一的後端服務。

背景

適用於容器的應用程式閘道可讓您在相同的連接埠上設定多個 Web 應用程式,以啟用多網站裝載。 您可以使用唯一後端服務來裝載兩個以上 (含) 的唯一網站。 請查看下列範例情節:

此圖顯示具有容器 應用程式閘道的多月臺裝載。

必要條件

  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
    • 在命名空間中呼叫 backend-v1backend-v2test-infra 兩個服務
    • 在命名空間中呼叫 backend-v1backend-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 控制器在 ARM 中建立適用於容器的應用程式閘道資源時,ALB 控制器會針對前端資源使用下列命名慣例:fe-<8 個隨機產生的字元>

如果您要變更在 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,會傳回來自 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"
}

如果使用 curl 命令 fabrikam.com 指定伺服器名稱指標,則對於前端 FQDN,會傳回來自 backend-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"
}

恭喜,您已在適用於容器的應用程式閘道上透過不同的主機名稱和閘道 API,安裝 ALB 控制器、部署後端應用程式,並且將流量路由傳送至兩個不同的後端服務。