使用 Envoy Gateway 实现 Ingress 和负载均衡
目录
Overview
Envoy Gateway 为 Kubernetes 集群提供第 7 层(L7)的 ingress 和负载均衡功能。
它基于 Kubernetes Gateway API,构建于 Envoy Proxy 之上,支持高级的 HTTP、HTTPS、gRPC 和 TCP 路由,具备完整的可观测性和策略控制能力。
Envoy Gateway 通过定义 GatewayClass、Gateway 和 Route 资源,统一了 ingress 流量管理模型,
提供了一个标准且可移植的替代方案,取代传统的 Ingress 对象。
网络流量架构
External Client (Browser / curl) https://www.example.com
│
▼
DNS Resolution (https://www.example.com) → External IP (e.g. 34.23.88.11)
│
▼
External Load Balancer [L4]
│
▼
Envoy Gateway (Gateway API) [L7]
│
▼
Kubernetes Service (ClusterIP) [L4]
│
▼
Pod (Application)
-
External LoadBalancer 提供一个外部 IP,将流量映射到集群内部。
-
Envoy Gateway 负责 TLS 终止,应用路由规则,并转发到后端服务。
-
Service(ClusterIP)将流量分发到跨节点的 Pods。
该架构用完全符合 Gateway API 的数据平面替代了传统的 “Ingress Controller”。
配置路由
定义 GatewayClass
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: envoy
spec:
controllerName: gateway.envoyproxy.io/gatewayclass-controller
创建 Gateway
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: public-gateway
namespace: gateway-system
spec:
gatewayClassName: envoy
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: Same
- name: https
protocol: HTTPS
port: 443
tls:
mode: Terminate
certificateRefs:
- kind: Secret
name: site-cert
了解更多 Configure Gateway API Gateway。
创建 HTTP 路由
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: web-route
namespace: default
spec:
parentRefs:
- name: public-gateway
namespace: gateway-system
hostnames:
- 'app.example.com'
rules:
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: web-service
port: 8080
了解更多 Configure Gateway API Route。
说明:
-
Gateway 对外暴露 80 和 443 端口用于 HTTP/S 流量。
-
HTTPRoute 根据主机名和路径定义路由规则。
-
多个 HTTPRoute 对象可以共享同一个 Gateway。
配置集群外部流量入口
Envoy Gateway 支持多种外部流量入口模式,具体取决于您的基础设施环境。
| 环境 | 入口类型 | 示例 |
|---|
| Cloud | Service type=LoadBalancer | 云服务商分配公网 IP |
| Bare Metal | MetalLB + Service type=LoadBalancer | MetalLB 分配外部 IP |
| Edge / Internal | NodePort 或 hostNetwork | 用于本地或私有访问 |
示例:暴露 Envoy Gateway 的 Service 配置:
apiVersion: v1
kind: Service
metadata:
name: envoy-gateway
namespace: gateway-system
spec:
type: LoadBalancer
selector:
app: envoy
ports:
- name: http
port: 80
targetPort: 8080
- name: https
port: 443
targetPort: 8443
您可以通过以下命令查看分配的外部 IP:
kubectl get svc envoy-gateway -n gateway-system
负载均衡策略
Envoy Gateway 通过后端引用支持灵活的负载均衡方式。
默认轮询(Round-Robin)
backendRefs:
- name: app-v1
port: 8080
- name: app-v2
port: 8080
加权负载均衡
backendRefs:
- name: app-v1
port: 8080
weight: 80
- name: app-v2
port: 8080
weight: 20
会话亲和(Sticky Sessions)
Envoy 支持基于 Cookie 或源 IP 的会话亲和,通过策略配置实现:
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
name: web-policy
namespace: default
spec:
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: web-route
sessionAffinity:
type: Cookie
cookie:
name: session_id
ttl: 3600s
了解更多关于 BackendTrafficPolicy。
TLS 与安全
TLS 终止
在 Gateway 监听器层面处理:
tls:
mode: Terminate
certificateRefs:
- kind: Secret
name: site-cert
TLS 透传
用于端到端加密,Envoy 直接转发加密流量:
Gateway 与服务间的 mTLS
高级策略 CRD 可启用 mTLS 或客户端证书验证。
在无云负载均衡器的裸金属集群中:
-
配置 MetalLB IPAddressPool 和 L2Advertisement。
-
确保 Envoy Gateway 的 Service 类型为 LoadBalancer。
-
MetalLB 会自动分配外部 IP。
-
DNS 记录可将域名(如 app.example.com)映射到分配的 IP。
故障排查
| 问题 | 可能原因 | 解决方案 |
|---|
| Gateway 无外部 IP | MetalLB 配置错误 | 检查 Service 类型及 MetalLB 控制器状态 |
| HTTPRoute 被忽略 | parentRefs 不匹配 | 确认 Gateway 名称和命名空间正确 |
| TLS 无法生效 | Secret 未找到 | 验证 certificateRefs 路径 |
| Gateway 返回 404 | 无匹配的主机或路径 | 确认 HTTPRoute 规则配置 |
| 延迟突增 | 代理过载 | 扩容 Envoy Gateway 副本 |