摘要
本文主要探讨了在Envoy Gateway中如何配置gRPC路由。通过详细阐述其前置条件、安装步骤、验证方法以及路由匹配规则等内容,展示了从基础环境准备到实现gRPC流量精确转发的完整流程。同时,借助实际操作命令和示例,帮助读者深入理解并掌握在k8s环境下利用Envoy Gateway进行gRPC路由配置的技术要点,为构建高效、灵活的gRPC服务架构提供了有力参考。
一、GRPC路由概述
GRPCRoute资源允许用户通过匹配HTTP/2流量并将其转发到后端gRPC服务器来配置gRPC路由。要了解有关gRPC路由的更多信息,请参阅Gateway API文档。
二、安装
安装gRPC路由示例资源:
kubectl apply -f https://raw.githubusercontent.com/envoyproxy/gateway/latest/examples/kubernetes/grpc-routing.yaml
此清单安装了一个GatewayClass、Gateway、Deployment、Service和GRPCRoute资源。GatewayClass是一个集群范围的资源,表示可以实例化的网关类。
注意:Envoy Gateway默认配置为管理controllerName: gateway.envoyproxy.io/gatewayclass-controller
的GatewayClass。
三、验证
(一)检查GatewayClass状态
kubectl get gc --selector=example=grpc-routing
状态应反映“Accepted=True”,表示Envoy Gateway正在管理GatewayClass。
(二)检查Gateway状态
kubectl get gateways --selector=example=grpc-routing
状态应反映“Ready=True”,表示Envoy代理基础设施已配置完成。状态还提供了网关的地址,此地址稍后用于测试与代理后端服务的连接性。
(三)检查GRPCRoute状态
kubectl get grpcroutes --selector=example=grpc-routing -o yaml
GRPCRoute的状态应显示“Accepted=True”以及引用示例网关的parentRef
。example-route
匹配“grpc-example.com”的任何流量并将其转发到“yages”服务。
四、测试配置
(一)获取网关地址
在测试到yages
后端的GRPC路由之前,获取网关的地址。
export GATEWAY_HOST=$(kubectl get gateway/example-gateway -o jsonpath='{.status.addresses[0].value}')
(二)测试GRPC路由
- 使用grpcurl命令测试到
yages
后端的GRPC路由。grpcurl -plaintext -authority=grpc-example.com ${GATEWAY_HOST}:80 yages.Echo/Ping
- 您应该看到如下响应:
{
"text": "pong"
}
- Envoy Gateway还支持此配置的gRPC - Web请求。以下
curl
命令可用于通过HTTP/2发送gRPC - Web请求,您应收到与上一个命令相同的响应。- 主体中的数据
AAAAAAA=
是Ping RPC接受的空消息(数据长度为0)的base64编码表示。
- 主体中的数据
curl --http2-prior-knowledge -s ${GATEWAY_HOST}:80/yages.Echo/Ping -H 'Host: grpc-example.com' -H 'Content-Type: application/grpc-web-text' -H 'Accept: application/grpc-web-text' -XPOST -d'AAAAAAA=' | base64 -d
五、GRPCRoute匹配
matches
字段可用于根据gRPC的服务和/或方法名称将路由限制为特定的请求集。它支持两种匹配类型:Exact
和RegularExpression
。
(一)Exact匹配(精确匹配)
Exact
匹配是默认的匹配类型。
以下示例展示了如何基于grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo
的服务和方法名称进行请求匹配,以及匹配所有方法名称为Ping
的服务(在我们的部署中匹配yages.Echo/Ping
)。
1. 从标准输入应用
cat <<EOF | kubectl apply -f -
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: GRPCRoute
metadata:
name: yages
labels:
example: grpc-routing
spec:
parentRefs:
- name: example-gateway
hostnames:
- "grpc-example.com"
rules:
- matches:
- method:
method: ServerReflectionInfo
service: grpc.reflection.v1alpha.ServerReflection
- method:
method: Ping
backendRefs:
- group: ""
kind: Service
name: yages
port: 9000
weight: 1
EOF
3. 验证GRPCRoute状态
kubectl get grpcroutes --selector=example=grpc-routing -o yaml
4. 测试GRPC路由
grpcurl -plaintext -authority=grpc-example.com ${GATEWAY_HOST}:80 yages.Echo/Ping
(二)RegularExpression匹配(正则表达式匹配)
以下示例展示了如何基于服务和方法名称使用RegularExpression
匹配类型进行请求匹配。它匹配所有模式为/.*/Echo/Pin.+
的服务和方法(在我们的部署中匹配yages.Echo/Ping
)。
1. 从标准输入应用
cat <<EOF | kubectl apply -f -
apiVersion: gateway.networking.k8s.io/v1alpha2
kind: GRPCRoute
metadata:
name: yages
labels:
example: grpc-routing
spec:
parentRefs:
- name: example-gateway
hostnames:
- "grpc-example.com"
rules:
- matches:
- method:
method: ServerReflectionInfo
service: grpc.reflection.v1alpha.ServerReflection
- method:
method: "Pin.+"
service: ".*.Echo"
type: RegularExpression
backendRefs:
- group: ""
kind: Service
name: yages
port: 9000
weight: 1
EOF
3. 验证GRPCRoute状态
kubectl get grpcroutes --selector=example=grpc-routing -o yaml
4. 测试GRPC路由
grpcurl -plaintext -authority=grpc-example.com ${GATEWAY_HOST}:80 yages.Echo/Ping