Istio提供熔断的功能来使你的微服务具有应对故障、潜在峰值和其他网络因素影响的能力。比如熔断器可以允许你限制服务并发连接的次数、调用失败的次数,一旦达到限制的次数,流量就会被Envoy拦截。

部署Deployment资源

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80

定义DestinationRule

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: nginx-dr
spec:
host: nginx
trafficPolicy:
connectionPool:
http:
http1MaxPendingRequests: 1
maxRequestsPerConnection: 1
tcp:
maxConnections: 1
outlierDetection:
consecutiveErrors: 1
intercal: 10s
baseEjectionTime: 10s
masEjectionPercent: 100

使用fortio测试

安装fortio

curl -L https://github.com/fortio/fortio/releases/download/v1.3.1/fortio-1.3.1-1.x86_64.rpm -o fortio-1.3.1-1.x86_64.rpm
rpm -ivh fortio-1.3.1-1.x86_64.rpm

测试

# -c表示并发数 -n表示总请求数 -qps表示每秒查询数,0表示不限制 地址是svc的CLUSTER-IP
fortio load -c 3 -n 30 -qps 0 http://10.1.248.193

可以从下图看出只有33%的请求成功了