Share via


具有適用於容器的應用程式閘道的後端 MTLS - 閘道 API (預覽)

本文將透過範例,說明如何使用閘道 API 中的下列資源建立應用程式。 其中提供下列作業的步驟:

  • 使用一個 HTTPS 接聽程式建立閘道資源。
  • 建立參考後端服務的 HTTPRoute 資源。
  • 建立 BackendTLSPolicy 資源,該資源具有 HTTPRoute 中所參考後端服務的用戶端和 CA 憑證。

背景

相互傳輸層安全性 (MTLS) 是依賴憑證來加密通訊並識別服務用戶端的流程。 這可讓後端工作負載只信任來自已驗證裝置的連線,進一步提升其安全性狀態。

請參閱下圖:

A diagram showing the Application Gateway for Containers backend MTLS process.

必要條件

重要

適用於容器的應用程式閘道目前為預覽版。
請參閱 Microsoft Azure 預覽版增補使用規定,以了解適用於 Azure 功能 (搶鮮版 (Beta)、預覽版,或尚未正式發行的版本) 的法律條款。

  1. 如果遵循 BYO 部署策略,請確定您已設定了適用於容器的應用程式閘道和 ALB 控制器
  2. 如果遵循 ALB 受控部署策略,請確定您已透過 ApplicationLoadBalancer 自訂資源佈建了 ALB 控制器,以及適用於容器的應用程式閘道。
  3. 部署範例 HTTP 應用程式 在您的叢集上套用下列 deployment.yaml 檔案,以建立範例 Web 應用程式,並部署範例秘密來示範後端相互驗證 (mTLS)。
kubectl apply -f https://trafficcontrollerdocs.blob.core.windows.net/examples/https-scenario/end-to-end-ssl-with-backend-mtls/deployment.yaml

此命令會在叢集上建立下列項目:

  • 名為 test-infra 的命名空間
  • test-infra 命名空間中名為 mtls-app 的一個服務
  • test-infra 命名空間中名為 mtls-app 的一個部署
  • test-infra 命名空間中名為 mtls-app-nginx-cm 的一個 ConfigMap
  • test-infra 命名空間中名為 backend.comfrontend.comgateway-client-certca.bundle 的四個秘密

部署必要的閘道 API 資源

建立閘道:

kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1beta1
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: https-listener
    port: 443
    protocol: HTTPS
    allowedRoutes:
      namespaces:
        from: Same
    tls:
      mode: Terminate
      certificateRefs:
      - kind : Secret
        group: ""
        name: frontend.com
EOF

注意

當 ALB 控制器在 ARM 中建立適用於容器的應用程式閘道資源時,ALB 控制器會針對前端資源使用下列命名慣例:fe-<8 個隨機產生的字元>

如果您要變更在 Azure 中建立的前端名稱,請考慮遵循自備部署策略

建立閘道資源之後,請確定狀態有效,接聽程式的狀態為已程式化,並且已經將位址指派給閘道。

kubectl get gateway gateway-01 -n test-infra -o yaml

成功建立閘道後輸出內容的範例。

status:
  addresses:
  - type: IPAddress
    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

建立閘道之後,請建立 HTTPRoute。

kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1beta1
kind: HTTPRoute
metadata:
  name: https-route
  namespace: test-infra
spec:
  parentRefs:
  - name: gateway-01
  rules:
  - backendRefs:
    - name: mtls-app
      port: 443
EOF

建立 HTTPRoute 資源之後,請確定路由的狀態為 [已接受],且適用於容器的應用程式閘道資源的狀態為 [已程式化]

kubectl get httproute https-route -n test-infra -o yaml

確認已成功更新適用於容器的應用程式閘道資源的狀態。

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

建立 BackendTLSPolicy

kubectl apply -f - <<EOF
apiVersion: alb.networking.azure.io/v1
kind: BackendTLSPolicy
metadata:
  name: mtls-app-tls-policy
  namespace: test-infra
spec:
  targetRef:
    group: ""
    kind: Service
    name: mtls-app
    namespace: test-infra
  default:
    sni: backend.com
    ports:
    - port: 443
    clientCertificateRef:
      name: gateway-client-cert
      group: ""
      kind: Secret
    verify:
      caCertificateRef:
        name: ca.bundle
        group: ""
        kind: Secret
      subjectAltName: backend.com
EOF

建立 BackendTLSPolicy 物件之後,請檢查物件上的狀態,以確保原則有效。

kubectl get backendtlspolicy -n test-infra mtls-app-tls-policy -o yaml

有效 BackendTLSPolicy 物件的建立範例輸出。

status:
  conditions:
  - lastTransitionTime: "2023-06-29T16:54:42Z"
    message: Valid BackendTLSPolicy
    observedGeneration: 1
    reason: Accepted
    status: "True"
    type: Accepted

測試應用程式的存取權

現在已準備完成,可透過指派給前端的 FQDN 將部分流量傳送至範例應用程式。 使用下列命令來取得 FQDN。

fqdn=$(kubectl get gateway gateway-01 -n test-infra -o jsonpath='{.status.addresses[0].value}')

以 CURL 執行 FQDN 時,會根據 HTTPRoute 上設定的後端傳回回應。

curl --insecure https://$fqdn/

恭喜您,您已安裝了 ALB 控制器,部署了後端應用程式,並且在適用於容器的應用程式閘道上設定流量分割/加權循環配置資源。