当前位置: 首页 > news >正文

安装 Envoy Gateway

执行部署
# https://gateway.envoyproxy.io/docs/install/install-helm/ # 安装网关 API CRD 和 Envoy 网关 helm install eg oci://docker.io/envoyproxy/gateway-helm --version v1.7.3 -n envoy-gateway-system --create-namespace
创建测试应用
kubectl apply -f https://github.com/envoyproxy/gateway/releases/download/v1.7.3/quickstart.yaml -n default # 查看访问地址 cby@DESKTOP-IKRNJQE:~$ kubectl get svc -n envoy-gateway-system NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE envoy-default-eg-e41e7b31 LoadBalancer 10.68.120.80 192.168.1.15 80:32116/TCP 6s envoy-gateway ClusterIP 10.68.64.83 <none> 18000/TCP,18001/TCP,18002/TCP,19001/TCP,9443/TCP 30m cby@DESKTOP-IKRNJQE:~$
测试访问
cby@DESKTOP-IKRNJQE:~$ curl --verbose --header "Host: www.example.com" http://192.168.1.15/get * Trying 192.168.1.15:80... * Established connection to 192.168.1.15 (192.168.1.15 port 80) from 172.30.72.41 port 39072 * using HTTP/1.x > GET /get HTTP/1.1 > Host: www.example.com > User-Agent: curl/8.18.0 > Accept: */* > * Request completely sent off < HTTP/1.1 200 OK < content-type: application/json < x-content-type-options: nosniff < date: Mon, 11 May 2026 13:51:37 GMT < content-length: 475 < { "path": "/get", "host": "www.example.com", "method": "GET", "proto": "HTTP/1.1", "headers": { "Accept": [ "*/*" ], "User-Agent": [ "curl/8.18.0" ], "X-Envoy-External-Address": [ "192.168.1.100" ], "X-Forwarded-For": [ "192.168.1.100" ], "X-Forwarded-Proto": [ "http" ], "X-Request-Id": [ "d30d74c9-2c8e-4249-8ca6-ee24156e3317" ] }, "namespace": "default", "ingress": "", "service": "", "pod": "backend-869c8646c5-s4426" * Connection #0 to host 192.168.1.15:80 left intact }cby@DESKTOP-IKRNJQE:~$

其他可选实现:

  • Envoy Gateway: Envoy 官方实现
  • Istio: 服务网格方案
  • Contour: VMware 维护的 Envoy 控制器
  • Kong: Kong Ingress Controller

步骤 3: 创建 GatewayClass

cat > gatewayclass.yaml << EOF apiVersion: gateway.networking.k8s.io/v1 kind: GatewayClass metadata: name: nginx-gateway-class spec: controllerName: gateway.envoyproxy.io/gatewayclass-controller EOF

应用配置:

kubectl apply -f gatewayclass.yaml kubectl get gatewayclass cby@DESKTOP-IKRNJQE:~$ kubectl get gatewayclass gatewayclass.gateway.networking.k8s.io/nginx-gateway-class created NAME CONTROLLER ACCEPTED AGE eg gateway.envoyproxy.io/gatewayclass-controller True 19m nginx-gateway-class gateway.envoyproxy.io/gatewayclass-controller True 18s cby@DESKTOP-IKRNJQE:~$

步骤 4: 部署示例应用

创建两个简单的后端服务用于测试:

cat > demo-app.yaml << EOF apiVersion: apps/v1 kind: Deployment metadata: name: web-app-v1 spec: replicas: 1 selector: matchLabels: app: web-app-v1 template: metadata: labels: app: web-app-v1 spec: containers: - name: web-app-v1 image: registry.cn-hangzhou.aliyuncs.com/chenby/cby:nginx-v1 ports: - containerPort: 80 --- apiVersion: apps/v1 kind: Deployment metadata: name: web-app-v2 spec: replicas: 1 selector: matchLabels: app: web-app-v2 template: metadata: labels: app: web-app-v2 spec: containers: - name: web-app-v2 image: registry.cn-hangzhou.aliyuncs.com/chenby/cby:nginx-v2 ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: labels: app: web-app-v2 name: web-app-v2 spec: selector: app: web-app-v2 ports: - port: 80 protocol: TCP targetPort: 80 --- apiVersion: v1 kind: Service metadata: labels: app: web-app-v1 name: web-app-v1 spec: selector: app: web-app-v1 ports: - port: 80 protocol: TCP targetPort: 80 EOF

同样创建 v2 版本,然后应用:

kubectl apply -f demo-app.yaml

测试查看

cby@DESKTOP-IKRNJQE:~$ kubectl get pod NAME READY STATUS RESTARTS AGE backend-869c8646c5-s4426 1/1 Running 0 20m web-app-v1-844db9d88f-tjwzz 1/1 Running 0 22s web-app-v2-dd8d978b8-9stzg 1/1 Running 0 22s cby@DESKTOP-IKRNJQE:~$ cby@DESKTOP-IKRNJQE:~$ cby@DESKTOP-IKRNJQE:~$ cby@DESKTOP-IKRNJQE:~$ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE backend ClusterIP 10.68.86.118 <none> 3000/TCP 21m kubernetes ClusterIP 10.68.0.1 <none> 443/TCP 60d web-app-v1 ClusterIP 10.68.255.182 <none> 80/TCP 27s web-app-v2 ClusterIP 10.68.113.128 <none> 80/TCP 27s cby@DESKTOP-IKRNJQE:~$ cby@DESKTOP-IKRNJQE:~$ curl ^[[200~10.68.255.182~^C cby@DESKTOP-IKRNJQE:~$ cby@DESKTOP-IKRNJQE:~$ cby@DESKTOP-IKRNJQE:~$ curl 10.68.255.182 <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$ cby@DESKTOP-IKRNJQE:~$ curl 10.68.113.128 <h1 style="color: #2196F3;">Hello V2</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$

步骤 5: 配置基本 HTTP 路由

首先创建 Gateway:

cat > gateway.yaml << EOF apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: production-gateway namespace: web-app spec: gatewayClassName: nginx-gateway-class listeners: - name: http port: 80 protocol: HTTP allowedRoutes: namespaces: from: All EOF

创建 HTTPRoute:

cat > httproute-basic.yaml << EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: web-app-route namespace: default spec: parentRefs: - name: production-gateway namespace: web-app hostnames: - "app.example.com" rules: - matches: - path: type: PathPrefix value: / backendRefs: - name: web-app-v1 port: 80 - name: web-app-v2 port: 80 EOF

应用配置:

kubectl create namespace web-app kubectl apply -f gateway.yaml kubectl apply -f httproute-basic.yaml # 验证 kubectl get gateway -n web-app kubectl get httproute -n default cby@DESKTOP-IKRNJQE:~$ kubectl get gateway -n web-app NAME CLASS ADDRESS PROGRAMMED AGE production-gateway nginx-gateway-class 192.168.1.16 True 13s cby@DESKTOP-IKRNJQE:~$ cby@DESKTOP-IKRNJQE:~$ kubectl get httproute -n default NAME HOSTNAMES AGE backend ["www.example.com"] 26m web-app-route ["app.example.com"] 56s cby@DESKTOP-IKRNJQE:~$

获取 LoadBalancer IP 并测试:

curl -H "Host: app.example.com" http://192.168.1.16 cby@DESKTOP-IKRNJQE:~$ curl -H "Host: app.example.com" http://192.168.1.16 <h1 style="color: #2196F3;">Hello V2</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$ curl -H "Host: app.example.com" http://192.168.1.16 <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$ curl -H "Host: app.example.com" http://192.168.1.16 <h1 style="color: #2196F3;">Hello V2</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$ curl -H "Host: app.example.com" http://192.168.1.16 <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$ curl -H "Host: app.example.com" http://192.168.1.16 <h1 style="color: #2196F3;">Hello V2</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$ curl -H "Host: app.example.com" http://192.168.1.16 <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$

步骤 6: 配置 TLS 终止

6.1 创建自签名证书(测试用)
openssl genrsa -out tls.key 2048 openssl req -new -key tls.key -out tls.csr -subj "/CN=app.example.com" openssl x509 -req -in tls.csr -signkey tls.key -out tls.crt -days 365 kubectl create secret tls app-tls-secret \ --cert=tls.crt \ --key=tls.key \ -n web-app rm tls.key tls.csr tls.crt
6.2 更新 Gateway 配置启用 HTTPS
cat > gateway-with-tls.yaml << EOF apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: production-gateway namespace: web-app spec: gatewayClassName: nginx-gateway-class listeners: - name: http port: 80 protocol: HTTP allowedRoutes: namespaces: from: All - name: https port: 443 protocol: HTTPS tls: mode: Terminate certificateRefs: - name: app-tls-secret kind: Secret group: "" allowedRoutes: namespaces: from: All EOF

应用更新:

kubectl apply -f gateway-with-tls.yaml

测试 HTTPS 访问:

curl -k -H "Host: app.example.com" https://192.168.1.16 cby@DESKTOP-IKRNJQE:~$ curl -k -H "Host: app.example.com" https://192.168.1.16 <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$ curl -k -H "Host: app.example.com" https://192.168.1.16 <h1 style="color: #2196F3;">Hello V2</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$ curl -k -H "Host: app.example.com" https://192.168.1.16 <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$ curl -k -H "Host: app.example.com" https://192.168.1.16 <h1 style="color: #2196F3;">Hello V2</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$ curl -k -H "Host: app.example.com" https://192.168.1.16 <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$

步骤 7: 配置流量分割(金丝雀发布)

创建将 90% 流量发送到 v1,10% 流量发送到 v2 的路由:

cat > httproute-canary.yaml << EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: web-app-canary-route namespace: default spec: parentRefs: - name: production-gateway namespace: web-app hostnames: - "app.example.com" rules: - matches: - path: type: PathPrefix value: / backendRefs: - name: web-app-v1 port: 80 weight: 90 - name: web-app-v2 port: 80 weight: 10 EOF

应用配置:

kubectl delete -f httproute-basic.yaml kubectl apply -f httproute-canary.yaml cby@DESKTOP-IKRNJQE:~$ kubectl get HTTPRoute NAMESPACE NAME HOSTNAMES AGE default backend ["www.example.com"] 45m default web-app-canary-route ["app.example.com"] 104s cby@DESKTOP-IKRNJQE:~$

测试流量分割:

for i in {1..20}; do curl -s -H "Host: app.example.com" http://192.168.1.16 done cby@DESKTOP-IKRNJQE:~$ for i in {1..20}; do curl -s -H "Host: app.example.com" http://192.168.1.16 done <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #2196F3;">Hello V2</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #2196F3;">Hello V2</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$

步骤 8: 高级路由配置

8.1 基于 Header 的路由
cat > httproute-header.yaml << EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: header-based-route namespace: default spec: parentRefs: - name: production-gateway namespace: web-app hostnames: - "app.example.com" rules: - matches: - headers: - name: x-version value: v2 backendRefs: - name: web-app-v2 port: 80 - matches: - path: type: PathPrefix value: / backendRefs: - name: web-app-v1 port: 80 EOF

测试:

kubectl delete -f httproute-canary.yaml kubectl apply -f httproute-header.yaml # 不带 Header,访问 v1 curl -H "Host: app.example.com" http://192.168.1.16 # 带 Header,访问 v2 curl -H "Host: app.example.com" -H "x-version: v2" http://192.168.1.16 cby@DESKTOP-IKRNJQE:~$ curl -H "Host: app.example.com" http://192.168.1.16 <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$ cby@DESKTOP-IKRNJQE:~$ curl -H "Host: app.example.com" -H "x-version: v2" http://192.168.1.16 <h1 style="color: #2196F3;">Hello V2</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$
8.2 URL 重写
cat > httproute-rewrite.yaml << EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: rewrite-route namespace: default spec: parentRefs: - name: production-gateway namespace: web-app hostnames: - "app.example.com" rules: - matches: - path: type: PathPrefix value: /api/v1 filters: - type: URLRewrite urlRewrite: path: type: ReplacePrefixMatch replacePrefixMatch: / backendRefs: - name: web-app-v1 port: 80 - matches: - path: type: PathPrefix value: /api/v2 filters: - type: URLRewrite urlRewrite: path: type: ReplacePrefixMatch replacePrefixMatch: / backendRefs: - name: web-app-v2 port: 80 EOF

测试使用

kubectl delete -f httproute-header.yaml kubectl apply -f httproute-rewrite.yaml curl -H "Host: app.example.com" http://192.168.1.16/api/v1 curl -H "Host: app.example.com" http://192.168.1.16/api/v2 cby@DESKTOP-IKRNJQE:~$ curl -H "Host: app.example.com" http://192.168.1.16/api/v1 <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$ curl -H "Host: app.example.com" http://192.168.1.16/api/v2 <h1 style="color: #2196F3;">Hello V2</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> cby@DESKTOP-IKRNJQE:~$
8.3 请求/响应头修改
cat > httproute-modify.yaml << EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: header-modify-route namespace: default spec: parentRefs: - name: production-gateway namespace: web-app hostnames: - "app.example.com" rules: - matches: - path: type: PathPrefix value: / filters: - type: RequestHeaderModifier requestHeaderModifier: add: - name: X-Forwarded-Gateway value: "nginx-gateway" set: - name: X-Custom-Header value: "custom-value" - type: ResponseHeaderModifier responseHeaderModifier: add: - name: X-Powered-By value: "Gateway-API" backendRefs: - name: web-app-v1 port: 80 EOF
kubectl delete -f httproute-rewrite.yaml kubectl apply -f httproute-modify.yaml curl -v -H "Host: app.example.com" http://192.168.1.16/ cby@DESKTOP-IKRNJQE:~$ curl -v -H "Host: app.example.com" http://192.168.1.16/ * Trying 192.168.1.16:80... * Established connection to 192.168.1.16 (192.168.1.16 port 80) from 172.30.72.41 port 46798 * using HTTP/1.x > GET / HTTP/1.1 > Host: app.example.com > User-Agent: curl/8.18.0 > Accept: */* > * Request completely sent off < HTTP/1.1 200 OK < server: nginx/1.29.8 < date: Mon, 11 May 2026 14:54:24 GMT < content-type: text/html < content-length: 128 < last-modified: Mon, 11 May 2026 14:02:13 GMT < etag: "6a01e165-80" < accept-ranges: bytes < x-powered-by: Gateway-API < <h1 style="color: #4CAF50;">Hello V1</h1><p>时间:2026-05-11 星期一</p><p>地点:内蒙古自治区 呼和浩特市</p> * Connection #0 to host 192.168.1.16:80 left intact cby@DESKTOP-IKRNJQE:~$

最佳实践

1. 安全管理

1.1 使用 RBAC 限制权限

为应用团队创建受限角色,只允许管理 Route 资源:

apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: gateway-route-manager namespace: default rules: - apiGroups: ["gateway.networking.k8s.io"] resources: ["httproutes", "grpcroutes"] verbs: ["get", "list", "watch", "create", "update", "patch"]
1.2 使用 ReferenceGrant 控制跨命名空间访问
apiVersion: gateway.networking.k8s.io/v1beta1 kind: ReferenceGrant metadata: name: allow-tls-ref namespace: cert-manager spec: from: - group: gateway.networking.k8s.io kind: Gateway namespace: web-app to: - group: "" kind: Secret name: wildcard-tls-cert
1.3 TLS 最佳实践
  • 使用通配符证书减少证书管理复杂度
  • 定期轮换证书,建议使用 cert-manager 自动化
  • 优先使用 TLS Terminate 模式
  • 强制 HTTPS,配置 HTTP 到 HTTPS 重定向

2. 性能优化

2.1 合理配置监听器

限制允许的路由来源,减少配置复杂度:

spec: listeners: - name: http port: 80 protocol: HTTP allowedRoutes: namespaces: from: Selector selector: matchLabels: allow-gateway-access: "true"
2.2 避免过度复杂的路由规则
  • 合并相似的路由规则,减少配置数量
  • 使用最具体的匹配条件优先,提高匹配效率
  • 避免过多的 Header/Query 匹配,影响性能

3. 可靠性保障

3.1 渐进式发布策略

金丝雀发布流程:

  1. 部署新版本服务(v2)
  2. 配置 5% 流量到 v2
  3. 监控错误率和延迟
  4. 逐步增加流量比例(10% → 25% → 50% → 100%)
  5. 确认稳定后,移除旧版本
# 阶段 1: 5% 流量 backendRefs: - name: web-app-v1 weight: 95 - name: web-app-v2 weight: 5 # 阶段 2: 25% 流量 backendRefs: - name: web-app-v1 weight: 75 - name: web-app-v2 weight: 25 # 阶段 3: 100% 流量 backendRefs: - name: web-app-v2 weight: 100
3.2 蓝绿部署

通过快速切换权重实现蓝绿部署:

# 初始状态: 100% 蓝色环境 backendRefs: - name: blue-service weight: 100 - name: green-service weight: 0 # 切换时: 快速切换到绿色环境 backendRefs: - name: blue-service weight: 0 - name: green-service weight: 100

4. 多租户隔离

4.1 命名空间隔离策略
spec: listeners: - name: http port: 80 protocol: HTTP allowedRoutes: namespaces: from: Selector selector: matchLabels: environment: production

5. 调试和故障排查

5.1 常见问题诊断清单

问题 1: Gateway 状态为 "Not Accepted"

检查步骤:

# 1. 检查 GatewayClass 是否存在且有效 kubectl get gatewayclass # 2. 检查 Controller 是否运行 kubectl get pods -n <controller-namespace> # 3. 查看 Gateway 事件 kubectl describe gateway <name> -n <namespace> # 4. 检查控制器日志 kubectl logs -n <controller-namespace> -l app=<controller-name>

问题 2: HTTPRoute 未被接受

检查步骤:

# 1. 验证 parentRefs 是否正确 kubectl get httproute <name> -o yaml # 2. 检查 Gateway 是否允许该命名空间的路由 kubectl describe gateway <gateway-name> -n <namespace> # 3. 验证后端服务是否存在 kubectl get svc <backend-service-name> # 4. 检查 ReferenceGrant (如果跨命名空间) kubectl get referencegrant -A

问题 3: 流量未正确路由

检查步骤:

# 1. 验证路由规则匹配条件 kubectl describe httproute <name> # 2. 测试后端服务直接访问 kubectl run test --rm -i --tty --image=curlimages/curl -- \ curl http://<service-name>.<namespace>.svc.cluster.local # 3. 检查 Gateway 外部 IP kubectl get gateway <name> -o jsonpath='{.status.addresses}' # 4. 使用详细输出测试 curl -v -H "Host: <hostname>" http://<gateway-ip><path>

6. 生产环境建议

6.1 高可用架构
  • 部署多个 Controller 副本实现控制器高可用
  • 使用多个 Gateway 实例分散负载
  • 配置跨区域部署提高灾难恢复能力
spec: replicas: 3 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 0
6.2 备份和恢复
  • 定期备份 Gateway API 资源配置
  • 使用 GitOps 工具(如 ArgoCD、Flux)管理配置
  • 建立配置变更审批流程
# 备份所有 Gateway API 资源 kubectl get gatewayclass,gateway,httproute,tcproute,referencegrant -A -o yaml > gateway-backup.yaml
6.3 容量规划

关键监控指标:

  • 每秒请求数 (RPS)
  • 平均/ P95/P99 延迟
  • 错误率 (4xx, 5xx)
  • 活跃连接数
  • 带宽使用率
6.4 版本升级策略
  • 先在测试环境验证新版本
  • 阅读发行说明,了解破坏性变更
  • 灰度升级,先升级部分节点
  • 保留回滚方案

7. 常见应用场景

场景 1: 多域名托管
apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: multi-domain-route spec: parentRefs: - name: production-gateway hostnames: - "app1.example.com" - "app2.example.com" - "app3.example.com" rules: - matches: - path: type: PathPrefix value: / backendRefs: - name: shared-backend port: 80
场景 2: API 网关
apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: api-gateway-route spec: parentRefs: - name: production-gateway hostnames: - "api.example.com" rules: - matches: - path: type: PathPrefix value: /users backendRefs: - name: user-service port: 8080 - matches: - path: type: PathPrefix value: /orders backendRefs: - name: order-service port: 8080 - matches: - path: type: PathPrefix value: /payments backendRefs: - name: payment-service port: 8080
场景 3: A/B 测试
apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: ab-test-route spec: parentRefs: - name: production-gateway hostnames: - "app.example.com" rules: - matches: - path: type: PathPrefix value: / backendRefs: - name: variant-a port: 80 weight: 50 - name: variant-b port: 80 weight: 50

常见问题与故障排查

FAQ

Q1: Gateway API 和 Ingress 可以共存吗?

A: 可以。它们使用不同的资源类型,可以同时在集群中运行。建议逐步迁移,先在新服务中使用 Gateway API。

Q2: 如何选择 Gateway Controller 实现?

A: 考虑因素:

  • 功能需求:是否需要 gRPC、TCP 等高级功能
  • 性能要求:吞吐量、延迟指标
  • 生态系统:与现有工具链的集成
  • 社区活跃度:维护频率、问题响应速度
  • 企业支持:是否需要商业支持

推荐选择:

  • 通用场景: Envoy Gateway、NGINX Gateway Fabric
  • 服务网格: Istio
  • 高性能: Envoy Gateway
  • 简单易用: Contour

Q3: Gateway API 的性能如何?

A: Gateway API 本身是控制平面,性能取决于数据平面实现(Envoy/Nginx 等)、配置复杂度和硬件资源。现代实现(如 Envoy)可以达到数十万 QPS,延迟在毫秒级。

Q4: 如何处理 WebSocket 连接?

A: HTTPRoute 天然支持 WebSocket,无需特殊配置。确保后端服务正确处理 Upgrade 头即可。

Q5: 可以实现速率限制吗?

A: Gateway API v1 标准不直接支持速率限制,但可以通过以下方式实现:

  • Controller 特定扩展(如 Envoy Gateway 的 RateLimitFilter)
  • 在服务网格层实现(Istio)
  • 使用外部速率限制服务

Q6: 如何迁移现有的 Ingress 配置?

A: 迁移步骤:

  1. 分析现有 Ingress 资源
  2. 映射 Ingress 规则到 HTTPRoute
  3. 创建对应的 Gateway 资源
  4. 测试新配置
  5. 逐步切换流量
  6. 删除旧 Ingress 资源

可以使用官方工具ingress2gateway辅助转换:

kubectl get ingress -A -o yaml | ingress2gateway -f -

Q7: Gateway API 支持哪些 Kubernetes 版本?

A: Gateway API v1 需要 Kubernetes 1.22+,推荐使用最新的稳定版 Kubernetes 以获得最佳支持。

Q8: 如何实现灰度发布的自动化?

A: 结合以下工具:

  • Argo Rollouts:渐进式交付
  • Flagger:自动化的金丝雀分析
  • 自定义控制器:根据监控指标自动调整权重

总结

Kubernetes Gateway API 代表了 Kubernetes 流量管理的未来方向。通过本教程,您应该已经掌握了:

  1. 核心概念:理解了 GatewayClass、Gateway、HTTPRoute 等资源的作用和关系
  2. 架构原理:了解了控制平面和数据平面的交互机制
  3. 实践能力:能够从零搭建 Gateway API 环境并配置各种路由场景
  4. 最佳实践:学会了安全管理、性能优化、可靠性保障等生产环境技巧

下一步学习建议:

  • 深入研究特定 Gateway Controller 的文档
  • 探索服务网格与 Gateway API 的集成
  • 学习 GitOps 工作流管理 Gateway 配置

祝您在 Kubernetes Gateway API 的学习之路上取得成功!

关于

小陈运维

关于 - 小陈运维

CSDN、GitHub、知乎、开源中国、思否、掘金、简书、华为云、阿里云、腾讯云、哔哩哔哩、今日头条、新浪微博、个人博客

全网可搜《小陈运维》

文章主要发布于微信公众号

分类: Kubernetes

免责声明:本内容来自平台创作者,博客园系信息发布平台,仅提供信息存储空间服务。

好文要顶 关注我 收藏该文 微信分享

小陈运维
粉丝 - 15 关注 - 0

+加关注

0

0

升级成为会员

« 上一篇: 二进制安装Kubernetes(k8s)v1.36.0

posted @ 2026-05-12 12:21 小陈运维 阅读(159) 评论(0) 收藏 举报

http://www.jsqmd.com/news/1078893/

相关文章:

  • 知识库文档清洗:垃圾进垃圾出
  • AI模型访问控制机制与能力评估实践指南
  • C++大成之路:右值引用 move 语义
  • 抖音账号与手机号关联验证:合规路径、技术实现与风险规避指南
  • 9 年 IDEA 老用户,终于把它彻底卸载了!
  • SMD贴片式网络变压器专业厂家的核心能力解码:技术壁垒与行业实践
  • ESPHome:用配置文件搞定智能硬件开发
  • 不用注册就能用的 Web 应用合集
  • 协同线程与协同函数
  • 【JetBrains认证工程师亲授】:Ubuntu下IntelliJ IDEA免sudo安装+全局命令行启动+Shell集成三步到位(实测11种发行版兼容)
  • 【软工方法论22】代码重构原则与实践
  • 还在用 SSMS 手动导入 Excel?这款插件让 SQL Server 数据导入效率提升 10 倍(支持 Upsert + 大数据流式导入)
  • V 语言精选资源库
  • Kubernetes Pod 完全指南:从入门到实战,轻松掌握容器编排核心
  • 【题目讲解】 算法系列之定长类滑动窗口解析(上)
  • 拆解RAG分层架构:文档解析、切片、向量检索、问答逻辑解耦(原理+案例+Java代码)
  • 截断流Witt代数的模表示:基于p-特征与高度的简单模分类与构造
  • Go语言的sync.RWMutex读写锁升级与降级在并发访问模式变化中的限制
  • 2026 洗衣液十大名牌最新资讯汇总 主流品牌定位与家用场景指南
  • 分类评估指标实战指南:从混淆矩阵到业务价值落地
  • 高维点集密度分析:Jensen不等式与凸性原理的应用
  • 配置wsl记录(坎坷版)
  • Python 百年奥运数据分析实战|Pandas 清洗 + Matplotlib/Pyecharts 可视化 + 拖拽大屏完整项目(附源码)
  • GoGoGo虚拟定位:Android位置模拟技术的深度解析与实践指南
  • 震惊!小程序开发公司选错就亏大了,这3点你必须知道!
  • 自由职业者-技术顾问的生存指南:找客户与项目管理
  • 抖音无水印视频下载终极指南:3分钟搞定批量下载与智能管理
  • Apple Silicon Mac 电池健康管理终极指南:开源架构设计与实现原理
  • WorkBuddy自动化实战:手把手教你设置第一个定时任务
  • GraalVM原生镜像构建实战:十分钟让你的Java应用启动速度快100倍