Istio 超时处理 (TrafficManagement - Timeout)
超时是为了解决长时间或者无限期的等待造成服务不可用,通常需要在代码中植入此类的网络层面的弹性处理逻辑,但通过 Istio 就可以使用 Vitual Sevice来优雅的实现超时的处理。
什么场景需要用到超时处理
在生产环境中经常会碰到由于调用方等待下游的响应过长,堆积大量的请求阻塞了自身服务,造成雪崩的情况,通过通过超时处理来避免由于无限期等待造成的故障,进而增强服务的可用性。
通过例子来理解
nginx 服务设置了超时时间为3秒,如果超出这个时间就不在等待,返回超时错误
httpd 服务设置了响应时间延迟5秒,任何请求都需要等待5秒后才能返回
client 通过访问 nginx 服务去反向代理 httpd 服务,由于 httpd 服务需要5秒后才能返回,但nginx 服务只等待3秒,所以客户端会提示超时错误
apiVersion: apps/v1
kind: Deployment
metadata:
labels: app: nginx-deployment
name: nginx-deployment
spec:
replicas: 1
selector: matchLabels: app: nginx-deployment
strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate
template: metadata: labels: app: nginx-deployment spec: containers: - image: 'nginx:latest' name: nginx-deployment
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels: app: httpd-deployment
name: httpd-deployment
spec:
replicas: 1
selector: matchLabels: app: httpd-deployment
strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate
template: metadata: labels: app: httpd-deployment spec: containers: - image: 'httpd:latest' name: httpd-deployment
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector: app: nginx-deployment
type: ClusterIP
ports:
- name: http port: 80 targetPort: 80 protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: httpd-service
spec:
selector: app: httpd-deployment
type: ClusterIP
ports:
- name: http port: 80 targetPort: 80 protocol: TCP
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: nginx-vs
spec:
hosts:
- nginx-service
http:
- route: - destination: host: nginx-service timeout: 3s
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpd-vs
spec:
hosts:
- httpd-service
http:
- fault: delay: percentage: value: 100 fixedDelay: 5s route: - destination: host: httpd-service
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels: app: client-deployment
name: client-deployment
spec:
replicas: 1
selector: matchLabels: app: client-deployment
strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate
template: metadata: labels: app: client-deployment spec: containers: - image: 'busybox:latest' name: client-deployment command: [ "/bin/sh", "-c", "sleep 3600"]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
配置 nginx 反向代理 模拟下游调用
kubectl exec -it nginx-deployment-56c94b9957-xgw88 -- sh
tee /etc/nginx/conf.d/default.conf <<-'EOF'
server { listen 80; server_name localhost; location / { proxy_pass http://httpd-service; proxy_http_version 1.1; }
}
EOF
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
执行 nginx -t ; nginx -s reload
重启服务以生效配置
进入客户端容器测试结果
kubectl exec -it client-deployment-56c94b9957-xgw88 -- sh
while true; do wget -q -O - http://nginx-service; done
每隔3秒,由于 nginx 服务的超时时间到了而 httpd 未有响应,则提示返回超时错误。
文章来源: blog.csdn.net,作者:叶康铭,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/m0_38030719/article/details/108940200
- 点赞
- 收藏
- 关注作者
评论(0)