熔断

    熔断,是创建弹性微服务应用程序的重要模式。熔断能够使您的应用程序具备应对来自故障、潜在峰值和其他未知网络因素影响的能力。

    这个任务中,你将配置熔断规则,然后通过有意的使熔断器“跳闸”来测试配置。

    • 启动 样例程序。

    如果您启用了 sidecar 自动注入,通过以下命令部署 服务:

    否则,您必须在部署 httpbin 应用程序前进行手动注入,部署命令如下:

    Zip

    1. $ kubectl apply -f <(istioctl kube-inject -f @samples/httpbin/httpbin.yaml@)

    应用程序 httpbin 作为此任务的后端服务。

    配置熔断器

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

    如果您的 Istio 启用了双向 TLS 身份验证,则必须在应用目标规则之前将 TLS 流量策略 mode:ISTIO_MUTUAL 添加到 DestinationRule 。否则请求将产生 503 错误,如所述。

    1. $ kubectl apply -f - <<EOF
    2. apiVersion: networking.istio.io/v1alpha3
    3. kind: DestinationRule
    4. metadata:
    5. name: httpbin
    6. spec:
    7. host: httpbin
    8. trafficPolicy:
    9. connectionPool:
    10. tcp:
    11. maxConnections: 1
    12. http:
    13. http1MaxPendingRequests: 1
    14. maxRequestsPerConnection: 1
    15. outlierDetection:
    16. consecutiveErrors: 1
    17. interval: 1s
    18. baseEjectionTime: 3m
    19. maxEjectionPercent: 100
    20. EOF
    • 验证目标规则是否已正确创建:
    1. $ kubectl get destinationrule httpbin -o yaml
    2. apiVersion: networking.istio.io/v1alpha3
    3. kind: DestinationRule
    4. metadata:
    5. name: httpbin
    6. ...
    7. spec:
    8. host: httpbin
    9. connectionPool:
    10. http:
    11. http1MaxPendingRequests: 1
    12. maxRequestsPerConnection: 1
    13. tcp:
    14. outlierDetection:
    15. baseEjectionTime: 180.000s
    16. consecutiveErrors: 1
    17. interval: 1.000s
    18. maxEjectionPercent: 100
    • 向客户端注入 Istio Sidecar 代理,以便 Istio 对其网络交互进行管理:

    Zip

    • 登入客户端 Pod 并使用 Fortio 工具调用 httpbin 服务。-curl 参数表明发送一次调用:
    1. $ FORTIO_POD=$(kubectl get pod | grep fortio | awk '{ print $1 }')
    2. $ kubectl exec -it $FORTIO_POD -c fortio /usr/bin/fortio -- load -curl http://httpbin:8000/get
    3. HTTP/1.1 200 OK
    4. server: envoy
    5. date: Tue, 16 Jan 2018 23:47:00 GMT
    6. content-type: application/json
    7. access-control-allow-origin: *
    8. access-control-allow-credentials: true
    9. content-length: 445
    10. x-envoy-upstream-service-time: 36
    11. {
    12. "args": {},
    13. "headers": {
    14. "Content-Length": "0",
    15. "Host": "httpbin:8000",
    16. "User-Agent": "istio/fortio-0.6.2",
    17. "X-B3-Sampled": "1",
    18. "X-B3-Spanid": "824fbd828d809bf4",
    19. "X-B3-Traceid": "824fbd828d809bf4",
    20. "X-Ot-Span-Context": "824fbd828d809bf4;824fbd828d809bf4;0000000000000000",
    21. "X-Request-Id": "1ad2de20-806e-9622-949a-bd1d9735a3f4"
    22. },
    23. "origin": "127.0.0.1",
    24. "url": "http://httpbin:8000/get"
    25. }

    可以看到调用后端服务的请求已经成功!接下来,可以测试熔断。

    触发熔断器

    DestinationRule 配置中,您定义了 maxConnections: 1http1MaxPendingRequests: 1。这些规则意味着,如果并发的连接和请求数超过一个,在 istio-proxy 进行进一步的请求和连接时,后续请求或连接将被阻止。

    1. $ kubectl exec -it $FORTIO_POD -c fortio /usr/bin/fortio -- load -c 2 -qps 0 -n 20 -loglevel Warning http://httpbin:8000/get
    2. Fortio 0.6.2 running at 0 queries per second, 2->2 procs, for 5s: http://httpbin:8000/get
    3. Starting at max qps with 2 thread(s) [gomax 2] for exactly 20 calls (10 per thread + 0)
    4. 23:51:10 W http.go:617> Parsed non ok code 503 (HTTP/1.1 503)
    5. Ended after 106.474079ms : 20 calls. qps=187.84
    6. Aggregated Function Time : count 20 avg 0.010215375 +/- 0.003604 min 0.005172024 max 0.019434859 sum 0.204307492
    7. # range, mid point, percentile, count
    8. >= 0.00517202 <= 0.006 , 0.00558601 , 5.00, 1
    9. > 0.006 <= 0.007 , 0.0065 , 20.00, 3
    10. > 0.007 <= 0.008 , 0.0075 , 30.00, 2
    11. > 0.008 <= 0.009 , 0.0085 , 40.00, 2
    12. > 0.009 <= 0.01 , 0.0095 , 60.00, 4
    13. > 0.01 <= 0.011 , 0.0105 , 70.00, 2
    14. > 0.011 <= 0.012 , 0.0115 , 75.00, 1
    15. > 0.012 <= 0.014 , 0.013 , 90.00, 3
    16. > 0.016 <= 0.018 , 0.017 , 95.00, 1
    17. > 0.018 <= 0.0194349 , 0.0187174 , 100.00, 1
    18. # target 50% 0.0095
    19. # target 75% 0.012
    20. # target 99% 0.0191479
    21. # target 99.9% 0.0194062
    22. Code 200 : 19 (95.0 %)
    23. Code 503 : 1 (5.0 %)
    24. Response Header Sizes : count 20 avg 218.85 +/- 50.21 min 0 max 231 sum 4377
    25. Response Body/Total Sizes : count 20 avg 652.45 +/- 99.9 min 217 max 676 sum 13049
    26. All done 20 calls (plus 0 warmup) 10.215 ms avg, 187.8 qps

    有趣的是,几乎所有的请求都完成了!istio-proxy 确实允许存在一些误差。

    1. Code 200 : 19 (95.0 %)
    2. Code 503 : 1 (5.0 %)
    • 将并发连接数提高到 3 个:

    现在,您将开始看到预期的熔断行为,只有 63.3% 的请求成功,其余的均被熔断器拦截:

    1. Code 200 : 19 (63.3 %)
    2. Code 503 : 11 (36.7 %)
    • 查询 istio-proxy 状态以了解更多熔断详情:
    1. $ kubectl exec $FORTIO_POD -c istio-proxy -- pilot-agent request GET stats | grep httpbin | grep pending
    2. cluster.outbound|80||httpbin.springistio.svc.cluster.local.upstream_rq_pending_active: 0
    3. cluster.outbound|80||httpbin.springistio.svc.cluster.local.upstream_rq_pending_failure_eject: 0
    4. cluster.outbound|80||httpbin.springistio.svc.cluster.local.upstream_rq_pending_overflow: 12
    5. cluster.outbound|80||httpbin.springistio.svc.cluster.local.upstream_rq_pending_total: 39

    可以看到 upstream_rq_pending_overflow12,这意味着,目前为止已有 12 个调用被标记为熔断。

    • 清理规则:
    1. $ kubectl delete destinationrule httpbin

    相关内容

    为 Istio deployment(cluster)提供自动化 Istio 配置,并让其像单个网格一样工作。

    把 Istio 入口网关配置为外部服务的代理。

    用于隔离和边界保护的多网格部署

    将需要隔离的环境部署到单独的网格中,并通过网格联邦启用网格间通信。

    管控出口流量的备选方案比较,包括性能因素。

    Istio 中的安全管控出口流量,第二部分

    使用 Istio 的出口流量管控来阻止相关出口流量攻击。