Istio 目标规则 (Destination Rule)
目标规则(Destination Rule)是 Istio 重要的资源对象之一,它不能独自使用,必须跟 Virtual Service 共同发挥作用,作用是将流量标记分组并路由到具体服务。
Destination Rule 还可以做什么
通常在生产场景下,用使用 Destination Rule 对用户进行身份、地址位置等条件的识别后的流量路由,例如部分用户优先享用新版本,则可以通过HTTP Header附加相关的字段进行识别,路由到新版本的服务上。或者在版本更新的时候,使用灰度发布,对新旧版本标记子集,按照不同的负载百分比进行调整逐步迭代。
通过例子来理解
有两个Deployment(nginx 及 httpd),通过Service关联到一起,通过访问Service只能做到简单的负载均衡,通过实验发现 nginx 和 httpd 流量各自在 50% 左右。
Deployment & Service
apiVersion: apps/v1
kind: Deployment
metadata:
labels: app: nginx
name: nginx-deployment
spec:
replicas: 1
selector: matchLabels: app: nginx
strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate
template: metadata: labels: app: nginx server: web spec: containers: - image: 'nginx:latest' name: nginx-deployment
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels: app: httpd
name: httpd-deployment
spec:
replicas: 1
selector: matchLabels: app: httpd
strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate
template: metadata: labels: app: httpd server: web spec: containers: - image: 'httpd:latest' name: httpd-deployment
---
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
ports:
- port: 80 protocol: TCP targetPort: 80
selector: server: web
type: ClusterIP
- 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
如果想实现更加细颗粒度的流量管控,通过引入Istio Vistrual Service 及 Destination Rule,非常简单的就实现复杂的流量管理。
DestinationRule 根据标签将流量分成不同的子集,已提供 VirtualService 进行调度,并且设置相关的负载百分比实现精准的控制。
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: web-dr
spec:
host: web-svc
subsets:
- name: httpd labels: app: httpd
- name: nginx labels: app: nginx
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: web-vs
spec:
hosts:
- web-service
http:
- route: - destination: host: web-service subset: nginx weight: 80 - destination: host: web-service subset: httpd weight: 20
- 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
通过客户端测试以上的实验,请留意客户端也必须经过 Istio 注入,因为只有客户端被 Istio 注入才可以接收到来自 Pilot 有关 Virtual Service 和 Destination Rule 的配置信息,才可以保证流量接管生效。
apiVersion: apps/v1
kind: Deployment
metadata:
labels: app: client-deployment
name: client-deployment
spec:
replicas: 1
selector: matchLabels: app: client-deployment
strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate
template: metadata: labels: app: client-deployment spec: containers: - image: 'busybox:latest' name: client-deployment command: [ "/bin/sh", "-c", "sleep 3600"]
- 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
进入客户端容器执行 wget -q -O - web-service
观察执行结果
更丰富的流量策略
在生产环境中,应用到的流量策略不单单只是加权的负载均衡那么简单, Destination Rule 还支持 最小连接数、随机负载等等。
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: web-dr
spec:
host: web-svc
trafficPolicy: loadBalancer: simple: RANDOM
subsets:
- name: httpd labels: app: httpd
- name: nginx labels: app: nginx trafficPolicy: loadBalancer: simple: LEAST_CONN
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
Destination Rule 字段解析
host - 指的是 Kuberentes 中的服务
subsets - 针对不同标签选择的流量子集
loadBalancer - 负载均衡路由策略
文章来源: blog.csdn.net,作者:叶康铭,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/m0_38030719/article/details/108928551
- 点赞
- 收藏
- 关注作者
评论(0)