断路器
- 添加httpbin示例应用
#自动注入
kubectl apply -f istio-1.0.0/samples/httpbin/httpbin.yaml
#手动注入
kubectl apply -f <(istioctl kube-inject istio-1.0.0/samples/httpbin/httpbin.yaml)
- 配置断路器
1.创建目标规则
cat <<EOF | kubectl apply -f -
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
- maxConnections : 限制对后端服务发起的 HTTP/1.1 连接数,如果超过了这个限制,就会开启熔断。
- maxPendingRequests : 限制待处理请求列表的长度, 如果超过了这个限制,就会开启熔断。
- maxRequestsPerConnection : 在任何给定时间内限制对后端服务发起的 HTTP/2 请求数,如果超过了这个限制,就会开启熔断。
- 详细解释
- maxConnections : 表示在任何给定时间内, Envoy 与上游集群(这里指的是 httpbin 服务)建立的最大连接数。该配置仅适用于 HTTP/1.1 协议,因为 HTTP/2 协议可以在同一个 TCP 连接中发送多个请求,而 HTTP/1.1 协议在同一个连接中只能处理一个请求。如果超过了这个限制(即断路器溢出),集群的upstream_cx_overflow 计数器就会递增。
- maxPendingRequests : 表示待处理请求队列的长度。因为 HTTP/2 是通过单个连接并发处理多个请求的,因此该熔断策略仅在创建初始 HTTP/2 连接时有用,之后的请求将会在同一个 TCP 连接上多路复用。对于 HTTP/1.1 协议,只要没有足够的上游连接可用于立即分派请求,就会将请求添加到待处理请求队列中,因此该断路器将在该进程的生命周期内保持有效。如果该断路器溢出,集群的 upstream_rq_pending_overflow 计数器就会递增。
- maxRequestsPerConnection : 表示在任何给定时间内,上游集群中所有主机(这里指的是 httpbin 服务)可以处理的最大请求数。实际上,这适用于仅 HTTP/2 集群,因为 HTTP/1.1 集群由最大连接数断路器控制。如果该断路器溢出,集群的 upstream_rq_pending_overflow 计数器就会递增。
2.验证规则
kubectl get destinationrules.networking.istio.io -o yaml httpbin
- 添加客户端
创建客户端以将流量发送到httpbin服务。客户端是一个名为fortio的简单负载测试客户端。Fortio允许您控制传出HTTP调用的连接数,并发数和延迟。您将使用此客户端“跳闸”您在中设置的断路器策略DestinationRule
1.创建fortio
#自动注入
kubectl apply -f istio-1.0.0/samples/httpbin/sample-client/fortio-deploy.yaml
2.登录客户端pod并使用fortio工具进行呼叫httpbin,传入-curl
#获取fortio pod 的name
FORTIO_POD=$(kubectl get pod | grep fortio | awk '{ print $1 }')
#呼叫httpbin
kubectl exec -it $FORTIO_POD -c fortio /usr/local/bin/fortio -- load -curl http://httpbin:8000/get
- 测试断路器
1.使用两个并发连接(-c 2)调用服务并发送20个请求(-n 20)
kubectl exec -it $FORTIO_POD -c fortio /usr/local/bin/fortio -- load -c 2 -qps 0 -n 20 -loglevel Warning http://httpbin:8000/get
2.使用两个并发连接(-c 3)调用服务并发送20个请求(-n 20)
kubectl exec -it $FORTIO_POD -c fortio /usr/local/bin/fortio -- load -c 3 -qps 0 -n 20 -loglevel Warning http://httpbin:8000/get
- 查询istio-proxy统计信息
kubectl exec -it $FORTIO_POD -c istio-proxy -- sh -c 'curl localhost:15000/stats' | grep httpbin | grep pending
流量镜像
- 安装测试服务
安装启用了访问日志的httbin服务的两个版本,httpbin-v1和httpbin-v2。
httpbin-v1
cat <<EOF | istioctl kube-inject -f - | kubectl create -n httpbin -f -
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: httpbin-v1
spec:
replicas: 1
template:
metadata:
labels:
app: httpbin
version: v1
spec:
containers:
- image: docker.io/kennethreitz/httpbin
imagePullPolicy: IfNotPresent
name: httpbin
command: ["gunicorn", "--access-logfile", "-", "-b", "0.0.0.0:8080", "httpbin:app"]
ports:
- containerPort: 8080
EOF
httpbin-v2
cat <<EOF | istioctl kube-inject -f - | kubectl create -n httpbin -f -
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: httpbin-v2
spec:
replicas: 1
template:
metadata:
labels:
app: httpbin
version: v2
spec:
containers:
- image: docker.io/kennethreitz/httpbin
imagePullPolicy: IfNotPresent
name: httpbin
command: ["gunicorn", "--access-logfile", "-", "-b", "0.0.0.0:8080", "httpbin:app"]
ports:
- containerPort: 8080
EOF
sleep service
cat <<EOF | istioctl kube-inject -f - | kubectl create -n httpbin -f -
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: sleep
spec:
replicas: 1
template:
metadata:
labels:
app: sleep
spec:
containers:
- name: sleep
image: tutum/curl
command: ["/bin/sleep","infinity"]
imagePullPolicy: IfNotPresent
EOF
- 创建路由规则和策略
将所有流量路由到v1版本
默认情况为在httpbin服务的v1和v2两个版本之间进行负载
cat <<EOF | istioctl create -n httpbin -f -
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin
spec:
hosts:
- httpbin
http:
- route:
- destination:
host: httpbin
subset: v1
weight: 100
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: httpbin
spec:
host: httpbin
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
EOF
- 发送流量测试
kubectl exec -it -n httpbin SLEEP_POD -c sleep -- sh -c 'curl http://httpbin:8080/headers' | python -m json.tool
- 查看日志验证
kubectl logs -f V1_POD -c httpbin
kubectl logs -f V2_POD -c httpbin
镜像流量到v2
cat <<EOF | istioctl replace -n httpbin -f -
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin
spec:
hosts:
- httpbin
http:
- route:
- destination:
host: httpbin
subset: v1
weight: 100
mirror:
host: httpbin
subset: v2
EOF
- 发送流量测试
kubectl exec -it -n httpbin SLEEP_POD -c sleep -- sh -c 'curl http://httpbin:8080/headers' | python -m json.tool
- 查看日志验证
kubectl logs -f V1_POD -c httpbin
kubectl logs -f V2_POD -c httpbin