延迟注入(针对VirtualService)

延迟注入通过在服务之间的网络通信中引入人为的延迟,模拟实际生产环境中网络延迟的情况。

1、观察正常从frontend访问advertisement的返回时间,可以看到,返回时间非常短

[root@master01 11.4]# kubectl -n weather exec -it `kubectl get po -n weather|grep frontend-v1|awk '{print $1}'` -- bash -c 'time  curl http://advertisement:3003/ad'

{"adImgName":"airCleanImg"}

real    0m0.020s
user    0m0.005s
sys     0m0.000s

2、将该时间做一个延迟,比如延迟3s

配置VirtualService的路由规则

[root@master01 ~]# cd /root/cloud-native-istio/11_traffic-management/11.5
[root@master01 11.5]# kubectl apply -f vs-advertisement-fault-delay.yaml -n weather

查看规则

[root@master01 11.5]# kubectl -n weather get vs advertisement-route -o yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"networking.istio.io/v1alpha3","kind":"VirtualService","metadata":{"annotations":{},"name":"advertisement-route","namespace":"weather"},"spec":{"hosts":["advertisement"],"http":[{"fault":{"delay":{"fixedDelay":"3s","percentage":{"value":100}}},"route":[{"destination":{"host":"advertisement","subset":"v1"}}]}]}}
  creationTimestamp: "2023-11-09T01:59:22Z"
  generation: 2
  name: advertisement-route
  namespace: weather
  resourceVersion: "1190410"
  uid: c9228253-7c58-4d29-b739-f3a90155d3a4
spec:
  hosts:
  - advertisement
  http:
  - fault:
      delay:
        fixedDelay: 3s
        percentage:
          value: 100
    route:
    - destination:
        host: advertisement
        subset: v1

再次测试返回时间。这样,把返回时间延迟了3s,从而帮助研发人员来发现潜在影响。

[root@master01 11.5]# kubectl -n weather exec -it `kubectl get po -n weather|grep frontend-v1|awk '{print $1}'` -- bash -c 'time  curl http://advertisement:3003/ad'

{"adImgName":"airCleanImg"}

real    0m3.013s
user    0m0.002s
sys     0m0.003s

中断注入(针对VirtualService)

故意把http状态码编辑为一个错误的状态码,比如500,从而模拟该服务暂时不可用。

1、配置VirtualService的路由规则

[root@master01 ~]# cd /root/cloud-native-istio/11_traffic-management/11.5
[root@master01 11.5]# kubectl apply -f vs-advertisement-fault-abort.yaml -n weather

查看规则

[root@master01 11.5]# kubectl -n weather get vs advertisement-route -o yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"networking.istio.io/v1alpha3","kind":"VirtualService","metadata":{"annotations":{},"name":"advertisement-route","namespace":"weather"},"spec":{"hosts":["advertisement"],"http":[{"fault":{"abort":{"httpStatus":500,"percentage":{"value":100}}},"route":[{"destination":{"host":"advertisement","subset":"v1"}}]}]}}
  creationTimestamp: "2023-11-09T01:59:22Z"
  generation: 3
  name: advertisement-route
  namespace: weather
  resourceVersion: "1191066"
  uid: c9228253-7c58-4d29-b739-f3a90155d3a4
spec:
  hosts:
  - advertisement
  http:
  - fault:
      abort:
        httpStatus: 500
        percentage:
          value: 100
    route:
    - destination:
        host: advertisement
        subset: v1

2、测试状态码。实际上,该服务并没有任何故障,只是我们故意将它设置为故障的状态,从而让研发人员发现潜在的影响。

[root@master01 11.5]# kubectl -n weather exec -it `kubectl get po -n weather|grep frontend-v1|awk '{print $1}'` -- bash -c 'curl -I http://advertisement:3003/ad'

HTTP/1.1 500 Internal Server Error
content-length: 18
content-type: text/plain
date: Sat, 11 Nov 2023 02:19:57 GMT
server: envoy