第一步还是需要移除之前创建的 VirtualService
[root@k8s-master ~]# istioctl delete virtualservice reviews Command "delete" is deprecated, Use `kubectl delete` instead (see https://kubernetes.io/docs/tasks/tools/install-kubectl) Deleted config: virtualservice reviews [root@k8s-master ~]# istioctl get virtualservice Command "get" is deprecated, Use `kubectl get` instead (see https://kubernetes.io/docs/tasks/tools/install-kubectl) VIRTUAL-SERVICE NAME GATEWAYS HOSTS #HTTP #TCP NAMESPACE AGE bookinfo bookinfo-gateway * 1 0 default 3d [root@k8s-master ~]#
然后我们查看 istio 样例文件夹下面的文件:samples/bookinfo/networking/virtual-service-ratings-test-delay.yaml 的内容
# cat samples/bookinfo/networking/virtual-service-ratings-test-delay.yaml apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: ratings spec: hosts: - ratings http: - match: - headers: end-user: exact: jason fault: delay: percentage: value: 100.0 fixedDelay: 7s route: - destination: host: ratings subset: v1 - route: - destination: host: ratings subset: v1
这个 VirtualService 定义了一个在 jason 登录的情况下,访问 ratings 服务的 100% 的 7s 访问延迟。前面我们知道,Bookinfo 这个示例 productpage 服务调用 reviews,reviews 的不同版本会对 ratings 进行不同的调用,其中 reviews-v1 不调用 ratings,reviews-v2 和 reviews-v3 会调用 ratings,并做不同样式的渲染。并且在 productpage 访问 reviews 时,代码中有硬编码 6s 中的访问超时限制,而 reviews 访问 ratings 编码了 10s 的访问超时限制。
[root@k8s-master istio-1.3.1]# kubectl create -f samples/bookinfo/networking/virtual-service-ratings-test-delay.yaml The VirtualService "ratings" is invalid: []: Invalid value: map[string]interface {}{"apiVersion":"networking.istio.io/v1alpha3", "kind":"VirtualService", "metadata":map[string]interface {}{"creationTimestamp":"2019-11-11T07:31:13Z", "generation":1, "name":"ratings", "namespace":"default", "uid":"0e9e1bbe-6060-4c0b-95af-42b9bf306c64"}, "spec":map[string]interface {}{"hosts":[]interface {}{"ratings"}, "http":[]interface {}{map[string]interface {}{"fault":map[string]interface {}{"delay":map[string]interface {}{"fixedDelay":"7s", "percentage":map[string]interface {}{"value":100}}}, "match":[]interface {}{map[string]interface {}{"headers":map[string]interface {}{"end-user":map[string]interface {}{"exact":"jason"}}}}, "route":[]interface {}{map[string]interface {}{"destination":map[string]interface {}{"host":"ratings", "subset":"v1"}}}}, map[string]interface {}{"route":[]interface {}{map[string]interface {}{"destination":map[string]interface {}{"host":"ratings", "subset":"v1"}}}}}}}: validation failure list: spec.http.route.weight in body is required [root@k8s-master istio-1.3.1]# vim samples/bookinfo/networking/virtual-service-ratings-test-delay.yaml [root@k8s-master istio-1.3.1]# kubectl apply -f samples/bookinfo/networking/virtual-service-ratings-test-delay.yaml virtualservice.networking.istio.io/ratings created [root@k8s-master istio-1.3.1]# cat samples/bookinfo/networking/virtual-service-ratings-test-delay.yaml apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: ratings spec: hosts: - ratings http: - match: - headers: end-user: exact: jason fault: delay: percentage: value: 100.0 fixedDelay: 7s route: - destination: host: ratings subset: v1 weight: 1 - route: - destination: host: ratings subset: v1 weight: 1
创建完成后,前往 Bookinfo 应用,登录 jason,打开浏览器的Network
,刷新页面,发现请求加载很慢,大约 6s 后,出现如下界面:
看到 productpage 的请求大约耗时 6s,Reviews显示 unavailable 的错误。因为此时 reviews 请求 ratings 的访问超过了 6s 还没有响应,使得 productpage 中的硬编码的超时设置生效了。
当然有的时候我们也能成功访问到 reviews-v1 版本,因为此时并没有进一步访问 ratings 服务,所以一切都是正常的,会显示不带星的界面:
通过这种超时故障注入,可以帮助我们方便地发现服务间相互访问中存在的潜在问题。
原文:https://www.cnblogs.com/Python-K8S/p/13986050.html