比如,我们可以在frontend访问forecast时,让它遇到问题时重试5次。
1、设置策略
[root@master01 ~]# cd /root/cloud-native-istio/11_traffic-management/11.7
[root@master01 11.7]# kubectl apply -f vs-forecast-retry.yaml -n weather
2、查看策略
[root@master01 11.7]# 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":[{"retries":{"attempts":5,"perTryTimeout":"1s","retryOn":"5xx"},"route":[{"destination":{"host":"forecast","subset":"v2"}}]}]}}
creationTimestamp: "2023-11-09T01:59:22Z"
generation: 6
name: forecast-route
namespace: weather
resourceVersion: "1197963"
uid: 37e35b32-7449-4c5f-832a-178fe1a55583
spec:
hosts:
- forecast
http:
- retries:
attempts: 5 ##重试5次
perTryTimeout: 1s
retryOn: 5xx ##针对5xx的状态码进行重试
route:
- destination:
host: forecast
subset: v2
3、模拟故障
kubectl apply -f vs-recommendation-fault-abort.yaml -n weather
4、查看策略
[root@master01 11.7]# 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":{"abort":{"httpStatus":500,"percentage":{"value":100}}},"route":[{"destination":{"host":"recommendation","subset":"v1"}}]}]}}
creationTimestamp: "2023-11-09T02:26:09Z"
generation: 3
name: recommendation-route
namespace: weather
resourceVersion: "1198319"
uid: f5cb6910-5016-4279-8879-7431dc8a561b
spec:
hosts:
- recommendation
http:
- fault:
abort:
httpStatus: 500
percentage:
value: 100
route:
- destination:
host: recommendation
subset: v1
5、测试
[root@master01 11.7]# 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 503 Service Unavailable
content-type: application/json; charset=utf-8
content-length: 47
date: Sat, 11 Nov 2023 02:58:39 GMT
x-envoy-upstream-service-time: 372
server: envoy
6、查看日志数量
[root@master01 11.7]# kubectl logs -n weather `kubectl get po -n weather |grep forecast-v2 |awk '{print $1}'` -c istio-proxy |grep 503
[2023-11-09T07:06:19.862Z] "GET /weather?locate=%E5%8C%97%E4%BA%AC HTTP/1.1" 200 - via_upstream - "-" 0 665 13 12 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36" "58e2b3cc-45ae-9a42-a58a-93518c153503" "forecast:3002" "172.20.59.216:3002" inbound|3002|| 127.0.0.6:53633 172.20.59.216:3002 172.29.55.32:40058 outbound_.3002_.v2_.forecast.weather.svc.cluster.local default
[2023-11-09T07:06:20.411Z] "GET /activity?temp=31&weather=Clear HTTP/1.1" 200 - via_upstream - "-" 0 51 4 4 "-" "axios/0.18.0" "5ac1516c-70d0-9c79-ad76-90f9cbc3d65f" "recommendation:3005" "172.31.112.148:3005" outbound|3005|v1|recommendation.weather.svc.cluster.local 172.20.59.216:43180 10.0.128.69:3005 172.20.59.216:45034 - -
[2023-11-09T07:06:20.411Z] "GET /activity?temp=29&weather=Rain HTTP/1.1" 200 - via_upstream - "-" 0 57 4 4 "-" "axios/0.18.0" "1f3a3617-fe8c-91dc-9227-d7e428d1efbf" "recommendation:3005" "172.31.112.148:3005" outbound|3005|v1|recommendation.weather.svc.cluster.local 172.20.59.216:43178 10.0.128.69:3005 172.20.59.216:45032 - -
[2023-11-09T07:06:20.412Z] "GET /activity?temp=21&weather=Cloudy HTTP/1.1" 200 - via_upstream - "-" 0 44 4 4 "-" "axios/0.18.0" "dbcfd1eb-9459-9735-8be3-450bfb933d5c" "recommendation:3005" "172.31.112.148:3005" outbound|3005|v1|recommendation.weather.svc.cluster.local 172.20.59.216:43176 10.0.128.69:3005 172.20.59.216:45036 - -
2023-11-09T15:23:46.078503Z info xdsproxy connected to upstream XDS server: istiod.istio-system.svc:15012
2023-11-10T09:56:27.850307Z info xdsproxy connected to upstream XDS server: istiod.istio-system.svc:15012
[2023-11-11T02:58:39.195Z] "HEAD /weather?locate=hangzhou HTTP/1.1" 503 - via_upstream - "-" 0 0 5 5 "-" "curl/7.52.1" "351aabaf-b56b-9ec3-b45f-3b759b044ed9" "forecast:3002" "172.20.59.216:3002" inbound|3002|| 127.0.0.6:38265 172.20.59.216:3002 172.21.231.181:57404 outbound_.3002_.v2_.forecast.weather.svc.cluster.local default
[2023-11-11T02:58:39.216Z] "HEAD /weather?locate=hangzhou HTTP/1.1" 503 - via_upstream - "-" 0 0 2 2 "-" "curl/7.52.1" "351aabaf-b56b-9ec3-b45f-3b759b044ed9" "forecast:3002" "172.20.59.216:3002" inbound|3002|| 127.0.0.6:38265 172.20.59.216:3002 172.21.231.181:57404 outbound_.3002_.v2_.forecast.weather.svc.cluster.local default
[2023-11-11T02:58:39.264Z] "HEAD /weather?locate=hangzhou HTTP/1.1" 503 - via_upstream - "-" 0 0 2 2 "-" "curl/7.52.1" "351aabaf-b56b-9ec3-b45f-3b759b044ed9" "forecast:3002" "172.20.59.216:3002" inbound|3002|| 127.0.0.6:38265 172.20.59.216:3002 172.21.231.181:57404 outbound_.3002_.v2_.forecast.weather.svc.cluster.local default
[2023-11-11T02:58:39.353Z] "HEAD /weather?locate=hangzhou HTTP/1.1" 503 - via_upstream - "-" 0 0 2 2 "-" "curl/7.52.1" "351aabaf-b56b-9ec3-b45f-3b759b044ed9" "forecast:3002" "172.20.59.216:3002" inbound|3002|| 127.0.0.6:38265 172.20.59.216:3002 172.21.231.181:57404 outbound_.3002_.v2_.forecast.weather.svc.cluster.local default
[2023-11-11T02:58:39.535Z] "HEAD /weather?locate=hangzhou HTTP/1.1" 503 - via_upstream - "-" 0 0 2 2 "-" "curl/7.52.1" "351aabaf-b56b-9ec3-b45f-3b759b044ed9" "forecast:3002" "172.20.59.216:3002" inbound|3002|| 127.0.0.6:38265 172.20.59.216:3002 172.21.231.181:57404 outbound_.3002_.v2_.forecast.weather.svc.cluster.local default
[2023-11-11T02:58:39.564Z] "HEAD /weather?locate=hangzhou HTTP/1.1" 503 - via_upstream - "-" 0 0 2 1 "-" "curl/7.52.1" "351aabaf-b56b-9ec3-b45f-3b759b044ed9" "forecast:3002" "172.20.59.216:3002" inbound|3002|| 127.0.0.6:38265 172.20.59.216:3002 172.21.231.181:57404 outbound_.3002_.v2_.forecast.weather.svc.cluster.local default