istio篇(三)virtual service
虚拟服务(Virtual Service)以及目标规则(Destination Rule)是 Istio 流量路由的两大基石。虚拟服务可以将流量路由到 Istio 服务网格中的服务。每个虚拟服务由一组路由规则组成,这些路由规则按顺序进行评估。如果没有 Istio virtual service,仅仅使用 k8s service 的话,那么只能实现最基本的流量负载均衡转发,但是就不能实现类似按百
Istio virtual service
1.虚拟服务(Virtual Service)
虚拟服务(Virtual Service)以及目标规则(Destination Rule)是 Istio 流量路由的两大基石。虚拟服务可以将流量路由到 Istio 服务网格中的服务。每个虚拟服务由一组路由规则组成,这些路由规则按顺序进行评估。
如果没有 Istio virtual service,仅仅使用 k8s service 的话,那么只能实现最基本的流量负载均衡转发,但是就不能实现类似按百分比来分配流量等更加复杂、丰富、细粒度的流量控制了。
2.虚拟服务跟 K8s 服务的关系
虚拟服务相当于 K8s 服务的 sidecar,在原本 K8s 服务的功能之上,提供了更加丰富的路由控制。
3.虚拟服务实例
以下介绍使用 “虚拟服务(virtual service)+目标规则(destination rule)” 实现一个流量分流的例子。本示例共需要四种资源,k8s 和 istio 各两种:
k8s 资源介绍如下:
1 三个 deployment,一个 tomcat,一个 nginx,一个client
2 一个 service 关联上面这两个 deployment
Istio 资源介绍如下:
一个 virtual service 关联上面的 service,用来设置分流权重(weight)和设置分流目标规则定义)
这里没有结合destination rule来使用,只是单纯的使用virtual service来做流量分发
3.1k8s 资源
k8s 资源共有两类,分别为 deployment 和 service,其中 deployment 资源文件明细如下:
jiuxi-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd
labels:
server: httpd
app: web
spec:
replicas: 1
selector:
matchLabels:
server: httpd
app: web
template:
metadata:
name: httpd
labels:
server: httpd
app: web
spec:
containers:
- name: busybox
image: busybox
imagePullPolicy: IfNotPresent
command: [ "/bin/sh","-c","echo 'hello httpd' > /var/www/index.html;httpd -f -p 8080 -h /var/www" ]
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat
labels:
server: tomcat
app: web
spec:
replicas: 1
selector:
matchLabels:
server: tomcat
app: web
template:
metadata:
name: tomcat
labels:
server: tomcat
app: web
spec:
containers:
- name: tomcat
image: docker.io/kubeguide/tomcat-app:v1
imagePullPolicy: IfNotPresent
jiuxi-client.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: client
spec:
replicas: 1
selector:
matchLabels:
app: client
template:
metadata:
labels:
app: client
spec:
containers:
- name: busybox
image: busybox
imagePullPolicy: IfNotPresent
command: [ "/bin/sh", "-c", "sleep 3600" ]
部署上面的 jiuxi-deploy.yaml 文件,语句如下:
kubectl apply -f jiuxi-client.yaml
kubectl apply -f jiuxi-deploy.yaml
service 资源文件明细如下:
jiuxi-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: tomcat-svc
spec:
selector:
server: tomcat
ports:
- name: http
port: 8080
targetPort: 8080
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: httpd-svc
spec:
selector:
server: httpd
ports:
- name: http
port: 8080
targetPort: 8080
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: web-svc
spec:
selector:
app: web
ports:
- name: http
port: 8080
targetPort: 8080
protocol: TCP
部署上面的 service 文件,语句如下:
kubectl apply -f jiuxi-svc.yaml
自此,k8s 层面的资源文件准备完毕,现在通过访问 service,可以发现自动实现了RoundBin的负载均衡策略,即分配到 tomcat 和 nginx 的流量各为 50%。
3.2 Istio 资源
Istio 资源共有两类,分别为虚拟服务(Virtual Service)和目的地规则(Destination Rule)。虚拟服务作用在 k8s 服务之上,并加强了原 k8s 服务的功能:
-
指定目的地(tomcat 或 nginx)
-
重新分配流量(即不再是 50% / 50%,而是 80% / 20%)
jiuxi-vs.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: web-svc-vs
spec:
hosts:
- web-svc
http:
- route:
- destination:
host: tomcat-svc
weight: 20
- destination:
host: httpd-svc
weight: 80
执行
kubectl apply -f jiuxi-vs.yaml
最后看下服务是否都正常
kubectl get pod,svc,vs
3.3 Istio 注入
istioctl kube-inject -f jiuxi-deploy.yaml | kubectl apply -f -
上面的截图显示三个pod都是2/2是已经注入好了
Istio 注入成功后,通过 kubectl exec 登录到 busybox (client)容器中,然后执行如下语句,验证 virtual service 功能:
kubectl exec -it client-7db8d6545c-fr8xj -- sh
wget -q -O - http://web-svc:8080
通过istio自带的kiali也可以看到流量比例大概是4:1
3.4 根据header来调度流量
先把前面根据不同比例调度流量的virtual service停掉
kubectl delete -f jiuxi-vs.yaml
创建根据header匹配调度流量的virtual service
jiuxi-vs-with-condition.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: web-svc-vs
spec:
hosts:
- web-svc
http:
- match:
- headers:
end-user:
exact: jiuxi
route:
- destination:
host: tomcat-svc
- route:
- destination:
host: httpd-svc
创建
kubectl apply -f jiuxi-vs-with-condition.yaml
再去client容器中测试
不加header,不管访问多少次,结果都是hello httpd
wget -q -O - http://web-svc:8080
加入header,访问的就是tomcat页面了
wget -q -O - --header 'end-user:jiuxi' http://web-svc:8080
更多推荐
所有评论(0)