AKS 中基於 Gateway API 實現跨命名空間路由:多團隊協作的流量治理方案

AKS 中基於 Gateway API 實現跨命名空間路由:多團隊協作的流量治理方案_Kubernetes

在大型企業的 AKS(Azure Kubernetes Service)集羣中,多團隊協作部署是常態——不同業務團隊的服務通常隔離在獨立命名空間內,既保障資源安全,也便於權限管控。作為雲原生架構師,在基於 AKS(Azure Kubernetes Service)構建大規模微服務架構時,跨 Namespace 路由是繞不開的核心需求。它能幫助我們實現服務的隔離部署與統一入口管理,提升架構的靈活性和可維護性。本文將基於 Kubernetes Gateway API 標準,結合 NGINX Gateway Fabric 實現,詳細講解在已部署 AKS 集羣和 Gateway API 服務的前提下,如何完成跨 Namespace 路由的設計與落地。

核心概念與架構設計

關鍵組件説明

在開始實踐前,先明確幾個核心組件的角色定位,確保對整體架構有清晰認知:

  • AKS 集羣:作為基礎運行環境,已完成 Kubernetes 核心組件部署,支持 Namespace 隔離和資源編排。
  • Gateway API:Kubernetes 官方推出的新一代網關標準,相比 Ingress API 提供更靈活的角色劃分和跨 Namespace 支持,核心資源包括 Gateway(網關實例)、HTTPRoute(路由規則)、Service(後端服務)。
  • NGINX Gateway Fabric:基於 Gateway API 實現的高性能網關代理,負責接收外部流量並根據路由規則轉發至跨 Namespace 的後端服務,兼容 NGINX 的高性能轉發特性。
  • Namespace:用於隔離不同環境(如 dev、test、prod)或不同業務域的服務,本文將以 backend-service(後端服務命名空間)和 frontend-service(前端服務命名空間)為例進行跨域路由配置。

跨 Namespace 路由架構圖

外部流量 → AKS 負載均衡器 → Gateway(nginx-gateway-fabric)→ HTTPRoute(路由規則)→ 跨 Namespace Service → 後端 Pod

核心邏輯:通過 Gateway 暴露統一入口,利用 HTTPRoute 定義跨 Namespace 的路由規則,實現不同 Namespace 下服務的統一訪問和流量管控。

前置條件確認

在開始配置前,請確保以下環境已就緒:

  1. AKS 集羣運行正常(版本 ≥ 1.24,支持 Gateway API);
  2. Gateway API CRDs 已安裝(gateway.networking.k8s.io/v1beta1);
  3. NGINX Gateway Fabric 已部署(可通過 Helm 安裝,命名空間建議為 nginx-gateway);
  4. 跨 Namespace 服務已就緒(本文以 nginx-svc 為例)。

可通過以下命令驗證前置環境:

# 驗證 Gateway API CRDs
kubectl get crds | grep gateway.networking.k8s.io

# 驗證 NGINX Gateway Fabric 部署狀態
kubectl get pods -n nginx-gateway

分步實現跨 Namespace 路由

部署 Gateway 實例(統一入口)

首先創建 Gateway 資源,定義網關的監聽端口、協議和負載均衡配置,作為跨 Namespace 流量的統一入口。

創建 gateway.yaml 文件:

apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: aks-cross-namespace-gateway
  namespace: nginx-gateway
spec:
  gatewayClassName: nginx
  listeners:
  - name: http
    hostname: "foo.aiorg.ltd"
    protocol: HTTP
    port: 80
    allowedRoutes:
      namespaces:
        from: Selector
        selector:
          matchLabels:
            shared-gateway-access: "true"

應用配置:

kubectl apply -f gateway.yaml


AKS 中基於 Gateway API 實現跨命名空間路由:多團隊協作的流量治理方案_路由_02

驗證 Gateway 狀態:

kubectl get gateway -n nginx-gateway

AKS 中基於 Gateway API 實現跨命名空間路由:多團隊協作的流量治理方案_Gateway API_03

部署Namespace資源(HTTPRoute)

創建兩個Namespace資源,分別放置兩個服務

apiVersion: v1
kind: Namespace
metadata:
  name: order-ns
  labels:
    shared-gateway-access: "true"
---
apiVersion: v1
kind: Namespace
metadata:
  name: product-ns
  labels:
    shared-gateway-access: "true"

應用配置:

kubectl apply -f cross-namespace-ns.yaml

AKS 中基於 Gateway API 實現跨命名空間路由:多團隊協作的流量治理方案_路由_04

部署測試服務

部署nginx和httpd兩個測試服務,分別放置於nginx-ns和httpd-ns 命名空間

apiVersion: apps/v1
kind: Deployment
metadata:
  name: order-deployment
  namespace: order-ns
spec:
  replicas: 1 
  selector:
    matchLabels:
      app: order
  template:
    metadata:
      labels:
        app: order
    spec:
      containers:
      - name: order
        image: REPO/nginx-order:v1.5
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: order-service
  namespace: order-ns
spec:
  type: ClusterIP 
  selector:
    app: order
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: product-deployment
  namespace: product-ns
spec:
  replicas: 1 
  selector:
    matchLabels:
      app: product
  template:
    metadata:
      labels:
        app: product
    spec:
      containers:
      - name: product
        image: REPO/nginx-product:v1.5
        ports:
        - containerPort: 80 
---
apiVersion: v1
kind: Service
metadata:
  name: product-service
  namespace: product-ns
spec:
  type: ClusterIP 
  selector:
    app: product
  ports:
    - protocol: TCP
      port: 80 
      targetPort: 80

應用配置:

kubectl apply -f cross-namespace-service.yaml

AKS 中基於 Gateway API 實現跨命名空間路由:多團隊協作的流量治理方案_Gateway API_05

配置跨 Namespace 路由規則(HTTPRoute)

通過 HTTPRoute 資源定義路由規則,將不同路徑的流量轉發至對應 Namespace 的 Service。本文實現以下路由邏輯:

  • 路徑 /order → 轉發至 order-ns 命名空間的 order-service 服務
  • 路徑 /product → 轉發至 product-ns 命名空間的 product-service 服務

創建 httproute-cross-namespace.yaml 文件:

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: order-httproute
  namespace: order-ns
spec:
  parentRefs:
  - name: aks-cross-namespace-gateway
    namespace: nginx-gateway
  hostnames:
  - "foo.aiorg.ltd"
  rules:
  - matches:
    - path:
        value: /order
    backendRefs:
    - name: order-service
      port: 80
  - matches:
    - path:
        value: /
    backendRefs:
    - name: order-service
      port: 80
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: product-httproute
  namespace: product-ns
spec:
  parentRefs:
  - name: aks-cross-namespace-gateway
    namespace: nginx-gateway
  hostnames:
  - "foo.aiorg.ltd"
  rules:
  - matches:
    - path:
        value: /product
    backendRefs:
    - name: product-service
      port: 80

應用配置:

kubectl apply -f httproute-cross-namespace.yaml

AKS 中基於 Gateway API 實現跨命名空間路由:多團隊協作的流量治理方案_路由_06

路由驗證

本次實驗通過 HTTPRoute 資源定義路由規則,將不同路徑的流量轉發至對應 Namespace 的 Service。具體如下

  • 路徑 /order → 轉發至 order-ns 命名空間的 order-service 服務
  • 路徑 /product → 轉發至 product-ns 命名空間的 product-service 服務

驗證/order路由

瀏覽器訪問:

AKS 中基於 Gateway API 實現跨命名空間路由:多團隊協作的流量治理方案_Nginx Ingress_07

驗證/product路由

瀏覽器訪問:http://foo.aiorg.ltd/product/

AKS 中基於 Gateway API 實現跨命名空間路由:多團隊協作的流量治理方案_Gateway API_08

總結

本文基於 Kubernetes Gateway API 和 NGINX Gateway Fabric,完成了 AKS 環境下跨 Namespace 路由的部署與配置。核心要點包括:通過 Gateway 定義統一入口並允許跨 Namespace 路由關聯,通過 HTTPRoute 精準匹配路徑並轉發至目標服務

這種方案相比傳統 Ingress 具有更強的靈活性和擴展性,支持更細粒度的路由控制、流量管控和安全隔離,適用於大規模微服務架構中不同業務域、不同環境的服務統一接入場景。後續可基於此方案進一步擴展 HTTPS 加密、身份認證、限流熔斷等高級功能,構建企業級雲原生網關平台。