Istio 熔断

1、介绍

断路器是 Istio 提供的一个有用的技术。在微服务设置断路器可以有效保障微服务的抗操性。比如在断路器中,你可以设置服务并发连接次数限制或者服务调用失败次数,一旦达到设置的阈值,断路器将“跳闸”并停止与该主机的进一步连接。

因为断路器适用于负载平衡池中“真正”的服务网格(Istio 资源注入后的 Pod),因此可以在 Destination Rule 配置断路器,而 Destination Rule 最终会被翻译成 Envoy 的配置,然后作用在 Envoy 上。这样当流量进入到被 Istio 注入后的 Pod,流量就会被 Envoy 拦截,这样断路器就会针对流量发生作用。

2、Circuit breakers实例

2.1 部署deploy资源

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
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
selector:
server: nginx
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
server: nginx
spec:
replicas: 1
selector:
matchLabels:
server: nginx
template:
metadata:
labels:
server: nginx
spec:
containers:
- name: nginx
image: nginx:1.14-alpine
imagePullPolicy: IfNotPresent

2.2 部署Dr资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: test-dr
spec:
host: nginx-svc
trafficPolicy:
connectionPool:
http:
http1MaxPendingRequests: 1
maxRequestsPerConnection: 1
outlierDetection:
consecutiveErrors: 1
intercal: 10s
baseEjectionTime: 10s
masEjectionPercent: 100

​ 详细说明如下:

1
2
3
4
5
6
http1MaxPendingRequests: http 请求挂起状态的最大请求数
maxRequestsPerConnection: 一定时间内限制对后端服务发起的最大请求数,如果超过了此配置,就会出现限流。
outlierDetection.consecutiveErrors: 拒绝连接的最大失败次数
outlierDetection.interval: 触发熔断的时间间隔,在 interval 时间间隔内,达到 consecutiveErrors 即触发熔断
outlierDetection.baseEjectionTime: 熔断时长
maxEjectionPercent: 熔断连接最大百分比

部署以上资源,并将deploy资源进行istio注入

1
istioctl kube-inject -f test-deploy.yaml |kubectl apply -f -

3、验证断路器

1
fortio load -c 10 -n 50 -qps 0 http://10.106.31.6

image-20200601152441657

执行结果如下

image-20200601152656744

由上图可知有34个报错,说明断路器生效