07-断路器

Catalogue
  1. 1. 配置断路器
  2. 2. 部署测试客户端fortio
  3. 3. 测试
  4. 4. 清除测试环境
  5. 5. 参考资料

该任务演示如何配置断路器,对于连接、请求、离群值检测。

断路器是创建弹性微服务应用程序的重要模式。断路器使您可以编写的应用程序,以限制故障,延迟尖峰和网络特性的其他不良影响的影响。

配置断路器

  • httpbin.yaml
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
apiVersion: v1
kind: Service
metadata:
name: httpbin
labels:
app: httpbin
spec:
ports:
- name: http
port: 8000
selector:
app: httpbin
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: httpbin
spec:
replicas: 1
template:
metadata:
labels:
app: httpbin
version: v1
spec:
containers:
- image: docker.io/citizenstig/httpbin
imagePullPolicy: IfNotPresent
name: httpbin
ports:
- containerPort: 8000
  • httpbin-destination-rule.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: httpbin
spec:
host: httpbin
trafficPolicy:
connectionPool:
tcp:
maxConnections: 1
http:
http1MaxPendingRequests: 1
maxRequestsPerConnection: 1
outlierDetection:
consecutiveErrors: 1
interval: 1s
baseEjectionTime: 3m
maxEjectionPercent: 100

部署测试客户端fortio

创建一个客户端发送流量到httpbin服务,该客户端是一个简单的负载测试客户端,称为fortio。Fortio使您可以控制连接数,并发性和传出HTTP调用的延迟。您将使用此客户端来“跳闸”在DestinationRule中设置的断路器策略。

  1. 部署fortio
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: fortio-deploy
spec:
replicas: 1
template:
metadata:
labels:
app: fortio
spec:
containers:
- name: fortio
image: istio/fortio:latest_release
imagePullPolicy: Always
ports:
- containerPort: 8080
name: http-fortio
- containerPort: 8079
name: grpc-ping
  1. 登录进fortio去通过curl调用httpbin,可以看到成功响应
1
2
$ FORTIO_POD=$(kubectl get pod | grep fortio | awk '{ print $1 }')
$ kubectl exec -it $FORTIO_POD -c fortio /usr/local/bin/fortio -- load -curl http://httpbin:8000/get

测试

在上面的DestinationRule设置中,指定了maxConnections:1和http1MaxPendingRequests:1.这些规则表明,如果您超过一个以上的连接并发请求,则在istio-proxy打开断路器,之后进行进一步的请求和连接时,您应该会看到一些故障。

  1. 通过两个并发连接(-c 2)调用服务,并发送20个请求(-n 20):
1
2
3
4
5
6
7
$ kubectl exec -it $FORTIO_POD  -c fortio /usr/local/bin/fortio -- load -c 2 -qps 0 -n 20 -loglevel Warning http://httpbin:8000/get

响应:
。。。。。
Code 200 : 13 (65.0 %)
Code 503 : 7 (35.0 %)
。。。。。。

从响应数据看到大部分请求通过了。

  1. 使并发连接数达到3
1
2
3
4
5
6
7
$ kubectl exec -it $FORTIO_POD  -c fortio /usr/local/bin/fortio -- load -c 3 -qps 0 -n 20 -loglevel Warning http://httpbin:8000/get

响应:
。。。。。。
Code 200 : 8 (40.0 %)
Code 503 : 12 (60.0 %)
。。。。。。

现在,您开始看到预期的断路器行为,大部分请求被拦截了

  1. 查询istio-proxy统计信息以查看更多信息
1
2
3
4
5
6
$ kubectl exec -it $FORTIO_POD  -c istio-proxy  -- sh -c 'curl localhost:15000/stats' | grep httpbin | grep pending

cluster.outbound|8000||httpbin.default.svc.cluster.local.upstream_rq_pending_active: 0
cluster.outbound|8000||httpbin.default.svc.cluster.local.upstream_rq_pending_failure_eject: 0
cluster.outbound|8000||httpbin.default.svc.cluster.local.upstream_rq_pending_overflow: 19
cluster.outbound|8000||httpbin.default.svc.cluster.local.upstream_rq_pending_total: 22

您可以看到19为upstream_rq_pending_overflow值,这意味着到目前为止已标记19个调用为断路。

清除测试环境

1
2
3
$ kubectl delete destinationrule httpbin
$ kubectl delete deploy httpbin fortio-deploy
$ kubectl delete svc httpbin

参考资料