AKS 中基於 Gateway API 實現跨命名空間路由:多團隊協作的流量治理方案
核心概念與架構設計
關鍵組件説明
- 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 API CRDs
kubectl get crds | grep gateway.networking.k8s.io
# 驗證 NGINX Gateway Fabric 部署狀態
kubectl get pods -n nginx-gateway
分步實現跨 Namespace 路由
部署 Gateway 實例(統一入口)
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
kubectl get gateway -n nginx-gateway
部署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
部署測試服務
部署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
配置跨 Namespace 路由規則(HTTPRoute)
- 路徑 /order → 轉發至 order-ns 命名空間的 order-service 服務
- 路徑 /product → 轉發至 product-ns 命名空間的 product-service 服務
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
路由驗證
本次實驗通過 HTTPRoute 資源定義路由規則,將不同路徑的流量轉發至對應 Namespace 的 Service。具體如下
- 路徑 /order → 轉發至 order-ns 命名空間的 order-service 服務
- 路徑 /product → 轉發至 product-ns 命名空間的 product-service 服務
驗證/order路由
瀏覽器訪問:
驗證/product路由
瀏覽器訪問:http://foo.aiorg.ltd/product/