时速云使用 Higress 替换 Ngnix Ingress + Spring Cloud Gateway 的生产实践
作者:王金山,北京云思畅想科技有限公司技术部微服务架构师,负责公司 API 网关和服务网格等研发工作
时速云介绍
Cloud Native
时速云成立于2014年10月,致力于通过云原生技术帮助企业实现数字化转型,拥有云原生应用平台 TCAP 和云原生数据平台 KubeData 两大核心产品体系,产品包含云原生 DevOps、容器云 PaaS、中间件服务、边缘计算、微服务治理、服务网格、API 网关等。
需求背景
Cloud Native

以 HAProxy、Nginx Ingress 为基础的流量网关
Spring Cloud 微服务体系的 Spring Cloud Gateway
作为 API 网关的 Kong
服务网格体系下的 Istio Ingress Gateway
以上产品虽说各自有自己的应用场景,但是带来的问题是技术栈各不相同,为了满足不同的需求引入多种工具后,带来的就是维护成本的急剧增加。因此,寻找一种既可以满足所有需求,又可以使用统一技术栈的工具,成为我们追求的目标。Higress 正符合我们的需求。
Higress 解决方案
Cloud Native
替代 Nginx Ingress
apiVersion: networking.k8s.io/v1kind: Ingressmetadata:annotations:# 兼容 Nginx Ingress 注解: /# Higress 注解,支持 method/header/query 匹配路由: POST: hi: hiname: foospec:ingressClassName: higressrules:host: foo.example.comhttp:paths:pathType: Prefixpath: /foobackend:service:name: foo-serviceport:number: 5678
并且 Nginx Ingress 的 Lua 代码性能比较差,Higress 对比 Nginx Ingress 的性能提升很大,如下图所示:

替代 Spring Cloud Gateway
apiVersion: networking.higress.io/v1kind: McpBridgemetadata:name: defaultnamespace: higress-systemspec:registries:# 定义一个名为 my-nacos 的服务来源name: my-nacos# 注册中心类型是 Nacos 2.x,支持 gRPC 协议type: nacos2# 注册中心的访问地址,可以是域名或者IPdomain: 127.0.0.1# 注册中心的访问端口,Nacos 默认都是 8848port: 8848# Nacos 命名空间 IDnacosNamespaceId: d8ac64f3-xxxx-xxxx-xxxx-47a814ecf358# Nacos 服务分组nacosGroups:DEFAULT_GROUP
接着,配置 Ingress 转发到注册在 Nacos 上的服务 user-center:
apiVersion: networking.k8s.io/v1kind: Ingressmetadata:annotations:: user-center.DEFAULT-GROUP.d8ac64f3-xxxx-xxxx-xxxx-47a814ecf358.nacosname: usernamespace: defaultspec:rules:http:paths:backend:resource:apiGroup: networking.higress.iokind: McpBridgename: defaultpath: /pathType: Prefix
这样 Spring Cloud 微服务无需做任何改造,即可接入 Higress 网关。对比 Spring Cloud Gateway/Zuul 等传统 Java 微服务网关, Higress 的性能高出2倍以上,可以显著降低资源成本。
替代 Kong

apiVersion: extensions.higress.io/v1alpha1kind: WasmPluginmetadata:name: key-authnamespace: higress-systemspec:# 全局配置,配置认证规则defaultConfig:consumers:credential: 2bda943c-xxxx-xxxx-xxxx-00163e1250b5name: consumer1credential: c8c8e9ca-xxxx-xxxx-xxxx-e700dcc40e35name: consumer2keys:x-api-key# 从请求header识别keyin_header: true# 开启全局认证,consumer未识别将拒绝访问global_auth: true# 匹配规则,配置授权规则matchRules:# 路由级生效配置,匹配default命名空间下名为foo的ingressingress:default/fooconfig:# 仅允许 consumer1 访问allow:consumer1# 域名级生效配置domain:www.test.com*.example.comconfig:# 仅允许 consumer1 访问allow:consumer2url: oci://higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/key-auth:1.0.0
发起一个认证请求,因为 xxx.exmaple.com 仅授权了 consumer2 访问,所以下面这个 curl 命令将返回 403:
curl http://xxx.example.com/test -H 'x-api-key: 2bda943c-xxxx-xxxx-xxxx-00163e1250b5'此外,Higress 提供了更灵活的自定义插件机制,相比 Kong 新增插件需要重新部署网关,Higress 可以动态扩展并热更新插件逻辑,对流量完全无损,而且也可以支持多种语言开发,不局限于 Lua 语言。
替代 Istio Ingress Gateway
helm upgrade higress -n higress-system higress.io/higress --reuse-values --set global.enableIstioAPI=true开启后,可以直接使用 Istio API 来管理 Higress 上的路由:
apiVersion: networking.istio.io/v1alpha3kind: Gatewaymetadata:name: devopsnamespace: higress-systemspec:selector:higress: higress-system-higress-gatewayservers:port:number: 80name: httpprotocol: HTTPhosts:devops.com---apiVersion: networking.istio.io/v1beta1kind: VirtualServicemetadata:name: devopsnamespace: higress-systemspec:gateways:higress-system/devopshosts:devops.comhttp:name: defaultroute:destination:host: devops.default.svc.cluster.local
基于 Istio API,Higress 也支持 TCP 路由,这可以替代我们之前使用 HAProxy 来代理 MySql 等中间件的功能,例如:
apiVersion: networking.istio.io/v1beta1kind: Gatewaymetadata:name: mysqlnamespace: higress-systemspec:selector:higress: higress-system-higress-gatewayservers:hosts:'*'port:name: tcpnumber: 3306protocol: TCP---apiVersion: networking.istio.io/v1beta1kind: VirtualServicemetadata:name: mysqlnamespace: higress-systemspec:gateways:mysqlhosts:'*'tcp:match:port: 3306route:destination:host: mysqlport:number: 3306subset: v1
收益与展望
Cloud Native

Higress 同时支持 K8s Ingress API 以及 Istio Gateway/VirtualService API,多个集群可以快速平滑升级
使用 Higress 进行了多种网关的统一,统一了流量入口/路由负载/安全认证的技术栈,实现了降本增效
Higress 基于 Envoy,与东西向流量治理的 Sidecar 采用相同的技术栈,降低了开发人员扩展和维护的成本
最后,我们也对 Higress 未来发展提出几点期待:
希望能尽快支持 Gateway API,并支持 TCPRoute/UDPRoute,增强四层能力
期待社区的 Wasm 插件生态越来越丰富,提供更多开箱即用的能力
目前时速云会在一个 K8s 集群中部署多套 Higress 网关,希望能提供 Operator 机制来简化运维
