istio DestinationRule 流量管控
DestinationRule 是 VirtualService 路由生效后,配置应用于请求的策略集
dr 的作用:
DR的基本用法-定义子集
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| mkdir chap3 && cd chap3 cp chap2/vs.yaml chap2/mygw1.yaml chap3/
kubectl delete svc svc2 kubectl delete svc svc1
kubectl label pod pod1 name=pod kubectl label pod pod2 name=pod kubectl get pods -l name=pod
kubectl get pods --show-label
kubectl expose --name=svc1 pod pod1 --port=80 --selector=name=pod
kubectl describe svc svc1
vim vs.yaml
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: myvs spec: hosts: - "aa.yuan.cc" gateways: - mygw http: - route: - destination: host: svc1 subset: xx weight: 55 - destination: host: svc1 subset: yy weight: 45
kubectl apply -f vs.yaml
vim dr.yaml
apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: mydr spec: host: svc1 subsets: - name: xx labels: run: pod1 - name: yy labels: run: pod2
kubectl apply -f dr.yaml kubectl get dr
while true ; do curl aa.yuan.cc ; sleep 1 ; done
|
查看 kiali 权重图
蓝绿部署
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| vim dr.yaml
apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: mydr spec: host: svc1 subsets: - name: v1 labels: run: pod1 - name: v2 labels: run: pod2
vim vs.yaml
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: myvs spec: hosts: - "aa.yuan.cc" gateways: - mygw http: - route: - destination: host: svc1 subset: v1 weight: 100 - destination: host: svc1 subset: v2 weight: 0
kubectl apply -f dr.yaml kubectl apply -f vs.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
| vim vs.yaml
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: myvs spec: hosts: - "aa.yuan.cc" gateways: - mygw http: - route: - destination: host: svc1 subset: v1 weight: 100 - destination: host: svc1 subset: v2 weight: 0
1、v2 并没有对任何人开放 2、v2 对极少一部分人开放 3、v2 给更多的人开放
|
会话保持
会话保持的目的是,让同一个客户端访问的时候,访问到同一个pod上,istio用一致性哈希算法实现
了会话保持。
在trafficPolicy.loadBalancer.consistentHash里的字段包括:
httpHeaderName: 根据HTTP Header获取哈希值
httpCookie: 根据HTTP Cookie获取哈希值 (根据 Cookie 来做会话保持)
userSourceIp: 根据源IP获取哈希值 (根据 IP 地址来做的会话保持)
minimumRingSize: 哈希环所需的最小虚拟节点数量,默认值为1024
使用httpCookie时的字段:
name: cookie的名称
path: 设置cookie的路径
ttl:cookie的生命期
1
| while true ; do curl aa.rhce.cc --cookie "user=tester" --silent -w "Status: %{http_code}\n"; sleep 1 ; done
|
基于 cookie 的会话保持
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| mv vs.yaml vs-subset.yaml cp ../chap2/vs.yaml .
vim vs.yaml
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: myvs spec: hosts: - "aa.yuan.cc" gateways: - mygw http: - route: - destination: host: svc1
kubectl apply -f vs.yaml
vim dr.yaml
apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: mydr spec: host: svc1 trafficPolicy: loadBalancer: consistentHash: httpCookie: name: user ttl: 60s subsets: - name: v1 labels: run: pod1 - name: v2 labels: run: pod2
kubectl apply -f dr.yaml
while true ; do curl --cookie "user=tester" aa.rhce.cc ; sleep 1 ; done
while true ; do curl --cookie "user11=tester" aa.rhce.cc ; sleep 1 ; done
|
基于 ip 地址的会话保持
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 32 33
| vim dr.yaml
apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: mydr spec: host: svc1 trafficPolicy: loadBalancer: consistentHash: useSourceIp: true subsets: - name: v1 labels: run: pod1 - name: v2 labels: run: pod2
kubectl apply -f dr.yaml
while true ; do curl aa.rhce.cc ; sleep 1 ; done
cp dr.yaml dr-session.yaml kubectl delete -f dr.yaml
|
DR调度算法-RANDOM-ROUND_ROBIN
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 32 33 34
| cp dr.yaml dr-arithmetic.yaml vim dr-arithmetic.yaml
apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: mydr spec: host: svc1 trafficPolicy: loadBalancer: simple: RANDOM -------------------------------------------------
apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: mydr spec: host: svc1 subsets: - name: v1 labels: run: pod1 - name: v2 labels: run: pod2 trafficPolicy: loadBalancer: simple: RANDOM
|
DR调度算法-LEAST_CONN
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 32 33 34 35 36
| kubectl get pod -o wide
vim dr-arithmetic.yaml
apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: mydr spec: host: svc1 trafficPolicy: loadBalancer: simple: LEAST_CONN
kubectl apply -f dr-arithmetic.yaml
yum install ctags* gcc -y mkdir -p -m 644 /usr/local/man/man1
wget wget http://www.ha97.com/code/webbench-1.5.tar.gz tar zxvf webbench-1.5.tar.gz ; cd webbench-1.5
make && make install
kubectl get pods -owide
webbench -c 500 -t 300 http://10.244.223.203/
kubectl delete -f dr-arithmetic.yaml
|
熔断
熔断的定义是在DR里定义的,主要有2部分
第一部分:
trafficPolicy:
用于定义连接池
连接池的定义分成两类:
- TCP 连接
- maxConncections: 到目标主机的最大连接数
- connectTimeOut: TCP连接超时,最小值必须要大于1ms
- Http 连接
- http1MaxPendingReguests: 针对一个目标的HTTP请求最大排队数量,默认是1024
- http2MaxRequests: 对一个后端的最大请求数
- maxRequestsPerConnection:
- maxReties: 在给定的时间,集群所有主机最大重试数,默认值为3
第二部分:
outlierDetection:
用于定义熔断的条件,达到什么条件就开始熔断
consecutiveErrors: 超过这错误数量之后,主机会被移除连接池。默认是5,当上游服务是
http服务时,5xx返回代码会记录为错误。当上游主机提供的是TCP服务时,TCP连接超时和连接错
误被标记为错误。
interval: 在移除检测之间的时间间隔,默认是10s,必须要>=1ms
baseEjectionTime: 最小的移除时间长度。主机每次被移除后的间隔时间等于被移除的次数和
最小移除时间的乘积。这样的实现,让系统能够自动增加不健康上游服务实例的间隔时间,默认时
间为30s。
maxEjectionPercent: 上游服务的负载均衡池中允许被移除的主机的最大百分比,默认是10%。
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 32 33 34 35 36
| vim dr-rongduan.yaml
apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: mydr spec: host: svc1 trafficPolicy: connectionPool: http: http1MaxPendingRequests: 1 maxRequestsPerConnection: 1 outlierDetection: consecutiveGatewayErrors: 1 interval: 10s baseEjectionTime: 3m maxEjectionPercent: 100
kubectl apply -f dr-rongduan.yaml
rpm -i https://github.com/fortio/fortio/releases/download/v1.30.0/fortio-1.30.0-1.x86_64.rpm
fortio load -c 5 -n 20 -qps 0 http://aa.yuan.cc
|