在前面章节的学习中,我们了解到,天气预报应用中frontend会调用forecast,而forecast会调用recommendation

那么,我们就依据此调用关系,来设置一个超时策略,比如将超时策略设置在frontend和forecast中间,然后在forecast和recommendation中间设置一个延迟注入的策略,来模拟超时。
1、设置超时策略
[root@master01 ~]# cd /root/cloud-native-istio/11_traffic-management/11.6
[root@master01 11.6]# kubectl apply -f vs-forecast-timeout.yaml -n weather
查看策略
[root@master01 11.6]# kubectl get vs forecast-route -n weather -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":"forecast-route","namespace":"weather"},"spec":{"hosts":["forecast"],"http":[{"route":[{"destination":{"host":"forecast","subset":"v2"}}],"timeout":"1s"}]}}
creationTimestamp: "2023-11-09T01:59:22Z"
generation: 5
name: forecast-route
namespace: weather
resourceVersion: "1197197"
uid: 37e35b32-7449-4c5f-832a-178fe1a55583
spec:
hosts:
- forecast
http:
- route:
- destination:
host: forecast
subset: v2
timeout: 1s
到frontend的pod里,curl访问forecast进行测试
[root@master01 11.6]# kubectl -n weather exec -it `kubectl get po -n weather|grep frontend-v1|awk '{print $1}'` -- bash -c 'curl -I http://forecast:3002/weather?locate=hangzhou'
HTTP/1.1 200 OK
content-type: application/json; charset=utf-8
content-length: 665
date: Sat, 11 Nov 2023 02:52:51 GMT
x-envoy-upstream-service-time: 48
server: envoy
说明:由于该请求返回时间远远少于1s,所以状态码为200,是正常的。
2、注入延迟策略
[root@master01 ~]# cd /root/cloud-native-istio/11_traffic-management/11.6
[root@master01 11.6]# kubectl apply -f vs-recommendation-fault-delay.yaml -n weather
查看策略
[root@master01 11.6]# kubectl get vs recommendation-route -n weather -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":"recommendation-route","namespace":"weather"},"spec":{"hosts":["recommendation"],"http":[{"fault":{"delay":{"fixedDelay":"4s","percentage":{"value":100}}},"route":[{"destination":{"host":"recommendation","subset":"v1"}}]}]}}
creationTimestamp: "2023-11-09T02:26:09Z"
generation: 2
name: recommendation-route
namespace: weather
resourceVersion: "1197482"
uid: f5cb6910-5016-4279-8879-7431dc8a561b
spec:
hosts:
- recommendation
http:
- fault:
delay:
fixedDelay: 4s
percentage:
value: 100
route:
- destination:
host: recommendation
subset: v1
到frontend的pod里,curl访问forecast再次进行测试
[root@master01 11.6]# kubectl -n weather exec -it `kubectl get po -n weather|grep frontend-v1|awk '{print $1}'` -- bash -c 'curl -I http://forecast:3002/weather?locate=hangzhou'
HTTP/1.1 504 Gateway Timeout
content-length: 24
content-type: text/plain
date: Sat, 11 Nov 2023 02:54:22 GMT
server: envoy
说明:超过了1s,直接返回504状态码,而不会等待4s。