一、理解超时与重试
超时的原理是,如果程序请求长时间无法返回结果,则需要设置超时机制,超过设置的时间则返回错误的信息;这样做既可以节约等待时消耗的资源,也可以避免由于级联错误引起的一系列问题。设置超时的方式也有很多种,比如通过修改代码在应用程序侧设置请求超时时间,但是这样非常的不灵活,也很容易出现遗漏的现象,而Istio则可以在基础设施层解决这一问题。
重试的原理是,在网络环境不稳定的情况下,会出现暂时的网络不可达的现象,这时就需要重试机制,通过多次的尝试来获取正确的返回信息。
二、配置超时
1)设置默认的目标规则
在目标规则中定义可用的版本
➜ kubectl apply -f ../../samples/bookinfo/networking/destination-rule-all.yaml
destinationrule.networking.istio.io/productpage created
destinationrule.networking.istio.io/reviews created
destinationrule.networking.istio.io/ratings created
destinationrule.networking.istio.io/details created
2)初始化版本路由
➜ kubectl apply -f ../../samples/bookinfo/networking/virtual-service-all-v1.yaml
virtualservice.networking.istio.io/productpage created
virtualservice.networking.istio.io/reviews created
virtualservice.networking.istio.io/ratings created
virtualservice.networking.istio.io/details created
3)设置虚拟服务
通过配置路由规则(HTTPRoute)的timeout字段来指定http请求的超时时间(默认情况下,超时是被禁用的);配置reviews服务,将请求路由到v2的版本上,使其可以发起对ratings服务的调用。
➜ kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v2
EOF
virtualservice.networking.istio.io/reviews configured
➜ kubectl describe vs reviews
Name: reviews
Namespace: default
Labels: <none>
Annotations: <none>
API Version: networking.istio.io/v1beta1
Kind: VirtualService
Metadata:
Creation Timestamp: 2021-07-24T09:23:41Z
Generation: 2
Managed Fields:
API Version: networking.istio.io/v1alpha3
Fields Type: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
.:
f:kubectl.kubernetes.io/last-applied-configuration:
f:spec:
.:
f:hosts:
f:http:
Manager: kubectl-client-side-apply
Operation: Update
Time: 2021-07-24T09:23:41Z
Resource Version: 645195
Self Link: /apis/networking.istio.io/v1beta1/namespaces/default/virtualservices/reviews
UID: 65fea820-0b25-4616-ae0e-c628bef8dc13
Spec:
Hosts:
reviews
Http:
Route:
Destination:
Host: reviews
Subset: v2
Events: <none>
4)给ratings服务的调用注入2秒延迟的故障
➜ kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: ratings
spec:
hosts:
- ratings
http:
- fault:
delay:
percent: 100
fixedDelay: 2s
route:
- destination:
host: ratings
subset: v1
EOF
virtualservice.networking.istio.io/ratings configured
➜ kubectl describe vs ratings
Name: ratings
Namespace: default
Labels: <none>
Annotations: <none>
API Version: networking.istio.io/v1beta1
Kind: VirtualService
Metadata:
Creation Timestamp: 2021-07-24T09:23:42Z
Generation: 2
Managed Fields:
API Version: networking.istio.io/v1alpha3
Fields Type: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
.:
f:kubectl.kubernetes.io/last-applied-configuration:
f:spec:
.:
f:hosts:
f:http:
Manager: kubectl-client-side-apply
Operation: Update
Time: 2021-07-24T09:23:42Z
Resource Version: 645606
Self Link: /apis/networking.istio.io/v1beta1/namespaces/default/virtualservices/ratings
UID: 5f2f821e-932c-40c2-95ae-c3ac6ef02dcf
Spec:
Hosts:
ratings
Http:
Fault:
Delay:
Fixed Delay: 2s
Percent: 100
Route:
Destination:
Host: ratings
Subset: v1
Events: <none>
5)请求服务查看延迟
➜ curl -o /dev/null -s -w "time_starttransfer:%{time_starttransfer}\ntime_total:%{time_total}\n" http://localhost/productpage
time_starttransfer:2.094652
time_total:2.094805
6)给reviews服务调用增加一个0.5秒的请求超时
➜ kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v2
timeout: 0.5s
EOF
virtualservice.networking.istio.io