使用 Envoy Gateway 进行 Ingress 和负载均衡
概述
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)
-
外部 LoadBalancer 提供外部 IP,将流量映射到集群中。
-
Envoy Gateway 终止 TLS,应用路由规则,并将流量转发到后端服务。
-
Service(ClusterIP)将流量分发到各节点上的 Pod。
此架构以完全符合 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
了解更多信息,请参阅配置 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
了解更多信息,请参阅配置 Gateway API Route。
说明
-
Gateway 为 HTTP/S 流量公开端口 80 和 443。
-
HTTPRoute 根据主机名和路径定义路由规则。
-
多个 HTTPRoute 对象可以共享同一个 Gateway。
配置 Ingress 集群流量
Envoy Gateway 支持多种外部流量入口模式,具体取决于您的基础设施。
公开 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 通过其后端引用提供灵活的负载均衡方法。
默认轮询
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
会话亲和性(粘性会话)
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。
故障排除
商标声明
Envoy® 是 The Linux Foundation 的注册商标。
MetalLB™ 是 The Linux Foundation 的商标。
Alauda 是独立供应商。本产品与 The Linux Foundation 没有关联,也未获得其认可或赞助。所有商标均归其各自所有者所有,此处仅用于标识目的。