当前位置:   article > 正文

OpenShift 4 之Istio-Tutorial (7) 利用VirtualService的故障注入实现混沌测试Chaos Testing_virtualservice fault

virtualservice fault

OpenShift 4.x HOL教程汇总
说明:本文已经在OpenShift 4.12环境中验证

在VirtualService中可以对HTTP请求注入模拟的故障,从而实现混沌测试。这是通过在VirtualService中设置HTTPFaultInjection的delay和abort属性实现的。

  • delay用来延时访问。
  • abort用来终止访问。

下面以recommendation微服务为例实现delay和abort混沌测试。需要按照《OpenShift 4 之Istio-Tutorial (6) 服务恢复能力》的“环境准备”部署recommendation v1和recommendation 2,并且删除所有针对recommendation的VirtualService和DestinationRule。

  1. 首先查看istiofiles/destination-rule-recommendation.yml文件,其中定义了名为recommendation的DestinationRule,它会将发到名为recommendation的Service的请求平均分配给后台所有lable包含“app=recommendation”的Pod。
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: recommendation
spec:
  host: recommendation
  subsets:
  - labels:
      app: recommendation
    name: app-recommendation
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  1. 执行命令,为名为recommendation的Service创建DestinationRule。
$ oc apply -f istiofiles/destination-rule-recommendation.yml -n ${ISTIO_APP}
  • 1

注入延时故障

  1. 查看istiofiles/virtual-service-recommendation-delay.yml,其中在VirtualService中定义了将50%的请求注入7s的延时。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: recommendation
spec:
  hosts:
  - recommendation
  http:
  - fault:
      delay:
        fixedDelay: 7.000s
        percent: 50
    route:
    - destination:
        host: recommendation
        subset: app-recommendation
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  1. 执行命令创建VirtualService。
$ oc apply -f istiofiles/virtual-service-recommendation-delay.yml -n ${ISTIO_APP}
  • 1
  1. 执行命令,验证测试请求的响应时间。可以发现有的请求返回比较快,有的返回比较慢。
$ INGRESS_GATEWAY=$(oc get route istio-ingressgateway -n ${ISTIO_SYSTEM} -o 'jsonpath={.spec.host}')
$ ./scripts/run.sh $INGRESS_GATEWAY/customer
  • 1
  • 2
  1. 进入Kiali控制台的Distributed Trace,在Service选择recommendation,在Lookback选择1m,然后点击右侧的Search Traces按钮。然后在下方可以看到跟踪到的请求和消耗的时间。其中快的请求是ms级别,慢的请求超过7s。
    在这里插入图片描述

注入终止故障

  1. 查看istiofiles/virtual-service-recommendation-503.yml文件,它定义了终止abort故障,请求返回HTTP 503。
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: recommendation
spec:
  hosts:
  - recommendation
  http:
  - fault:
      abort:
        httpStatus: 503
        percentage:
          value: 100
    route:
    - destination:
        host: recommendation
        subset: version-v1
  - route:
    - destination:
        host: recommendation
        subset: version-v1
      weight: 100
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  1. 执行命令创建VirtualService。
$ oc apply -f istiofiles/virtual-service-recommendation-503.yml -n ${ISTIO_APP}
  • 1
  1. 执行命令连续访问customer微服务。可以看到有一部分会返回结果是503的错误。
$ ./scripts/run.sh $INGRESS_GATEWAY/customer
customer => preference => recommendation v1 from '67976848-4l4s7': 13
customer => Error: 503 - preference => Error: 503 - fault filter abort
customer => Error: 503 - preference => Error: 503 - fault filter abort
customer => preference => recommendation v1 from '67976848-4l4s7': 14
customer => preference => recommendation v1 from '67976848-4l4s7': 15
customer => Error: 503 - preference => Error: 503 - fault filter abort
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/365812
推荐阅读
相关标签
  

闽ICP备14008679号