路由指向 gRPC 服务

前置准备

  1. Higress 安装在 K8s 内的 higress-system 命名空间下,监听的 HTTP 端口为 80。 为了测试方便,gateway 端口映射都本地 127.0.0.1:80;
  2. 目标是为 default 命名空间下的 部署 grpc-httpbin 服务,服务监听的端口为 9091;
  3. grpc-httpbin 服务具体内容请参考 github httpbin;
  4. grpcurl 工具请参考 github grpcurl;

准备后端 grpc-httpbin 服务

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: grpc-httpbin-v1
  5. namespace: default
  6. spec:
  7. selector:
  8. app: grpc-httpbin-v1
  9. ports:
  10. - protocol: TCP
  11. port: 9091
  12. targetPort: 9091
  13. —-
  14. apiVersion: apps/v1
  15. kind: Deployment
  16. metadata:
  17. name: grpc-httpbin-v1
  18. namespace: default
  19. labels:
  20. app: grpc-httpbin-v1
  21. spec:
  22. replicas: 1
  23. selector:
  24. matchLabels:
  25. app: grpc-httpbin-v1
  26. template:
  27. metadata:
  28. labels:
  29. app: grpc-httpbin-v1
  30. spec:
  31. containers:
  32. - name: grpc-httpbin-v1
  33. image: registry.cn-hangzhou.aliyuncs.com/2456868764/httpbin:v1.0.1
  34. env:
  35. - name: POD_NAME
  36. valueFrom:
  37. fieldRef:
  38. fieldPath: metadata.name
  39. - name: NAMESPACE
  40. valueFrom:
  41. fieldRef:
  42. fieldPath: metadata.namespace
  43. resources:
  44. requests:
  45. cpu: 10m

配置路由

  1. apiVersion: networking.k8s.io/v1
  2. kind: Ingress
  3. metadata:
  4. annotations:
  5. nginx.ingress.kubernetes.io/backend-protocol: GRPC
  6. name: ingress-grpc-httpbin
  7. namespace: default
  8. spec:
  9. ingressClassName: higress
  10. rules:
  11. - host: foo.com
  12. http:
  13. paths:
  14. - path: /
  15. pathType: Prefix
  16. backend:
  17. service:
  18. name: grpc-httpbin-v1
  19. port:
  20. number: 9091

nginx.ingress.kubernetes.io/backend-protocol Ingress Annotaion 配置指定后端服务使用的协议,默认为HTTP,支持HTTP、HTTP2、HTTPS、GRPC和GRPCS。

grpcurl 调用测试

  1. 列出后端服务列表
  1. grpcurl -plaintext -authority foo.com 127.0.0.1:80 list
  2. grpc.reflection.v1.ServerReflection
  3. grpc.reflection.v1alpha.ServerReflection
  4. order.OrderManagement
  1. 调用 sayHello 方法
  1. grpcurl -plaintext -authority foo.com -d ‘{“name”: jun”}’ 127.0.0.1:80 order.OrderManagement/sayHello
  2. Hello jun