時速云使用 Higress 替換 Ngnix Ingress + Spring Cloud Gateway 的生產(chǎn)實(shí)踐
作者:王金山,北京云思暢想科技有限公司技術(shù)部微服務(wù)架構(gòu)師,負(fù)責(zé)公司 API 網(wǎng)關(guān)和服務(wù)網(wǎng)格等研發(fā)工作
時速云介紹
Cloud Native
時速云成立于2014年10月,致力于通過云原生技術(shù)幫助企業(yè)實(shí)現(xiàn)數(shù)字化轉(zhuǎn)型,擁有云原生應(yīng)用平臺 TCAP 和云原生數(shù)據(jù)平臺 KubeData 兩大核心產(chǎn)品體系,產(chǎn)品包含云原生 DevOps、容器云 PaaS、中間件服務(wù)、邊緣計(jì)算、微服務(wù)治理、服務(wù)網(wǎng)格、API 網(wǎng)關(guān)等。
需求背景
Cloud Native

以 HAProxy、Nginx Ingress 為基礎(chǔ)的流量網(wǎng)關(guān)
Spring Cloud 微服務(wù)體系的 Spring Cloud Gateway
作為 API 網(wǎng)關(guān)的 Kong
服務(wù)網(wǎng)格體系下的 Istio Ingress Gateway
以上產(chǎn)品雖說各自有自己的應(yīng)用場景,但是帶來的問題是技術(shù)棧各不相同,為了滿足不同的需求引入多種工具后,帶來的就是維護(hù)成本的急劇增加。因此,尋找一種既可以滿足所有需求,又可以使用統(tǒng)一技術(shù)棧的工具,成為我們追求的目標(biāo)。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 的服務(wù)來源name: my-nacos# 注冊中心類型是 Nacos 2.x,支持 gRPC 協(xié)議type: nacos2# 注冊中心的訪問地址,可以是域名或者IPdomain: 127.0.0.1# 注冊中心的訪問端口,Nacos 默認(rèn)都是 8848port: 8848# Nacos 命名空間 IDnacosNamespaceId: d8ac64f3-xxxx-xxxx-xxxx-47a814ecf358# Nacos 服務(wù)分組nacosGroups:DEFAULT_GROUP
接著,配置 Ingress 轉(zhuǎn)發(fā)到注冊在 Nacos 上的服務(wù) 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 微服務(wù)無需做任何改造,即可接入 Higress 網(wǎng)關(guān)。對比 Spring Cloud Gateway/Zuul 等傳統(tǒng) Java 微服務(wù)網(wǎng)關(guān), Higress 的性能高出2倍以上,可以顯著降低資源成本。
替代 Kong

apiVersion: extensions.higress.io/v1alpha1kind: WasmPluginmetadata:name: key-authnamespace: higress-systemspec:# 全局配置,配置認(rèn)證規(guī)則defaultConfig:consumers:credential: 2bda943c-xxxx-xxxx-xxxx-00163e1250b5name: consumer1credential: c8c8e9ca-xxxx-xxxx-xxxx-e700dcc40e35name: consumer2keys:x-api-key# 從請求header識別keyin_header: true# 開啟全局認(rèn)證,consumer未識別將拒絕訪問global_auth: true# 匹配規(guī)則,配置授權(quán)規(guī)則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
發(fā)起一個認(rèn)證請求,因?yàn)?xxx.exmaple.com 僅授權(quán)了 consumer2 訪問,所以下面這個 curl 命令將返回 403:
curl http://xxx.example.com/test -H 'x-api-key: 2bda943c-xxxx-xxxx-xxxx-00163e1250b5'此外,Higress 提供了更靈活的自定義插件機(jī)制,相比 Kong 新增插件需要重新部署網(wǎng)關(guān),Higress 可以動態(tài)擴(kuò)展并熱更新插件邏輯,對流量完全無損,而且也可以支持多種語言開發(fā),不局限于 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 進(jìn)行了多種網(wǎng)關(guān)的統(tǒng)一,統(tǒng)一了流量入口/路由負(fù)載/安全認(rèn)證的技術(shù)棧,實(shí)現(xiàn)了降本增效
Higress 基于 Envoy,與東西向流量治理的 Sidecar 采用相同的技術(shù)棧,降低了開發(fā)人員擴(kuò)展和維護(hù)的成本
最后,我們也對 Higress 未來發(fā)展提出幾點(diǎn)期待:
希望能盡快支持 Gateway API,并支持 TCPRoute/UDPRoute,增強(qiáng)四層能力
期待社區(qū)的 Wasm 插件生態(tài)越來越豐富,提供更多開箱即用的能力
目前時速云會在一個 K8s 集群中部署多套 Higress 網(wǎng)關(guān),希望能提供 Operator 機(jī)制來簡化運(yùn)維
