<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          時速云使用 Higress 替換 Ngnix Ingress + Spring Cloud Gateway 的生產(chǎn)實(shí)踐

          共 5726字,需瀏覽 12分鐘

           ·

          2023-06-21 23:21

          作者:王金山,北京云思暢想科技有限公司技術(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

          時速云的 PaaS 平臺中存在著多款網(wǎng)關(guān)軟件:
          • 以 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

          Higress 可以作為 K8s 集群的 Ingress 入口網(wǎng)關(guān), 并且兼容了大量 K8s Nginx Ingress 的注解,可以從 K8s Nginx Ingress 快速平滑遷移到 Higress。如下是一個基于 Higress 自帶注解來實(shí)現(xiàn) REST 路由,并兼容 Nginx Ingress 注解重寫路徑的示例:
          apiVersion: networking.k8s.io/v1kind: Ingressmetadata:  annotations:    # 兼容 Nginx Ingress 注解    nginx.ingress.kubernetes.io/rewrite-target: /    # Higress 注解,支持 method/header/query 匹配路由    higress.io/match-method: POST    higress.io/exact-match-query-higressQuery: hi    higress.io/prefix-match-header-x-higress-header: hi  name: foospec:  ingressClassName: higress  rules:  - host: foo.example.com    http:      paths:      - pathType: Prefix        path: /foo        backend:          service:            name: foo-service            port:              number: 5678

          并且 Nginx Ingress 的 Lua 代碼性能比較差,Higress 對比 Nginx Ingress 的性能提升很大,如下圖所示:

          替代 Spring Cloud Gateway

          Spring Cloud 微服務(wù)體系下,作為微服務(wù)網(wǎng)關(guān)必須跟微服務(wù)注冊中心進(jìn)行對接,實(shí)現(xiàn)服務(wù)發(fā)現(xiàn)。Higress 提供了 McpBridge 這個 CRD,可以很方便地跟多種注冊中心對接,我們使用的 Spring Cloud 注冊中心是 Nacos,McpBridge 配置如下:
          apiVersion: networking.higress.io/v1kind: McpBridgemetadata:  name: default  namespace: higress-systemspec:  registries:    # 定義一個名為 my-nacos  的服務(wù)來源  - name: my-nacos    # 注冊中心類型是 Nacos 2.x,支持 gRPC 協(xié)議    type: nacos2    # 注冊中心的訪問地址,可以是域名或者IP    domain: 127.0.0.1    # 注冊中心的訪問端口,Nacos 默認(rèn)都是 8848    port: 8848    # Nacos 命名空間 ID    nacosNamespaceId: 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:    higress.io/destination: user-center.DEFAULT-GROUP.d8ac64f3-xxxx-xxxx-xxxx-47a814ecf358.nacos  name: user  namespace: defaultspec:  rules:  - http:      paths:      - backend:          resource:            apiGroup: networking.higress.io            kind: McpBridge            name: default        path: /        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

          時速云的 API 網(wǎng)關(guān)產(chǎn)品用于幫助企業(yè)和開發(fā)者完成 API 的創(chuàng)建、維護(hù)、發(fā)布、監(jiān)控告警等整個生命周期的管理。通過 API 網(wǎng)關(guān),以 API 的形式將后端服務(wù)開放出來,提供給各方使用。認(rèn)證鑒權(quán)能力是 API 網(wǎng)關(guān)中的關(guān)鍵能力,我們的 API 網(wǎng)關(guān)最初是基于 Kong 來構(gòu)建的,主要使用了 Kong 的 Key Auth/Basic Auth/JWT Auth/HMAC Auth 插件,而 Higress 也都提供了這些插件能力:
          以 Key Auth 為例,通過 Higress 提供的 WasmPlugin CRD 做如下配置即可:
          apiVersion: extensions.higress.io/v1alpha1kind: WasmPluginmetadata:  name: key-auth  namespace: higress-systemspec:  # 全局配置,配置認(rèn)證規(guī)則  defaultConfig:    consumers:    - credential: 2bda943c-xxxx-xxxx-xxxx-00163e1250b5      name: consumer1    - credential: c8c8e9ca-xxxx-xxxx-xxxx-e700dcc40e35      name: consumer2    keys:    - x-api-key    # 從請求header識別key    in_header: true    # 開啟全局認(rèn)證,consumer未識別將拒絕訪問    global_auth: true  # 匹配規(guī)則,配置授權(quán)規(guī)則  matchRules:  # 路由級生效配置,匹配default命名空間下名為foo的ingress  - ingress:    - default/foo    config:      # 僅允許 consumer1 訪問      allow:      - consumer1  # 域名級生效配置  - domain:    - www.test.com    - *.example.com    config:      # 僅允許 consumer1 訪問      allow:      - consumer2  url: 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

          時速云的服務(wù)網(wǎng)格產(chǎn)品是基于 Istio 搭建的,在 Istio 服務(wù)網(wǎng)格架構(gòu)中一般由 Istio Ingress Gateway 來負(fù)責(zé)南北向的流量治理。因?yàn)?Higress 也能夠支持 Istio API,所以我們也使用 Higress 統(tǒng)一了服務(wù)網(wǎng)格中的南北向流量管理。
          Higress 本身并未對 Istio 有強(qiáng)依賴,因此默認(rèn)關(guān)閉了 Istio 的 API 支持,需要通過 helm 參數(shù)配置開啟該支持:
          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: devops  namespace: higress-systemspec:  selector:    higress: higress-system-higress-gateway  servers:  - port:      number: 80      name: http      protocol: HTTP    hosts:    - devops.com---apiVersion: networking.istio.io/v1beta1kind: VirtualServicemetadata:  name: devops  namespace: higress-systemspec:  gateways:  - higress-system/devops  hosts:  - devops.com  http:  - name: default    route:    - destination:        host: devops.default.svc.cluster.local 

          基于 Istio API,Higress 也支持 TCP 路由,這可以替代我們之前使用 HAProxy 來代理 MySql 等中間件的功能,例如:

          apiVersion: networking.istio.io/v1beta1kind: Gatewaymetadata:  name: mysql   namespace: higress-systemspec:  selector:    higress: higress-system-higress-gatewayservers:  - hosts:    - '*'   port:     name: tcp     number: 3306     protocol: TCP---apiVersion: networking.istio.io/v1beta1kind: VirtualServicemetadata:  name: mysql  namespace: higress-systemspec:  gateways:  - mysql  hosts:  - '*'  tcp:  - match:    - port: 3306    route:    - destination:         host: mysql         port:           number: 3306         subset: v1   


          收益與展望




          Cloud Native

          遷移后的架構(gòu)基于 Higress 在產(chǎn)品層面做了各個技術(shù)棧的統(tǒng)一融合,增強(qiáng)了用戶體驗(yàn),具體收益如下:
          1. Higress 同時支持 K8s Ingress API 以及 Istio Gateway/VirtualService API,多個集群可以快速平滑升級

          2. 使用 Higress 進(jìn)行了多種網(wǎng)關(guān)的統(tǒng)一,統(tǒng)一了流量入口/路由負(fù)載/安全認(rèn)證的技術(shù)棧,實(shí)現(xiàn)了降本增效

          3. Higress 基于 Envoy,與東西向流量治理的 Sidecar 采用相同的技術(shù)棧,降低了開發(fā)人員擴(kuò)展和維護(hù)的成本

          最后,我們也對 Higress 未來發(fā)展提出幾點(diǎn)期待:

          1. 希望能盡快支持 Gateway API,并支持 TCPRoute/UDPRoute,增強(qiáng)四層能力

          2. 期待社區(qū)的 Wasm 插件生態(tài)越來越豐富,提供更多開箱即用的能力

          3. 目前時速云會在一個 K8s 集群中部署多套 Higress 網(wǎng)關(guān),希望能提供 Operator 機(jī)制來簡化運(yùn)維

          瀏覽 51
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产一区二区在线播放 | 国产乱淫AV片免费 | 操操影院 | 苍井空黄色视频 | 日皮视频在线免费观看 |