OpenShift 4 之Istio-Tutorial (5) 其它流量控制场景以及VirtualService和DestinationRule的关系

本系列OpenShift Servic Mesh教程是基于Red Hat官方公开发行的《Introducing Istio Service Mesh for Micoservices》出版物,我将所有操作在OpenShift 4.2.x环境中进行了验证。喜欢读英文或者需要了解更多和场景相关知识点的小伙伴可以通过上面的链接下载该书慢慢阅读。

在上一节我们了解可如何通过流量管理实现灰度发布,本节我们再介绍Istio的其它两个流量控制功能:流量镜像和流量负载均衡。这些功能都是通过VirtualService和DestinationRule实现的。
OpenShift 4 之Istio-Tutorial (5) 其它流量控制场景以及VirtualService和DestinationRule的关系_第1张图片

文章目录

  • 流量镜像
  • Service对于Pod的流量负载均衡
  • VirtualService和DestinationRule的关系

流量镜像

  1. 在第一个命令窗口获得运行recommendation-v1和recommendation-v2的pod实例名。
$ oc get pod -n tutorial| grep recommendation
recommendation-v1-549bf5fcb9-zdxhd   2/2     Running   0          154m
recommendation-v2-599867df6c-5t2b7   2/2     Running   0          87m
  1. 在第一个命令窗口执行命令查看运行recommendation-v2微服务的容器日志。
$ oc logs -f recommendation-v2-599867df6c-5t2b7 -c recommendation -n tutorial
  1. 在第二个命令窗口执行命令修改VirtualService策略,将请求发到recommendation-v1上,同时镜像转发到recommendation-v2上。
    文件 istiofiles/virtual-service-recommendation-v1-mirror-v2.yml定义了镜像mirror的转发策略。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: recommendation
spec:
  hosts:
  - recommendation
  http:
  - route:
    - destination:
        host: recommendation
        subset: version-v1
    mirror:
      host: recommendation
      subset: version-v2
  1. 执行命令修改对recommendation的VirtualService策略:
$ oc apply -f istiofiles/virtual-service-recommendation-v1-mirror-v2.yml -n tutorial
  1. 在第二个命令窗口向customer微服务请求。此时此窗口可以看到请求发到“recommendation v1”
$ curl $INGRESS_GATEWAY/customer
customer => preference => recommendation v1 from '549bf5fcb9-zdxhd': 1924
  1. 在第一个命令窗口中同时会看到recommendation-v2日志有一行新纪录,说明请求也被镜像转发到了recommendation-v2微服务。
  2. 删除基于镜像策略的VirutalService对象。
$ oc delete -f istiofiles/virtual-service-recommendation-v1-mirror-v2.yml -n tutorial

Service对于Pod的流量负载均衡

当一个微服务有多个版本或实例的时候,如果没有为Serivce定义DestinationRule,Serivce是按照轮序(round-robin)的方式将请求发给它对应的多个Pod。我们可以为Serivce定义DestinationRule对象,以实现从Service到Pod的负载均衡策略。

  1. 删除上一步创建的基于镜像策略的VirutalService对象。注意:此时已经没有针对recommendation的VirutalService了。
$ oc delete -f istiofiles/virtual-service-recommendation-v1-mirror-v2.yml -n tutorial
$ oc get istio-io
NAME                                                 HOST             AGE
destinationrule.networking.istio.io/recommendation   recommendation   79m
NAME                                      AGE
gateway.networking.istio.io/customer-gw   21h
NAME                                             GATEWAYS        HOSTS   AGE
virtualservice.networking.istio.io/customer-vs   [customer-gw]   [*]     21h
  1. 将运行recommendation-v2微服务Pod实例增加到3个。
$ oc scale deployment recommendation-v2 --replicas=3 -n tutorial
  1. 运行命令,查看请求转发到recommendation微服务的情况。此时从序列号可以看到请求是按照Pod实例轮训分发的。
./scripts/run.sh $INGRESS_GATEWAY/customer
customer => preference => recommendation v1 from '67976848-4l4s7': 1332
customer => preference => recommendation v2 from '3cbba7a9cde5': 82
customer => preference => recommendation v2 from '3cbba7a9cde5': 1
customer => preference => recommendation v2 from '3cbba7a9cde5': 1
customer => preference => recommendation v1 from '67976848-4l4s7': 1333
customer => preference => recommendation v2 from '3cbba7a9cde5': 83
customer => preference => recommendation v2 from '3cbba7a9cde5': 2
customer => preference => recommendation v2 from '3cbba7a9cde5': 2
...
  1. 运行命令修改DestinationRule对象的负载均衡策略为随机发送。
    文件istiofiles/destination-rule-recommendation_lb_policy_app.yml中配置对所有recommendation的为随机发送,无论是recommendation-v1还是recommendation-v2,无论是那个pod实例。
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: recommendation
spec:
  host: recommendation
  trafficPolicy:
    loadBalancer:
      simple: RANDOM

执行命令修改DestinationRule对象:

$ oc apply -f istiofiles/destination-rule-recommendation_lb_policy_app.yml -n tutorial
  1. 运行命令,查看请求转发到recommendation微服务的情况。从序列号可以看到请求是随机分发到后台不同recommendation的实例。
./scripts/run.sh $INGRESS_GATEWAY/customer
customer => preference => recommendation v2 from '3cbba7a9cde5': 4
customer => preference => recommendation v1 from '67976848-4l4s7': 1420
customer => preference => recommendation v2 from '3cbba7a9cde5': 26
customer => preference => recommendation v1 from '67976848-4l4s7': 1421
customer => preference => recommendation v2 from '3cbba7a9cde5': 5
customer => preference => recommendation v2 from '3cbba7a9cde5': 6
customer => preference => recommendation v2 from '3cbba7a9cde5': 227
customer => preference => recommendation v1 from '67976848-4l4s7': 1422
customer => preference => recommendation v2 from '3cbba7a9cde5': 7
customer => preference => recommendation v2 from '3cbba7a9cde5': 228
customer => preference => recommendation v2 from '3cbba7a9cde5': 229
customer => preference => recommendation v2 from '3cbba7a9cde5': 8
customer => preference => recommendation v1 from '67976848-4l4s7': 1423
customer => preference => recommendation v1 from '67976848-4l4s7': 1424
customer => preference => recommendation v2 from '3cbba7a9cde5': 5
customer => preference => recommendation v2 from '3cbba7a9cde5': 230
customer => preference => recommendation v2 from '3cbba7a9cde5': 9
customer => preference => recommendation v1 from '67976848-4l4s7': 1425
customer => preference => recommendation v2 from '3cbba7a9cde5': 6
customer => preference => recommendation v2 from '3cbba7a9cde5': 10
customer => preference => recommendation v1 from '67976848-4l4s7': 1426
customer => preference => recommendation v1 from '67976848-4l4s7': 1427
customer => preference => recommendation v1 from '67976848-4l4s7': 1428
customer => preference => recommendation v1 from '67976848-4l4s7': 1429
customer => preference => recommendation v2 from '3cbba7a9cde5': 7
customer => preference => recommendation v2 from '3cbba7a9cde5': 231
customer => preference => recommendation v1 from '67976848-4l4s7': 1430
customer => preference => recommendation v2 from '3cbba7a9cde5': 232
...

VirtualService和DestinationRule的关系

从上面的操作我们可以看出VirtualService对象和DestinationRule对象既可以单独使用、也可以结合使用。功能上VirtualService的功能之一是在后端不同Service中选择一个转发请求,而Service是在后端不同Pod中选择一个转发请求。VirtualService和DestinationRule是通过subnet关联起来的。
本文第二个场景“Service对于Pod的流量负载均衡”就是独立使用DestinationRule,而VirtualService也独立使用,例如下面把对reviews.com的请求中的一部分发到dev.reviews.com。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews-route-two-domains
spec:
  hosts:
  - reviews.com
  http:
  - route:
    - destination:
        host: dev.reviews.com
      weight: 25
    - destination:
        host: reviews.com
      weight: 75

你可能感兴趣的:(OpenShift,4,ServiceMesh,微服务)