总目录索引:istio 从入门到放弃系列
1、虚拟服务(Virtual Service)
虚拟服务(Virtual Service)以及目标规则(Destination Rule)是 Istio 流量路由的两大基石。虚拟服务可以将流量路由到 Istio 服务网格中的服务。每个虚拟服务由一组路由规则组成,这些路由规则按顺序进行评估。
如果没有 Istio virtual service,仅仅使用 k8s service 的话,那么只能实现最基本的流量负载均衡转发,但是就不能实现类似按百分比来分配流量等更加复杂、丰富、细粒度的流量控制了。
备注:虚拟服务相当于 K8s 服务的 sidecar,在原本 K8s 服务的功能之上,提供了更加丰富的路由控制。
2、虚拟服务示例
以下介绍使用 “虚拟服务(virtual service)+目标规则(destination rule)” 实现一个流量分流的例子。本示例共需要四种资源,k8s 和 istio 各两种:
k8s 资源介绍如下:
1、两个deployment,一个nginx,一个tomcat
2、一个service关联上面两个deploy
istio资源介绍如下:
1、一个destination rule #设置目标规则定义
2、一个VirtualService关联上面的svc,用来设置分流权重,以及设置分流目标规则定义
3、部署k8s资源
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deploy namespace: test spec: replicas: 1 selector: matchLabels: type: web app: nginx template: metadata: labels: type: web app: nginx spec: containers: - image: nginx:1.14-alpine imagePullPolicy: IfNotPresent name: nginx ports: - containerPort: 80 name: port protocol: TCP --- apiVersion: apps/v1 kind: Deployment metadata: name: tomcat-deploy namespace: test spec: replicas: 1 selector: matchLabels: type: web app: tomcat template: metadata: labels: type: web app: tomcat spec: containers: - image: docker.io/kubeguide/tomcat-app:v1 imagePullPolicy: IfNotPresent name: tomcat ports: - containerPort: 8080 name: port protocol: TCP --- apiVersion: v1 kind: Service metadata: name: web-svc namespace: test spec: ports: - name: port port: 8080 protocol: TCP targetPort: 8080 selector: type: web sessionAffinity: None type: NodePort
自此,k8s 层面的资源文件准备完毕,现在通过访问 service,可以发现自动实现了RoundBin的负载均衡策略,即分配到 tomcat 和 nginx 的流量各为 50%。
4、部署istio资源
Istio 资源共有两类,分别为虚拟服务(Virtual Service)和目的地规则(Destination Rule)。虚拟服务作用在 k8s 服务之上,并加强了原 k8s 服务的功能:
指定目的地(tomcat 或 nginx)
重新分配流量(即不再是 50% / 50%,而是 75% / 25%)
4.1 目的地规则文件
apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: test-dr spec: host: web-svc subsets: - name: tomcat labels: app: tomcat - name: nginx labels: app: nginx
4.2 虚拟服务文件
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: test-virtual-svc spec: hosts: - "web-svc" http: - route: - destination: host: web-svc subset: nginx weight: 25 - destination: host: web-svc subset: tomcat weight: 75
4.3 istio注入
执行如下命令进行 Istio 注入:
istioctl kube-inject -f test-deploy.yaml |kubectl apply -f -
然后exec进入nginx-deploy的pod中修改配置文件监听端口为8080
vi /etc/nginx/conf.d/default.conf
修改完保存退出,并重新加载nginx配置:
nginx -s reload
netstat -auntlp #查看监听端口
备注:如果不想每次注入,就给ns注入sidecar
kubectl label namespaces test istio-injection=enabled
kubectl get ns test --show-labels
4.4 通过客户端访问测试
kubectl run --namespace=test busybox --rm -ti --image busybox /bin/sh
wget -q -O - http://web-svc:8080
会发现出现页面的比例和设置的权重是一致的。
5、destination rule介绍
destination rules 是 Istio 流量路由的关键功能,它不能独自使用,必须跟 Virtual Service 共同发挥作用。当 destination rules 跟 virtual service 共同使用的时候,virtual service 决定将流量路由到逻辑地址,而 destination rules 则决定流量路由到物理地址。
virtual service 跟 destination rules 路由关系就像变量到内存的地址映射一样,destination 代表内存实际地址,而 virtual service 作用就像程序的指针。
destination rules 通常用在微服务的版本分组上(例如可以通过 version 标签将微服务进行分组)。通过 destination rules 的分组规则可以实现将流量路由到服务的不同版本中,进而实现类似灰度、金丝雀、蓝绿等版本分流的策略。
destination rules 不仅可以决定把流量路由到何处,还可以制定如何路由流量(比如是轮询路由流量,还是随机路由流量等等)
6、路由策略
默认情况下,Istio 使用轮询的负载均衡路由策略(round-robin),也就是说服务所有实例按顺序接收请求。当然 Istio 也支持如下的模型,这些模型都可以通过在 destination rule 中进行指定:
Random:请求被随机分配给服务的实例
Weighted:请求基于权重被分配给服务的实例
Least requests:请求被分配给服务最少被访问的实例
参考文章:https://blog.51cto.com/14625168/2487593
https://blog.51cto.com/14625168/2485525