基于istio实现dubbo3微服务治理

举报
可以交个朋友 发表于 2023/12/09 15:29:08 2023/12/09
【摘要】 背景使用dubbo3.0框架进行微服务的开发,云原生化的浪潮下,考虑将dubbo3.0的业务迁移到istio 服务网格进行流量治理。

一、背景

使用dubbo3.0框架进行微服务的开发,云原生化的浪潮下,考虑将dubbo3.0的业务迁移到istio 服务网格进行流量治理。


二、方案简介

  • dubbo3代码改造,主要是针对服务发现的配置
  • 部署dubbo3应用到kubernetes
  • istio自动注入Envoy容器实现流量拦截,配置istio流量治理策略进行流量管控

三、 Dubbo3代码更改适配

  1. 修改dubbo.properties
    需要注意dubbo.application.name的值和该服务对应的k8s svcname 保持一致
    image-20230912175208241.png

    image-20230912175516888.png

  2. 业务代码注解修改
    需要在客户端进行显式调用,providerBy=“xxx”, xxx 为服务端的k8s svcname
    image-20230912180016928.png


四: 实践操作

  1. 基础运行环境准备
    部署kubernetes环境 & istio环境
    image-20230912155139628.png

  2. 创建namespace资源,并将该ns设置sidecar自动注入

    apiVersion: v1
    kind: Namespace
    metadata:
      name: dubbo-demo
      labels:
        istio-injection: enabled
    
  3. 部署dubboprovider
    duboo 服务端正常启动
    image-20230912155845823.png

    查看日志可以发现服务端注册成功:
    image-20230912155717849.png

    部署文件参照如下: deployment-v1.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: dubbo-samples-mesh-provider-v1
      namespace: dubbo-demo
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: dubbo-samples-mesh-provider
          version: v1
      template:
        metadata:
          labels:
            app: dubbo-samples-mesh-provider
            version: v1
          annotations:
            # Prevent istio rewrite http probe
            sidecar.istio.io/rewriteAppHTTPProbers: "false"
        spec:
          containers:
            - name: server
              image: apache/dubbo-demo:dubbo-samples-mesh-provider-v1_0.0.1
              imagePullPolicy: Always
              ports:
                - name: grpc-tri
                  containerPort: 50052
                  protocol: TCP
                - name: http-health
                  containerPort: 22222
                  protocol: TCP
              livenessProbe:
                httpGet:
                  path: /live
                  port: http-health
                initialDelaySeconds: 10
                periodSeconds: 5
                timeoutSeconds: 1
              readinessProbe:
                httpGet:
                  path: /ready
                  port: http-health
                initialDelaySeconds: 5
                periodSeconds: 5
                timeoutSeconds: 2
              startupProbe:
                httpGet:
                  path: /startup
                  port: http-health
                failureThreshold: 30
                initialDelaySeconds: 10
                periodSeconds: 5
                timeoutSeconds: 2
    
    

    Deployment-v2.yaml 如下:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: dubbo-samples-mesh-provider-v2
      namespace: dubbo-demo
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: dubbo-samples-mesh-provider
          version: v2
      template:
        metadata:
          labels:
            app: dubbo-samples-mesh-provider
            version: v2
          annotations:
            # Prevent istio rewrite http probe
            sidecar.istio.io/rewriteAppHTTPProbers: "false"
        spec:
          containers:
            - name: server
              image: apache/dubbo-demo:dubbo-samples-mesh-provider-v2_0.0.1
              imagePullPolicy: Always
              ports:
                - name: grpc-tri
                  containerPort: 50052
                  protocol: TCP
                - name: http-health
                  containerPort: 22222
                  protocol: TCP
              livenessProbe:
                httpGet:
                  path: /live
                  port: http-health
                initialDelaySeconds: 10
                periodSeconds: 5
                timeoutSeconds: 1
              readinessProbe:
                httpGet:
                  path: /ready
                  port: http-health
                initialDelaySeconds: 5
                periodSeconds: 5
                timeoutSeconds: 2
              startupProbe:
                httpGet:
                  path: /startup
                  port: http-health
                failureThreshold: 30
                initialDelaySeconds: 10
                periodSeconds: 5
                timeoutSeconds: 2
    

    service.yaml,需要注意portname以grpc开头,istio需要根据此name来识别治理的协议

    apiVersion: v1
    kind: Service
    metadata:
      name: dubbo-samples-mesh-provider
      namespace: dubbo-demo
    spec:
      type: ClusterIP
      sessionAffinity: None
      selector:
        app: dubbo-samples-mesh-provider
      ports:
        - name: grpc-tri
          port: 50052
          targetPort: 50052
    
  4. 部署consumer资源
    consumer正常运行:
    image-20230912160403530.png

    查看consumer负载实例日志信息:(Triple 协议被Envoy代理负载均衡)
    image-20230912160703801.png

    部署yaml参照如下 deployment.yaml:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: dubbo-samples-mesh-consumer
      namespace: dubbo-demo
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: dubbo-samples-mesh-consumer
          version: v1
      template:
        metadata:
          labels:
            app: dubbo-samples-mesh-consumer
            version: v1
          annotations:
            # Prevent istio rewrite http probe
            sidecar.istio.io/rewriteAppHTTPProbers: "false"
        spec:
          containers:
            - name: server
              image: apache/dubbo-demo:dubbo-samples-mesh-consumer_0.0.1
              imagePullPolicy: Always
              ports:
                - name: grpc-tri
                  containerPort: 50052
                  protocol: TCP
                - name: http-health
                  containerPort: 22222
                  protocol: TCP
              env:
                - name: POD_NAMESPACE
                  valueFrom:
                    fieldRef:
                      fieldPath: metadata.namespace
                # This environment variable does not need to be configured by default. When the domain name suffix used inside k8s is artificially changed, it is only necessary to configure this
                #- name: CLUSTER_DOMAIN
                #  value: cluster.local
              livenessProbe:
                httpGet:
                  path: /live
                  port: http-health
                initialDelaySeconds: 5
                periodSeconds: 5
              readinessProbe:
                httpGet:
                  path: /ready
                  port: http-health
                initialDelaySeconds: 5
                periodSeconds: 5
              startupProbe:
                httpGet:
                  path: /startup
                  port: http-health
                failureThreshold: 30
                initialDelaySeconds: 5
                periodSeconds: 5
                timeoutSeconds: 2
    
    

    Consumer-svc.yaml

    apiVersion: v1
    kind: Service
    metadata:
      name: dubbo-samples-mesh-consumer
      namespace: dubbo-demo
    spec:
      type: ClusterIP
      sessionAffinity: None
      selector:
        app: dubbo-samples-mesh-consumer
      ports:
        - name: grpc-dubbo
          protocol: TCP
          port: 50052
          targetPort: 50052
    
  5. 查看provider的envoy日志

    kubectl logs -f -n dubbo-demo dubbo-samples-mesh-provider-v1-759fd9c4c8-c4l42 -c istio-proxy

    可以看到sidecar的inbound日志,provider作为服务端会接受入流量
    image-20230912161306569.png

    查看consumer的envoy日志: consumer作为客户端,sidecar会打印出流量日志
    consumer istio-proxy的日志,outbound信息如下:
    image-20230912172839511.png

  6. 创建流量治理规则
    灰度发布规则为: 老版本承担80%的流量,新版本承担20%的流量
    image-20230912173558937.png
    详细yaml如下: 主要是virtualservice和destinationrule

    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: dubbo-samples-mesh-provider
      namespace: dubbo-demo
    spec:
      hosts:
        - dubbo-samples-mesh-provider.dubbo-demo.svc.cluster.local
      http:
        - route:
            - destination:
                host: dubbo-samples-mesh-provider.dubbo-demo.svc.cluster.local
                subset: v1
                port:
                  number: 50052
              weight: 80
            - destination:
                host: dubbo-samples-mesh-provider.dubbo-demo.svc.cluster.local
                subset: v2
                port:
                  number: 50052
              weight: 20
    
    ---
    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
      name: dubbo-samples-mesh-provider
      namespace: dubbo-demo
    spec:
      host: dubbo-samples-mesh-provider.dubbo-demo.svc.cluster.local
      trafficPolicy:
        loadBalancer:
          simple: ROUND_ROBIN
      subsets:
        - name: v1
          labels:
            version: v1
        - name: v2
          labels:
            version: v2
    
  7. 查看消费者日志发现流量比例趋势为8:2 符合规则
    image-20230912174502832.png


五: 参考资料

  1. https://cn.dubbo.apache.org/zh-cn/overview/tasks/mesh/dubbo-mesh/
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。