Kubernetes Gateway API:Ingress 的现代化替代方案
背景与目标
Gateway API 引入标准化的流量入口模型(GatewayClass/Gateway/Route),弥补了 Ingress 扩展性不足的问题,原生支持 TCP/UDP/HTTP/HTTPS、多监听器、细粒度路由和跨团队职责分离。本文展示从安装 CRD、部署控制器,到创建 Gateway 与 HTTPRoute 的完整操作,并给出生产落地要点和排障步骤。
安装与准备
- 安装 Gateway API CRD(v1.1+):
kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.1.0/standard-install.yaml
- 部署实现控制器(示例使用 Envoy Gateway):
kubectl apply -f https://github.com/envoyproxy/gateway/releases/download/v1.1.1/install.yaml
kubectl -n envoy-gateway-system wait --for=condition=Available deploy --all --timeout=120s
若已使用 Istio/Contour/NGINX 等支持 Gateway API 的实现,可跳过此步骤,直接使用相应 GatewayClass 名称。
- 准备示例应用:
kubectl create namespace demo
kubectl label namespace demo istio-injection=enabled --overwrite # 若使用 Istio,可选
kubectl -n demo create deploy web --image=nginx:1.25 --port=80
kubectl -n demo expose deploy web --port=80
创建 GatewayClass 与 Gateway
- 声明 GatewayClass(Envoy Gateway 示例使用
envoy-gateway):
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: eg
spec:
controllerName: gateway.envoyproxy.io/gatewayclass-controller
应用并确认:
kubectl apply -f gatewayclass.yaml
kubectl get gatewayclasses
- 创建 Gateway,监听 80/443 端口:
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: demo-gw
namespace: demo
spec:
gatewayClassName: eg
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: Same
- name: https
protocol: HTTPS
port: 443
tls:
mode: Terminate
certificateRefs:
- group: core
kind: Secret
name: demo-tls
allowedRoutes:
namespaces:
from: Same
如果只需 HTTP,可删除 HTTPS 监听器;证书可使用 cert-manager 生成的 Secret。
绑定 HTTPRoute(流量转发与灰度)
- 基础转发:
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: web
namespace: demo
spec:
parentRefs:
- name: demo-gw
hostnames:
- web.example.com
rules:
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: web
port: 80
- 灰度/权重示例(同域名拆分流量):
rules:
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: web
port: 80
weight: 80
- name: web-canary
port: 80
weight: 20
Gateway API 支持在同一 HTTPRoute 内直接配置权重,无需额外注解。
验证与排障
- 查看 Gateway 监听状态:
kubectl -n demo get gateway demo-gw -o wide
kubectl get httproute -n demo -o wide
- 获取入口地址:
kubectl get svc -n envoy-gateway-system envoy-gateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}'
- 验证访问:
curl -H "Host: web.example.com" http://<gateway-ip> -I
- 常见问题排查:
- Gateway/HTTPRoute
Scheduled状态非True:检查controllerName是否匹配当前实现。 - 404/503:确认
hostnames与请求 Host 头一致;后端 Service 端口是否匹配。 - TLS 失败:证书 Secret 名称/namespace 是否与 Gateway 中引用一致;监听器协议是否为 HTTPS。
- 权重无效:查看实现是否支持全量 Gateway API 特性,或检查控制器版本。
生产落地要点
- 统一规划 GatewayClass 与命名规范,区分南北向/东西向入口及团队边界。
- 结合 cert-manager 管理证书生命周期,HTTPS 监听器保持自动续期。
- 将 Gateway/Route 资源纳入 GitOps,发布前使用
kubectl apply --server-dry-run校验。 - 对接监控:启用控制器的 Prometheus 指标与访问日志,建立 4xx/5xx 报警。
- 升级前验证 Gateway API 版本兼容性,并在灰度环境先行回归。
总结
Gateway API 提供了比 Ingress 更丰富、标准化的流量入口抽象。通过 CRD 安装、控制器部署、Gateway 与 Route 绑定,可以快速实现多监听器、权重路由和 TLS 终止。按上述步骤落地并结合 GitOps、监控与证书管理,即可平滑过渡到 Gateway API 模型。