Istio熔断机制

1、简介

熔断(Circuit Breaker),原是指当电流超过规定值时断开电路,进行短路保护或严重过载保护的机制 。对于微服务系统而言,熔断尤为重要,它可以使系统在遭遇某些模块故障时,通过服务降级等方式来提高系统核心功能的可用性,得以应对来自故障、潜在峰值或其他未知网络因素的影响。它通过限制对某个服务的请求量,从而避免在下游服务不可用或过载时,导致整个系统的崩溃。

2、特点

限制并发请求数量:Istio 可以设置一个服务实例同时处理的最大请求数。当请求数超过这个限制时,新的请求将被立即拒绝。这有助于防止下游服务在高并发场景下过载。

超时设置:Istio 支持对每个请求设置超时时间。如果请求在规定时间内没有得到响应,将自动中断。这有助于避免长时间等待一个可能已经失败的请求,提升系统的响应速度。

错误率监控:Istio 可以监控一段时间内的错误率。当错误率超过设定阈值时,熔断器将会打开,暂时拒绝所有请求,直到错误率恢复到正常水平。这有助于在服务出现故障时,快速隔离问题,防止影响扩大。

故障恢复:在熔断器打开期间,Istio 会定期尝试发送少量请求,以检测下游服务是否恢复正常。如果检测到服务恢复,熔断器将会关闭,恢复正常的请求处理。这有助于在故障解除后,迅速恢复服务。

3、部署示例应用

Istio 是通过 Envoy Proxy 来实现熔断机制的,Envoy 强制在网络层面配置熔断策略,这样就不必为每个应用程序单独配置或重新编程。下面就通过一个示例来演示如何为 Istio 网格中的服务配置熔断的连接数、请求数和异常检测。

图片

我们已经为 httpbin 服务设置了熔断策略,接下来创建一个 Java 客户端,用来向后端服务发送请求,观察是否会触发熔断策略。这个客户端可以控制连接数量、并发数、待处理请求队列,使用这一客户端,能够有效的触发前面在目标规则中设置的熔断策略。该客户端的 deployment yaml 内容如下:

[root@mast01 istio]# vim httpbin-client-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpbin-client-v1
namespace: bookinfo
spec:
replicas: 1
selector:
matchLabels:
app: httpbin-client-v1
version: v1
template:
metadata:
labels:
app: httpbin-client-v1
version: v1
spec:
containers:
– image: ceposta/http-envoy-client-standalone:latest
imagePullPolicy: IfNotPresent
name: httpbin-client
command: [“/bin/sleep”,”infinity”]

[root@mast01 istio]# kubectl apply -f httpbin-client-deploy.yaml
deployment.apps/httpbin-client-v1 created
注入 Istio

[root@mast01 istio]# kubectl apply -f <(istioctl kube-inject -f httpbin-client-deploy.yaml)
deployment.apps/httpbin-client-v1 configured
4、验证

尝试通过单线程(NUM_THREADS=1)创建一个连接,并进行 5 次调用(默认值:NUM_CALLS_PER_CLIENT=5)

[root@mast01 istio]# CLIENT_POD=$(kubectl get pod -n bookinfo | grep httpbin-client | awk ‘{ print $1 }’)

[root@mast01 istio]# kubectl -n bookinfo exec -it $CLIENT_POD -c httpbin-client — sh -c ‘export URL_UNDER_TEST=http://httpbin:8000/get export NUM_THREADS=1 && java -jar http-client.jar’
using num threads: 1
Starting pool-1-thread-1 with numCalls=5 parallelSends=false delayBetweenCalls=0 url=http://httpbin:8000/get mixedRespTimes=false
pool-1-thread-1: successes=[5], failures=[0], duration=[216ms] #表示线程1进行了5次请求,其中5次成功,0次失败,总耗时216毫秒。
尝试把线程数提高到 2

[root@mast01 istio]# kubectl -n bookinfo exec -it $CLIENT_POD -c httpbin-client — sh -c ‘export URL_UNDER_TEST=http://httpbin:8000/get export NUM_THREADS=2 && java -jar http-client.jar’
using num threads: 2
Starting pool-1-thread-1 with numCalls=5 parallelSends=false delayBetweenCalls=0 url=http://httpbin:8000/get mixedRespTimes=false
Starting pool-1-thread-2 with numCalls=5 parallelSends=false delayBetweenCalls=0 url=http://httpbin:8000/get mixedRespTimes=false
pool-1-thread-2: successes=[5], failures=[0], duration=[114ms] #表示线程2进行了5次请求,其中5次成功,0次失败,总耗时114毫秒。
pool-1-thread-1: successes=[5], failures=[0], duration=[158ms] #表示线程1进行了5次请求,其中5次成功,0次失败,总耗时158毫秒。
5、创建DestinationRule, 设置熔断策略

maxConnections : 限制对后端服务发起的 HTTP/1.1 连接数,如果超过了这个限制,就会开启熔断。

maxPendingRequests : 限制待处理请求列表的长度, 如果超过了这个限制,就会开启熔断。

maxRequestsPerConnection : 在任何给定时间内限制对后端服务发起的 HTTP/2 请求数,如果超过了这个限制,就会开启熔断。

[root@mast01 istio]# vim DestinationRule.yaml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: httpbin
namespace: bookinfo
spec:
host: httpbin
trafficPolicy:
connectionPool:
tcp:
maxConnections: 1
http:
http1MaxPendingRequests: 1
maxRequestsPerConnection: 1

[root@mast01 istio]# kubectl apply -f DestinationRule.yaml
destinationrule.networking.istio.io/httpbin configured
查看熔断策略在envoy的配置

[root@mast01 istio]# istioctl pc cluster httpbin-client-v1-6b6cfb9bfc-bvmc7.bookinfo –fqdn httpbin.bookinfo.svc.cluster.local -ojson

“connectTimeout”: “10s”,
“maxRequestsPerConnection”: 1,
“circuitBreakers”: {
“thresholds”: [
{
“maxConnections”: 1,
“maxPendingRequests”: 1,
“maxRequests”: 4294967295,
“maxRetries”: 4294967295
}
]
},

6、再次验证熔断

[root@mast01 istio]# kubectl -n bookinfo exec -it $CLIENT_POD -c httpbin-client — sh -c ‘export URL_UNDER_TEST=http://httpbin:8000/get export NUM_THREADS=2 && java -jar http-client.jar’
using num threads: 2
Starting pool-1-thread-2 with numCalls=5 parallelSends=false delayBetweenCalls=0 url=http://httpbin:8000/get mixedRespTimes=false
Starting pool-1-thread-1 with numCalls=5 parallelSends=false delayBetweenCalls=0 url=http://httpbin:8000/get mixedRespTimes=false
pool-1-thread-1: successes=[2], failures=[3], duration=[95ms] #表示线程1进行了5次请求,其中2次成功,3次失败,总耗时95毫秒。
pool-1-thread-2: successes=[4], failures=[1], duration=[144ms] #表示线程2进行了5次请求,其中4次成功,1次失败,总耗时144毫秒。

声明:文中观点不代表本站立场。本文传送门:http://eyangzhen.com/418788.html

(0)
联系我们
联系我们
分享本页
返回顶部