4.16 Istio故障注入测试:模拟服务故障验证系统容错能力

引言

故障注入是测试系统容错能力的重要方法。Istio提供了故障注入功能,可以模拟延迟、错误等故障场景。本文将详细介绍故障注入的使用方法。

一、故障注入概述

1.1 故障类型

  • 延迟注入:模拟网络延迟
  • 错误注入:模拟服务错误

1.2 使用场景

  • 容错测试
  • 性能测试
  • 恢复能力验证
  • 用户体验测试

二、延迟注入

2.1 延迟配置

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - fault:
      delay:
        percentage:
          value: 50
        fixedDelay: 5s
    route:
    - destination:
        host: ratings
        subset: v1

2.2 指数延迟

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
spec:
  http:
  - fault:
      delay:
        percentage:
          value: 100
        exponentialDelay: 1s

三、错误注入

3.1 HTTP错误

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - fault:
      abort:
        percentage:
          value: 10
        httpStatus: 500
    route:
    - destination:
        host: ratings
        subset: v1

3.2 自定义错误

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
spec:
  http:
  - fault:
      abort:
        percentage:
          value: 50
        httpStatus: 503
        grpcStatus: UNAVAILABLE

四、组合故障

4.1 延迟+错误

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
spec:
  http:
  - fault:
      delay:
        percentage:
          value: 50
        fixedDelay: 5s
      abort:
        percentage:
          value: 10
        httpStatus: 500
    route:
    - destination:
        host: ratings

五、实战案例

5.1 测试容错

# 注入50%延迟和10%错误
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - match:
    - headers:
        end-user:
          exact: jason
    fault:
      delay:
        percentage:
          value: 50
        fixedDelay: 5s
      abort:
        percentage:
          value: 10
        httpStatus: 500
    route:
    - destination:
        host: ratings
        subset: v1

六、操作管理

6.1 创建和测试

# 创建故障注入
kubectl apply -f fault-injection.yaml

# 测试效果
for i in {1..100}; do curl http://<gateway-ip>/productpage; done

# 观察错误率和延迟

6.2 移除故障

# 删除故障注入
kubectl delete virtualservice ratings

七、最佳实践

7.1 测试建议

  • 逐步增加故障率
  • 监控系统响应
  • 验证恢复能力
  • 记录测试结果

总结

通过本文,你学会了:

  1. 延迟注入:固定延迟、指数延迟
  2. 错误注入:HTTP错误、自定义错误
  3. 组合故障:延迟+错误
  4. 实战案例:容错测试

下一步学习

  • 4.17 Istio熔断限流实战
  • 4.18 Istio多集群部署实战

思考题

  1. 故障注入的作用是什么?
  2. 如何测试系统的容错能力?
  3. 延迟注入和错误注入有什么区别?

提示:故障注入是验证系统可靠性的重要方法。下一节我们将学习熔断限流。

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐