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

34 篇文章 1 订阅
21 篇文章 3 订阅

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. 执行命令,为名为recommendation的Service创建DestinationRule。
$ oc apply -f istiofiles/destination-rule-recommendation.yml -n ${ISTIO_APP}

注入延时故障

  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. 执行命令创建VirtualService。
$ oc apply -f istiofiles/virtual-service-recommendation-delay.yml -n ${ISTIO_APP}
  1. 执行命令,验证测试请求的响应时间。可以发现有的请求返回比较快,有的返回比较慢。
$ INGRESS_GATEWAY=$(oc get route istio-ingressgateway -n ${ISTIO_SYSTEM} -o 'jsonpath={.spec.host}')
$ ./scripts/run.sh $INGRESS_GATEWAY/customer
  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. 执行命令创建VirtualService。
$ oc apply -f istiofiles/virtual-service-recommendation-503.yml -n ${ISTIO_APP}
  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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值