istio-断路器示例

此任务阐述如何为连接,请求和异常检测(outlier detection)配置断路器。断路器是创建弹性微服务应用程序的重要模式。断路器使应用程序可以适应网络故障和延迟尖峰等网络不良影响。在此任务中,将配置断路规则,然后通过有意地“跳闸”来测试断路器配置。

1    环境准备

通过执行下面的命令,启动httpbin示例:

$ kubectl apply -f samples/httpbin/httpbin.yaml

httpbin.yaml的代码内容如下:

apiVersion: v1
kind: Service
metadata:
  name: httpbin
  labels:
    app: httpbin
spec:
  ports:
  - name: http
    port: 8000
    targetPort: 80
  selector:
    app: httpbin
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpbin
spec:
  replicas: 1
  selector:
    matchLabels:
      app: httpbin
      version: v1
  template:
    metadata:
      labels:
        app: httpbin
        version: v1
    spec:
      containers:
      - image: docker.io/kennethreitz/httpbin
        imagePullPolicy: IfNotPresent
        name: httpbin
        ports:
        - containerPort: 80

该httpbin应用程序充当此任务的后端服务。

2    配置断路器

创建一个目标规则,以在调用httpbin服务时应用断路器设置:

$ kubectl apply -f - <<EOF
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
EOF

3    添加客户端

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

  • 通过执行下面的命令部署fortio客户端:
$ kubectl apply -f  samples/httpbin/sample-client/fortio-deploy.yaml
  • 通过kubectl执行下面的命令,使用fortio客户端工具调用httpbin:
$ kubectl exec -it $FORTIO_POD  -c fortio /usr/bin/fortio -- load -curl  http://httpbin:8000/get

调用的结果如下:

HTTP/1.1 200 OK

server: envoy

date: Tue, 16 Jan 2018 23:47:00 GMT

content-type: application/json

access-control-allow-origin: *

access-control-allow-credentials: true

content-length: 445

x-envoy-upstream-service-time: 36

{

“args”: {},

“headers”: {

“Content-Length”: “0”,

“Host”: “httpbin:8000”,

“User-Agent”: “istio/fortio-0.6.2”,

“X-B3-Sampled”: “1”,

“X-B3-Spanid”: “824fbd828d809bf4”,

“X-B3-Traceid”: “824fbd828d809bf4”,

“X-Ot-Span-Context”: “824fbd828d809bf4;824fbd828d809bf4;0000000000000000”,

“X-Request-Id”: “1ad2de20-806e-9622-949a-bd1d9735a3f4”

},

“origin”: “127.0.0.1”,

“url”: “http://httpbin:8000/get”

}

4    触发断路器

在DestinationRule设置中,指定了maxConnections: 1和 http1MaxPendingRequests: 1。这些规则表明,如果超过一个以上的连接并发请求,则istio-proxy在为进一步的请求和连接打开路由时,应该会看到下面的情况 。

  • 以两个并发连接(-c 2)和发送20个请求(-n 20)调用服务:
$ kubectl exec -it $FORTIO_POD  -c fortio /usr/bin/fortio -- load -c 2 -qps 0 -n 20 -loglevel Warning http://httpbin:8000/get

调用的结果如下:

Fortio 0.6.2 running at 0 queries per second, 2->2 procs, for 5s: http://httpbin:8000/get

Starting at max qps with 2 thread(s) [gomax 2] for exactly 20 calls (10 per thread + 0)

23:51:10 W http.go:617> Parsed non ok code 503 (HTTP/1.1 503)

Ended after 106.474079ms : 20 calls. qps=187.84

Aggregated Function Time : count 20 avg 0.010215375 +/- 0.003604 min 0.005172024 max 0.019434859 sum 0.204307492

# range, mid point, percentile, count

>= 0.00517202 <= 0.006 , 0.00558601 , 5.00, 1

> 0.006 <= 0.007 , 0.0065 , 20.00, 3

> 0.007 <= 0.008 , 0.0075 , 30.00, 2

> 0.008 <= 0.009 , 0.0085 , 40.00, 2

> 0.009 <= 0.01 , 0.0095 , 60.00, 4

> 0.01 <= 0.011 , 0.0105 , 70.00, 2

> 0.011 <= 0.012 , 0.0115 , 75.00, 1

> 0.012 <= 0.014 , 0.013 , 90.00, 3

> 0.016 <= 0.018 , 0.017 , 95.00, 1

> 0.018 <= 0.0194349 , 0.0187174 , 100.00, 1

# target 50% 0.0095

# target 75% 0.012

# target 99% 0.0191479

# target 99.9% 0.0194062

Code 200 : 19 (95.0 %)

Code 503 : 1 (5.0 %)

Response Header Sizes : count 20 avg 218.85 +/- 50.21 min 0 max 231 sum 4377

Response Body/Total Sizes : count 20 avg 652.45 +/- 99.9 min 217 max 676 sum 13049

All done 20 calls (plus 0 warmup) 10.215 ms avg, 187.8 qps

有趣的是,几乎所有请求都通过了!可以看出istio-proxy 确实允许一些余地。

Code 200 : 19 (95.0 %)

Code 503 : 1 (5.0 %)

  • 以三个并发连接(-c 3)和发送30个请求(-n 20)调用服务:
$ kubectl exec -it $FORTIO_POD  -c fortio /usr/bin/fortio -- load -c 3 -qps 0 -n 30 -loglevel Warning http://httpbin:8000/get

调用的结果如下:

Fortio 0.6.2 running at 0 queries per second, 2->2 procs, for 5s: http://httpbin:8000/get

Starting at max qps with 3 thread(s) [gomax 2] for exactly 30 calls (10 per thread + 0)

23:51:51 W http.go:617> Parsed non ok code 503 (HTTP/1.1 503)

23:51:51 W http.go:617> Parsed non ok code 503 (HTTP/1.1 503)

23:51:51 W http.go:617> Parsed non ok code 503 (HTTP/1.1 503)

23:51:51 W http.go:617> Parsed non ok code 503 (HTTP/1.1 503)

23:51:51 W http.go:617> Parsed non ok code 503 (HTTP/1.1 503)

23:51:51 W http.go:617> Parsed non ok code 503 (HTTP/1.1 503)

23:51:51 W http.go:617> Parsed non ok code 503 (HTTP/1.1 503)

23:51:51 W http.go:617> Parsed non ok code 503 (HTTP/1.1 503)

23:51:51 W http.go:617> Parsed non ok code 503 (HTTP/1.1 503)

23:51:51 W http.go:617> Parsed non ok code 503 (HTTP/1.1 503)

23:51:51 W http.go:617> Parsed non ok code 503 (HTTP/1.1 503)

Ended after 71.05365ms : 30 calls. qps=422.22

Aggregated Function Time : count 30 avg 0.0053360199 +/- 0.004219 min 0.000487853 max 0.018906468 sum 0.160080597

# range, mid point, percentile, count

>= 0.000487853 <= 0.001 , 0.000743926 , 10.00, 3

> 0.001 <= 0.002 , 0.0015 , 30.00, 6

> 0.002 <= 0.003 , 0.0025 , 33.33, 1

> 0.003 <= 0.004 , 0.0035 , 40.00, 2

> 0.004 <= 0.005 , 0.0045 , 46.67, 2

> 0.005 <= 0.006 , 0.0055 , 60.00, 4

> 0.006 <= 0.007 , 0.0065 , 73.33, 4

> 0.007 <= 0.008 , 0.0075 , 80.00, 2

> 0.008 <= 0.009 , 0.0085 , 86.67, 2

> 0.009 <= 0.01 , 0.0095 , 93.33, 2

> 0.014 <= 0.016 , 0.015 , 96.67, 1

> 0.018 <= 0.0189065 , 0.0184532 , 100.00, 1

# target 50% 0.00525

# target 75% 0.00725

# target 99% 0.0186345

# target 99.9% 0.0188793

Code 200 : 19 (63.3 %)

Code 503 : 11 (36.7 %)

Response Header Sizes : count 30 avg 145.73333 +/- 110.9 min 0 max 231 sum 4372

Response Body/Total Sizes : count 30 avg 507.13333 +/- 220.8 min 217 max 676 sum 15214

All done 30 calls (plus 0 warmup) 5.336 ms avg, 422.2 qps

现在,开始看到预期的断路行为了。只有请求的63.3%成功,其余被断开了:

Code 200 : 19 (63.3 %)

Code 503 : 11 (36.7 %)

作者简介:季向远,北京神舟航天软件技术有限公司。本文版权归原作者所有。

K8S中文社区微信公众号

评论 抢沙发

登录后评论

立即登录