基于istio实现dubbo3微服务治理
一、背景
使用dubbo3.0框架进行微服务的开发,云原生化的浪潮下,考虑将dubbo3.0的业务迁移到istio 服务网格进行流量治理。
二、方案简介
- dubbo3代码改造,主要是针对服务发现的配置
- 部署dubbo3应用到kubernetes
- istio自动注入Envoy容器实现流量拦截,配置istio流量治理策略进行流量管控
三、 Dubbo3代码更改适配
-
修改dubbo.properties
需要注意dubbo.application.name的值和该服务对应的k8s svcname 保持一致
-
业务代码注解修改
需要在客户端进行显式调用,providerBy=“xxx”, xxx 为服务端的k8s svcname
四: 实践操作
-
基础运行环境准备
部署kubernetes环境 & istio环境
-
创建namespace资源,并将该ns设置sidecar自动注入
apiVersion: v1 kind: Namespace metadata: name: dubbo-demo labels: istio-injection: enabled
-
部署dubboprovider
duboo 服务端正常启动
查看日志可以发现服务端注册成功:
部署文件参照如下: 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
-
部署consumer资源
consumer正常运行:
查看consumer负载实例日志信息:(Triple 协议被Envoy代理负载均衡)
部署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
-
查看provider的envoy日志
kubectl logs -f -n dubbo-demo dubbo-samples-mesh-provider-v1-759fd9c4c8-c4l42 -c istio-proxy
可以看到sidecar的inbound日志,provider作为服务端会接受入流量
查看consumer的envoy日志: consumer作为客户端,sidecar会打印出流量日志
consumer istio-proxy的日志,outbound信息如下:
-
创建流量治理规则
灰度发布规则为: 老版本承担80%的流量,新版本承担20%的流量
详细yaml如下: 主要是virtualservice和destinationruleapiVersion: 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
-
查看消费者日志发现流量比例趋势为8:2 符合规则
五: 参考资料
- 点赞
- 收藏
- 关注作者
评论(0)