十三 Istio:Istio项目实际案例——Online Boutique( 八 )

如果我们刷新几次产品页面,我们应该得到如下图所示的错误信息 。

十三 Istio:Istio项目实际案例——Online Boutique

文章插图
请注意,错误信息说,失败的原因是故障过滤器中止 。如果我们打开 Grafana(getmesh istioctl dash grafana),我们也会注意到图中报告的错误 。
删除productcatalogservice这个VirtualService:
[root@k8scloude1 ~]# kubectl delete virtualservice productcatalogservicevirtualservice.networking.istio.io "productcatalogservice" deleted[root@k8scloude1 ~]# kubectl get virtualserviceNAMEGATEWAYSHOSTSAGEfrontend["frontend.online-boutique.svc.cluster.local"]6d14hfrontend-ingress["frontend-gateway"]["*"]23hrecommendationservice["recommendationservice.online-boutique.svc.cluster.local"]44m九.弹性9.1 弹性为了演示弹性功能,我们将在产品目录服务部署中添加一个名为 EXTRA_LATENCY 的环境变量 。这个变量会在每次调用服务时注入一个额外的休眠 。
通过运行 kubectl edit deploy productcatalogservice 来编辑产品目录服务部署 。
[root@k8scloude1 ~]# kubectl get deployNAMEREADYUP-TO-DATEAVAILABLEAGEadservice1/1116d14hcartservice1/1116d14hcheckoutservice1/1116d14hcurrencyservice1/1116d14hemailservice1/1116d14hfrontend1/11124hfrontend-v11/11128hloadgenerator1/1116d14hpaymentservice1/1116d14hproductcatalogservice1/1116d14hrecommendationservice1/1116d14hredis-cart1/1116d14hshippingservice1/1116d14h[root@k8scloude1 ~]# kubectl edit deploy productcatalogservicedeployment.apps/productcatalogservice edited这将打开一个编辑器 。滚动到有环境变量的部分,添加 EXTRA_LATENCY 环境变量 。
...spec:containers:- env:- name: EXTRA_LATENCYvalue: 6s ...
十三 Istio:Istio项目实际案例——Online Boutique

文章插图
保存并推出编辑器 。
如果我们刷新http://192.168.110.190/页面,我们会发现页面需要 6 秒的时间来加载(那是由于我们注入的延迟) 。
让我们给产品目录服务添加一个 2 秒的超时 。
[root@k8scloude1 ~]# vim productcatalogservice-timeout.yaml[root@k8scloude1 ~]# cat productcatalogservice-timeout.yamlapiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:name: productcatalogservicespec:hosts:- productcatalogservice.online-boutique.svc.cluster.localhttp:- route:- destination:host: productcatalogservice.online-boutique.svc.cluster.localtimeout: 2s创建 VirtualService 。
[root@k8scloude1 ~]# kubectl apply -f productcatalogservice-timeout.yamlvirtualservice.networking.istio.io/productcatalogservice created[root@k8scloude1 ~]# kubectl get virtualserviceNAMEGATEWAYSHOSTSAGEfrontend["frontend.online-boutique.svc.cluster.local"]6d14hfrontend-ingress["frontend-gateway"]["*"]24hproductcatalogservice["productcatalogservice.online-boutique.svc.cluster.local"]10srecommendationservice["recommendationservice.online-boutique.svc.cluster.local"]76m如果我们刷新页面http://192.168.110.190/,我们会注意到一个错误信息的出现:
十三 Istio:Istio项目实际案例——Online Boutique

文章插图
rpc error: code = Unavailable desc = upstream request timeout could not retrieve products该错误表明对产品目录服务的请求超时了 。原因为:我们修改了服务,增加了 6 秒的延迟,并将超时设置为 2 秒 。
让我们定义一个重试策略,有三次尝试,每次尝试的超时为 1 秒 。
[root@k8scloude1 ~]# vim productcatalogservice-retry.yaml[root@k8scloude1 ~]# cat productcatalogservice-retry.yamlapiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:name: productcatalogservicespec:hosts:- productcatalogservice.online-boutique.svc.cluster.localhttp:- route:- destination:host: productcatalogservice.online-boutique.svc.cluster.localretries:attempts: 3perTryTimeout: 1s[root@k8scloude1 ~]# kubectl apply -f productcatalogservice-retry.yamlvirtualservice.networking.istio.io/productcatalogservice configured[root@k8scloude1 ~]# kubectl get virtualserviceNAMEGATEWAYSHOSTSAGEfrontend["frontend.online-boutique.svc.cluster.local"]6d14hfrontend-ingress["frontend-gateway"]["*"]24hproductcatalogservice["productcatalogservice.online-boutique.svc.cluster.local"]10mrecommendationservice["recommendationservice.online-boutique.svc.cluster.local"]86m

经验总结扩展阅读