VirtualService资源详解
学习目标

什么是virtualService
VirtualService中文名称虚拟服务,是istio中一个重要的资源, 它定义了一系列针对指定服务的流量路由规则。每个路由规则都针对特定协议的匹配规则。如果流量符合这些特征,就会根据规则发送到服务注册表中的目标服务(或者目标服务的子集或版本)。
vs和k8s service的区别
如果没有 Istio virtual service,仅仅使用 k8s service 的话,那么只能实现最基本的流量负载均衡转发,但是就不能实现类似按百分比来分配流量等更加复杂、丰富、细粒度的流量控制了。
备注:虚拟服务相当于 K8s 服务的 sidecar,在原本 K8s 服务的功能之上,提供了更加丰富的路由控制。
例子:
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: test-virtual-svc 5spec: 6 hosts: 7 - "web-svc" 8 http: 9 - route: 10 - destination: 11 host: web-svc 12 subset: nginx 13 weight: 25 14 - destination: 15 host: web-svc 16 subset: tomcat 17 weight: 75
配置详解
exportTo
1只在当前名称空间有效
virtaulservice/vs-bookinfo-dot.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - . 8 hosts: 9 - "*" 10 gateways: 11 - bookinfo-gateway 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 route: 25 - destination: 26 host: productpage.istio.svc.cluster.local 27 port: 28 number: 9080
2所有名称空间有效
virtaulservice/vs-bookinfo-star.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 hosts: 9 - "*" 10 gateways: 11 - bookinfo-gateway 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 route: 25 - destination: 26 host: productpage.istio.svc.cluster.local 27 port: 28 number: 9080
3特定名称空间有效
virtaulservice/vs-bookinfo-istio-system.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 # - "default" 8 # - "istio" 9 - "istio-system" 10 hosts: 11 - "*" 12 gateways: 13 - bookinfo-gateway 14 http: 15 - match: 16 - uri: 17 exact: /productpage 18 - uri: 19 prefix: /static 20 - uri: 21 exact: /login 22 - uri: 23 exact: /logout 24 - uri: 25 prefix: /api/v1/products 26 route: 27 - destination: 28 host: productpage.istio.svc.cluster.local 29 port: 30 number: 9080
gateways
Gateway 名称列表,Sidecar 会据此使用路由。VirtualService 对象可以用于网格中的 Sidecar,也可以用于一个或多个 Gateway。这里公开的选择条件可以在协议相关的路由过滤条件中进行覆盖。保留字 mesh 用来指代网格中的所有 Sidecar。当这一字段被省略时,就会使用缺省值(mesh),也就是针对网格中的所有 Sidecar 生效。如果提供了 gateways 字段,这一规则就只会应用到声明的 Gateway 之中。要让规则同时对 Gateway 和网格内服务生效,需要显式的将 mesh 加入 gateways 列表。
1单个gateway
virtaulservice/vs-bookinfo-gw-single.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - bookinfo-gateway 10 http: 11 - match: 12 - uri: 13 exact: /productpage 14 - uri: 15 prefix: /static 16 - uri: 17 exact: /login 18 - uri: 19 exact: /logout 20 - uri: 21 prefix: /api/v1/products 22 route: 23 - destination: 24 host: productpage.istio.svc.cluster.local 25 port: 26 number: 9080
2多个gateway
virtaulservice/vs-bookinfo-multi-gw.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - bookinfo-gateway 10 - bookinfo-gateway-02 11 http: 12 - match: 13 - uri: 14 exact: /productpage 15 - uri: 16 prefix: /static 17 - uri: 18 exact: /login 19 - uri: 20 exact: /logout 21 - uri: 22 prefix: /api/v1/products 23 route: 24 - destination: 25 host: productpage.istio.svc.cluster.local 26 port: 27 number: 9080
3不同名称空间下的gateway
virtaulservice/vs-bookinfo-gw-namespace.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - default/bookinfo-gateway 10 http: 11 - match: 12 - uri: 13 exact: /productpage 14 - uri: 15 prefix: /static 16 - uri: 17 exact: /login 18 - uri: 19 exact: /logout 20 - uri: 21 prefix: /api/v1/products 22 route: 23 - destination: 24 host: productpage.istio.svc.cluster.local 25 port: 26 number: 9080
4省略gateways默认为mesh
virtaulservice/vs-review-v2.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - route: 10 - destination: 11 host: reviews 12 subset: v2
5gateways为mesh
virtaulservice/vs-review-mesh.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 gateways: 7 - mesh 8 hosts: 9 - reviews 10 http: 11 - route: 12 - destination: 13 host: reviews 14 subset: v3
hosts
必要字段:流量的目标主机。可以是带有通配符前缀的 DNS 名称,也可以是 IP 地址。根据所在平台情况,还可能使用短名称来代替 FQDN。这种场景下,短名称到 FQDN 的具体转换过程是要靠下层平台完成的。**一个主机名只能在一个 VirtualService 中定义。**同一个 VirtualService 中可以用于控制多个 HTTP 和 TCP 端口的流量属性。 Kubernetes 用户注意:当使用服务的短名称时(例如使用 reviews,而不是 reviews.default.svc.cluster.local),Istio 会根据规则所在的命名空间来处理这一名称,而非服务所在的命名空间。假设 “default” 命名空间的一条规则中包含了一个 reviews 的 host引用,就会被视为 reviews.default.svc.cluster.local,而不会考虑 reviews 服务所在的命名空间。为了避免可能的错误配置,建议使用 FQDN 来进行服务引用。 hosts 字段对 HTTP 和 TCP 服务都是有效的。网格中的服务也就是在服务注册表中注册的服务,必须使用他们的注册名进行引用;只有 Gateway 定义的服务才可以使用 IP 地址。
ip
virtaulservice/vs-bookinfo-hosts-ip.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 hosts: 7 - "192.168.198.155" 8 gateways: 9 - bookinfo-gateway 10 http: 11 - match: 12 - uri: 13 exact: /productpage 14 - uri: 15 prefix: /static 16 - uri: 17 exact: /login 18 - uri: 19 exact: /logout 20 - uri: 21 prefix: /api/v1/products 22 route: 23 - destination: 24 host: productpage.istio.svc.cluster.local 25 port: 26 number: 9080
多个hosts
virtaulservice/vs-bookinfo-hosts-multi.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 hosts: 7 - "bookinfo.com" 8 - "bookinfo.demo" 9 gateways: 10 - bookinfo-gateway 11 http: 12 - match: 13 - uri: 14 exact: /productpage 15 - uri: 16 prefix: /static 17 - uri: 18 exact: /login 19 - uri: 20 exact: /logout 21 - uri: 22 prefix: /api/v1/products 23 route: 24 - destination: 25 host: productpage.istio.svc.cluster.local 26 port: 27 number: 9080
匹配所有域名
virtaulservice/vs-bookinfo-hosts-star.yaml
1kind: VirtualService 2metadata: 3 name: bookinfo 4spec: 5 hosts: 6 - "*" 7 gateways: 8 - bookinfo-gateway 9 http: 10 - match: 11 - uri: 12 exact: /productpage 13 - uri: 14 prefix: /static 15 - uri: 16 exact: /login 17 - uri: 18 exact: /logout 19 - uri: 20 prefix: /api/v1/products 21 route: 22 - destination: 23 host: productpage.istio.svc.cluster.local 24 port: 25 number: 9080
短fqdn
virtaulservice/vs-bookinfo-hosts-fqdn-short.yaml
在default名称空间创建vs
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 hosts: 7 - "bookinfo" 8 http: 9 - match: 10 - uri: 11 exact: /productpage 12 - uri: 13 prefix: /static 14 - uri: 15 exact: /login 16 - uri: 17 exact: /logout 18 - uri: 19 prefix: /api/v1/products 20 route: 21 - destination: 22 host: productpage.istio.svc.cluster.local 23 port: 24 number: 9080
同时要创建一个同名service
1[root@master01 virtaulservice]# cat bookinfo-svc.yaml 2apiVersion: v1 3kind: Service 4metadata: 5 name: bookinfo 6 labels: 7 app: productpage 8 service: productpage 9spec: 10 ports: 11 - port: 9080 12 name: http 13 selector: 14 app: productpage
长fqdn
virtaulservice/vs-bookinfo-hosts-fqdn-long.yaml
在default名称空间创建vs
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 hosts: 7 - "bookinfo.default.svc.cluster.local" 8 http: 9 - match: 10 - uri: 11 exact: /productpage 12 - uri: 13 prefix: /static 14 - uri: 15 exact: /login 16 - uri: 17 exact: /logout 18 - uri: 19 prefix: /api/v1/products 20 route: 21 - destination: 22 host: productpage.istio.svc.cluster.local 23 port: 24 number: 9080
同时在default名称空间创建bookinfo svc
virtaulservice/bookinfo-svc.yaml
1apiVersion: v1 2kind: Service 3metadata: 4 name: bookinfo 5 labels: 6 app: productpage 7 service: productpage 8spec: 9 ports: 10 - port: 9080 11 name: http 12 selector: 13 app: productpage
http
HTTP 流量规则的有序列表。这个列表对名称前缀为 http-、http2-、grpc- 的服务端口,或者协议为 HTTP、HTTP2、GRPC 以及终结的 TLS,另外还有使用 HTTP、HTTP2 以及 GRPC 协议的 ServiceEntry 都是有效的。进入流量会使用匹配到的第一条规则。
corsPolicy
cors介绍 https://blog.csdn.net/java\_green\_hand0909/article/details/78740765
配置httpd服务
1[root@master01 html]# cat index.html 2<html> 3<head><title></title></head> 4<body> 5<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 6<script> 7$(function(){ 8 $("#cors").click( 9 function(){ 10 $.ajax({ 11 type:"get", 12 dataType : "html", 13 url:"http://bookinfo.demo:27941/productpage", 14 success:function(data){ 15 alert(data); 16 } 17 }) 18 }); 19 20 $("#cors2").click( 21 function(){ 22 $.ajax({ 23 type:"get", 24 dataType : "json", 25 url:"http://bookinfo.demo:27941/reviews/1", 26 contentType : 'application/json;charset=UTF-8', 27 success:function(data){ 28 var jsonStr = JSON.stringify(data); 29 alert(jsonStr); 30 } 31 }) 32 }); 33 $("#cors3").click( 34 function(){ 35 $.ajax({ 36 type:"delete", 37 contentType : 'application/json;charset=UTF-8', 38 dataType : "json", 39 url:"http://bookinfo.demo:27941/reviews/1", 40 success:function(data){ 41 var jsonStr = JSON.stringify(data); 42 alert(jsonStr); 43 } 44 }) 45 }); 46 $("#cors4").click( 47 function(){ 48 $.ajax({ 49 type:"get", 50 contentType : 'application/json;charset=UTF-8', 51 dataType : "json", 52 headers:{"X-Custom-Header":"value"}, 53 url:"http://bookinfo.demo:27941/reviews/1", 54 success:function(data){ 55 var jsonStr = JSON.stringify(data); 56 alert(jsonStr); 57 } 58 }) 59 }); 60 61}); 62 63</script> 64<input type="button" id="cors" value="简单请求"/> 65<input type="button" id="cors2" value="非简单请求"/> 66<input type="button" id="cors3" value="非简单请求delete"/> 67<input type="button" id="cors4" value="非简单请求headers"/> 68</body> 69</html>
注意替换端口 url:“http://bookinfo.demo:27941/productpage”,
启动nginx
systemctl start httpd
简单请求,配置cors
virtaulservice/corsPolicy/vs-productpage-cors.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 corsPolicy: 25 allowOrigins: 26 - exact: "http://mytest.com:8081" 27 route: 28 - destination: 29 host: productpage 30 port: 31 number: 9080
访问:
简单请求allowCredentials
virtaulservice/corsPolicy/vs-productpage-cors-allowCredentials.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 corsPolicy: 25 allowCredentials: true 26 allowOrigins: 27 - exact: "http://mytest.com:8081" 28 route: 29 - destination: 30 host: productpage 31 port: 32 number: 9080
简单请求allowOrigins prefix
virtaulservice/corsPolicy/vs-productpage-cors-allowOrigins-prefix.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 corsPolicy: 25 allowOrigins: 26 - prefix: "http://mytest" 27 route: 28 - destination: 29 host: productpage 30 port: 31 number: 9080
简单请求allowOrigins regex
virtaulservice/corsPolicy/vs-productpage-cors-allowOrigins-regex.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 corsPolicy: 25 allowOrigins: 26 - regex: ".*" 27 route: 28 - destination: 29 host: productpage 30 port: 31 number: 9080
简单请求exposeHeaders
virtaulservice/corsPolicy/vs-productpage-cors-exposeHeaders.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 corsPolicy: 25 allowOrigins: 26 - exact: "http://mytest.com:8081" 27 exposeHeaders: 28 - test 29 - test2 30 route: 31 - destination: 32 host: productpage 33 port: 34 number: 9080
非简单请求
virtaulservice/corsPolicy/vs-reviews-cors.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookreviews 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 prefix: /reviews 16 corsPolicy: 17 allowOrigins: 18 - exact: "http://mytest.com:8081" 19 allowMethods: 20 - GET 21 - OPTIONS 22 maxAge: "1m" 23 route: 24 - destination: 25 host: reviews 26 port: 27 number: 9080
非简单请求allowMethods
virtaulservice/corsPolicy/vs-reviews-cors-allowMethods.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookreviews 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 prefix: /reviews 16 corsPolicy: 17 allowOrigins: 18 - exact: "http://mytest.com:8081" 19 allowMethods: 20 - POST 21 - OPTIONS 22 maxAge: "1m" 23 route: 24 - destination: 25 host: reviews 26 port: 27 number: 9080
非简单请求allowHeaders
virtaulservice/corsPolicy/vs-reviews-cors-allowHeaders.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookreviews 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 prefix: /reviews 16 corsPolicy: 17 allowOrigins: 18 - exact: "http://mytest.com:8081" 19 allowMethods: 20 - GET 21 - OPTIONS 22 maxAge: "1m" 23 allowHeaders: 24 - X-Custom-Header 25 - content-type 26 route: 27 - destination: 28 host: reviews 29 port: 30 number: 9080
非简单请求maxAge
virtaulservice/corsPolicy/vs-reviews-cors-maxAge.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookreviews 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 prefix: /reviews 16 corsPolicy: 17 allowOrigins: 18 - exact: "http://mytest.com:8081" 19 allowMethods: 20 - GET 21 - OPTIONS 22 maxAge: "10s" 23 #maxAge: "1m" 24 #maxAge: "1h" 25 route: 26 - destination: 27 host: reviews 28 port: 29 number: 9080
delegate
向istiod容器设置环境变量
PILOT_ENABLE_VIRTUAL_SERVICE_DELEGATE=true
kubectl set env deploy istiod -n istio-system --list
kubectl set env deploy istiod -n istio-system PILOT_ENABLE_VIRTUAL_SERVICE_DELEGATE=true
配置文件
virtaulservice/delegate/vs-delegate.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - uri: 13 exact: /productpage 14 - uri: 15 prefix: /static 16 - uri: 17 exact: /login 18 - uri: 19 exact: /logout 20 - uri: 21 prefix: /api/v1/products 22 delegate: 23 name: productpage 24 namespace: istio
测试不成功,有待研究
fault
abort
virtaulservice/fault/vs-productpage-fault-abort.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5 namespace: istio 6spec: 7 gateways: 8 - bookinfo-gateway 9 hosts: 10 - '*' 11 http: 12 - fault: 13 abort: 14 httpStatus: 500 15 percentage: 16 value: 100 17 match: 18 - uri: 19 exact: /productpage 20 - uri: 21 prefix: /static 22 - uri: 23 exact: /login 24 - uri: 25 exact: /logout 26 - uri: 27 prefix: /api/v1/products 28 route: 29 - destination: 30 host: productpage 31 subset: v1
delay
virtaulservice/fault/vs-productpage-fault-delay.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5 namespace: istio 6spec: 7 gateways: 8 - bookinfo-gateway 9 hosts: 10 - '*' 11 http: 12 - fault: 13 delay: 14 percentage: 15 value: 100.0 16 fixedDelay: 7s 17 match: 18 - uri: 19 exact: /productpage 20 - uri: 21 prefix: /static 22 - uri: 23 exact: /login 24 - uri: 25 exact: /logout 26 - uri: 27 prefix: /api/v1/products 28 route: 29 - destination: 30 host: productpage 31 subset: v1
headers
request
add
virtaulservice/headers/vs-headers-request-add.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 headers: 25 request: 26 add: 27 TEST_REQUEST_HEADER: XX 28 route: 29 - destination: 30 host: productpage 31 port: 32 number: 9080
remove
virtaulservice/headers/vs-headers-request-remove.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 headers: 25 request: 26 remove: 27 - TEST_REQUEST_HEADER 28 route: 29 - destination: 30 host: productpage 31 port: 32 number: 9080
set
virtaulservice/headers/vs-headers-request-set.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 headers: 25 request: 26 set: 27 TEST_REQUEST_HEADER: XX 28 route: 29 - destination: 30 host: productpage 31 port: 32 number: 9080
response
add
virtaulservice/headers/vs-headers-response-add.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 headers: 25 response: 26 add: 27 TEST_REQUEST_HEADER: XX 28 route: 29 - destination: 30 host: productpage 31 port: 32 number: 9080
remove
virtaulservice/headers/vs-headers-response-remove.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 headers: 25 response: 26 remove: 27 - x-envoy-upstream-service-time 28 route: 29 - destination: 30 host: productpage 31 port: 32 number: 9080
set
virtaulservice/headers/vs-headers-response-set.yaml
没有就添加,有就修改
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 headers: 25 response: 26 set: 27 content-type: "text/html" 28 Test: "test" 29 x-envoy-upstream-service-time: "1111111111" 30 route: 31 - destination: 32 host: productpage 33 port: 34 number: 9080
match
authority
exact
virtaulservice/match/vs-match-authority-exact.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - authority: 13 exact: "bookinfo.demo:27941" 14 route: 15 - destination: 16 host: productpage 17 port: 18 number: 9080
prefix
virtaulservice/match/vs-match-authority-prefix.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - authority: 13 prefix: "bookinfo" 14 route: 15 - destination: 16 host: productpage 17 port: 18 number: 9080
regex
virtaulservice/match/vs-match-authority-regex.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - authority: 13 regex: "bookinfo.de.*" 14 route: 15 - destination: 16 host: productpage 17 port: 18 number: 9080
gateways
virtaulservice/match/vs-match-gateways.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 - bookinfo-gateway-02 9 hosts: 10 - '*' 11 http: 12 - match: 13 - uri: 14 exact: /productpage 15 gateways: 16 - bookinfo-gateway-02 17 - uri: 18 prefix: /static 19 route: 20 - destination: 21 host: productpage 22 port: 23 number: 9080
headers
exact
virtaulservice/match/
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - match: 10 - headers: 11 end-user: 12 exact: mark 13 route: 14 - destination: 15 host: reviews 16 subset: v2 17 - route: 18 - destination: 19 host: reviews 20 subset: v3
prefix
virtaulservice/match/vs-match-headers-prefix.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - match: 10 - headers: 11 end-user: 12 prefix: ma 13 route: 14 - destination: 15 host: reviews 16 subset: v2 17 - route: 18 - destination: 19 host: reviews 20 subset: v3
regex
virtaulservice/match/vs-match-headers-regex.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - match: 10 - headers: 11 end-user: 12 regex: "m.*k" 13 route: 14 - destination: 15 host: reviews 16 subset: v2 17 - route: 18 - destination: 19 host: reviews 20 subset: v3
ignoreUriCase
virtaulservice/match/vs-match-ignoreUriCase.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - uri: 13 exact: "/PRODUCTPAGE" 14 ignoreUriCase: true 15 route: 16 - destination: 17 host: productpage 18 port: 19 number: 9080
method
exact
virtaulservice/match/vs-match-method-exact.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - method: 13 exact: "GET" 14 route: 15 - destination: 16 host: productpage 17 port: 18 number: 9080
prefix
virtaulservice/match/vs-match-method-prefix.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - method: 13 prefix: "G" 14 route: 15 - destination: 16 host: productpage 17 port: 18 number: 9080
regex
virtaulservice/match/vs-match-method-regex.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - method: 13 regex: "G.*T" 14 route: 15 - destination: 16 host: productpage 17 port: 18 number: 9080
name
virtaulservice/match/vs-match-name.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - uri: 13 exact: /productpage 14 name: book 15 route: 16 - destination: 17 host: productpage 18 port: 19 number: 9080
port
virtaulservice/match/vs-match-port.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - port: 80 13 route: 14 - destination: 15 host: productpage 16 port: 17 number: 9080
queryParams
exact
virtaulservice/match/vs-match-queryParams-exact.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - queryParams: 13 test: 14 exact: test 15 route: 16 - destination: 17 host: productpage 18 port: 19 number: 9080
prefix
virtaulservice/match/vs-match-queryParams-prefix.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - queryParams: 13 test: 14 prefix: test 15 route: 16 - destination: 17 host: productpage 18 port: 19 number: 9080
不起作用,只要有queryParams为test就能访问
regex
virtaulservice/match/vs-match-queryParams-regex.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - queryParams: 13 test: 14 regex: "\\d+$" 15 route: 16 - destination: 17 host: productpage 18 port: 19 number: 9080
test值必须是数字
scheme
访问404,放弃,有待研究
exact
vs-match-scheme-exact.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - scheme: 13 exact: "http" 14 route: 15 - destination: 16 host: productpage 17 port: 18 number: 9080
prefix
vs-match-scheme-prefix.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - scheme: 13 prefix: "http" 14 route: 15 - destination: 16 host: productpage 17 port: 18 number: 9080
regex
vs-match-scheme-regex.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - scheme: 13 regex: ".*" 14 route: 15 - destination: 16 host: productpage 17 port: 18 number: 9080
sourceLabels
virtaulservice/match/vs-match-sourceLabels.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - match: 10 - sourceLabels: 11 app: productpage 12 version: v1 13 route: 14 - destination: 15 host: reviews 16 subset: v2
sourceNamespace
virtaulservice/match/vs-match-sourceNamespace.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - sourceNamespace: istio-system 13 route: 14 - destination: 15 host: productpage 16 port: 17 number: 9080
uri
exact
virtaulservice/match/vs-match-uri-exact.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - uri: 13 exact: /productpage 14 route: 15 - destination: 16 host: productpage 17 port: 18 number: 9080
prefix
virtaulservice/match/vs-match-uri-prefix.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - uri: 13 prefix: /product 14 route: 15 - destination: 16 host: productpage 17 port: 18 number: 9080
regex
virtaulservice/match/vs-match-uri-regex.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - uri: 13 regex: "/p.*e" 14 route: 15 - destination: 16 host: productpage 17 port: 18 number: 9080
withoutHeaders
测试不成功,放弃,有待研究
exact
vs-match-withoutHeaders-exact.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - match: 10 - withoutHeaders: 11 end-user: 12 exact: mark 13 route: 14 - destination: 15 host: reviews 16 subset: v2 17 - route: 18 - destination: 19 host: reviews 20 subset: v3
prefix
vs-match-withoutHeaders-prefix.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - match: 10 - withoutHeaders: 11 end-user: 12 prefix: ma 13 route: 14 - destination: 15 host: reviews 16 subset: v2 17 - route: 18 - destination: 19 host: reviews 20 subset: v3
regex
vs-match-withoutHeaders-regex.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - match: 10 - withoutHeaders: 11 end-user: 12 regex: "m.*k" 13 route: 14 - destination: 15 host: reviews 16 subset: v2 17 - route: 18 - destination: 19 host: reviews 20 subset: v3
mirror
virtaulservice/mirror/vs-http-mirror.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 route: 25 - destination: 26 host: productpage 27 port: 28 number: 9080 29 mirror: 30 host: productpage.istio-2.svc.cluster.local 31 port: 32 number: 9080 33 mirrorPercentage: 34 value: 100
1创建namespace
kubectl create ns istio-2
2打标签
kubectl label ns istio-2 istio-injection=enabled
3部署deployment
kubectl apply -f productpage-deploy.yaml -n istio-2
4打开日志
kubectl logs -f productpage-v1-64794f5db4-ng9sn -n istio-2
5创建资源
kubectl apply -f vs-http-mirror.yaml -n istio
6访问url
http://192.168.198.154:27941/productpage
subset
1创建dr
kubectl apply -f dr-productpage.yaml -n istio-2
2创建mirror资源
kubectl apply -f vs-http-mirror-subset.yaml -n istio
3访问
http://192.168.198.154:27941/productpage
4观察日志
name
virtaulservice/vs-bookinfo-name.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - bookinfo-gateway 10 http: 11 - match: 12 - uri: 13 exact: /productpage 14 - uri: 15 prefix: /static 16 - uri: 17 exact: /login 18 - uri: 19 exact: /logout 20 - uri: 21 prefix: /api/v1/products 22 name: bookinfo 23 route: 24 - destination: 25 host: productpage.istio.svc.cluster.local 26 port: 27 number: 9080
redirect
virtaulservice/redirect/vs-productpage-redirect.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 exact: /mypage 16 redirect: 17 uri: /productpage 18 authority: 192.168.198.154:27941 19 redirectCode: 308 20 - match: 21 - uri: 22 prefix: /productpage 23 - uri: 24 prefix: /static 25 - uri: 26 exact: /login 27 - uri: 28 exact: /logout 29 - uri: 30 prefix: /api/v1/products 31 route: 32 - destination: 33 host: productpage 34 port:
访问:
http://192.168.198.154:27941/mypage
retries
- attempts:必选字段,定义重试的次数
- perTryTimeout:每次重试超时的时间,单位可以是ms、s、m和h
- retryOn:进行重试的条件,可以是多个条件,以逗号分隔
其中重试条件retryOn的取值可以包括以下几种。
- 5xx:在上游服务返回5xx应答码,或者在没有返回时重试
- gateway-error:类似于5xx异常,只对502、503和504应答码进行重试。
- connect-failure:在链接上游服务失败时重试 retriable-4xx:在上游服务返回可重试的4xx应答码时执行重试。
- refused-stream:在上游服务使用REFUSED_STREAM错误码重置时执行重试。
- cancelled:gRPC应答的Header中状态码是cancelled时执行重试。
- deadline-exceeded:在gRPC应答的Header中状态码是deadline-exceeded时执行重试
- internal:在gRPC应答的Header中状态码是internal时执行重试
- resource-exhausted:在gRPC应答的Header中状态码是resource-exhausted时执行重试
- unavailable:在gRPC应答的Header中状态码是unavailable时执行重试。
设置延迟错误:
virtaulservice/retry/vs-reviews.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - route: 10 - destination: 11 host: reviews 12 subset: v3 13 fault: 14 delay: 15 percentage: 16 value: 100.0 17 fixedDelay: 7s
设置重试
virtaulservice/retry/vs-bookinfo.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - uri: 13 exact: /productpage 14 - uri: 15 prefix: /static 16 - uri: 17 exact: /login 18 - uri: 19 exact: /logout 20 - uri: 21 prefix: /api/v1/products 22 route: 23 - destination: 24 host: productpage 25 subset: v1 26 retries: 27 attempts: 5 28 perTryTimeout: 3s 29 retryOn: 5xx,connect-failure
是否重试其他机子
virtaulservice/retry/vs-bookinfo-retryRemoteLocalities.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - uri: 13 exact: /productpage 14 - uri: 15 prefix: /static 16 - uri: 17 exact: /login 18 - uri: 19 exact: /logout 20 - uri: 21 prefix: /api/v1/products 22 route: 23 - destination: 24 host: productpage 25 subset: v1 26 retries: 27 attempts: 5 28 perTryTimeout: 3s 29 retryOn: 5xx,connect-failure 30 retryRemoteLocalities: true
rewrite
uri
virtaulservice/rewrite/vs-http-rewrite.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - uri: 13 regex: "/m.*k" 14 rewrite: 15 uri: "/productpage" 16 route: 17 - destination: 18 host: productpage 19 port: 20 number: 9080
authority
virtaulservice/rewrite/vs-http-rewrite-authority.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 gateways: 7 - bookinfo-gateway 8 hosts: 9 - '*' 10 http: 11 - match: 12 - uri: 13 regex: "/m.*k" 14 rewrite: 15 uri: "/productpage" 16 authority: bookinfo.com:27941 17 route: 18 - destination: 19 host: productpage 20 port: 21 number: 9080
route
destination
host
virtaulservice/route/vs-reviews-host.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - route: 10 - destination: 11 host: reviews
port
virtaulservice/route/vs-reviews-port.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - route: 10 - destination: 11 host: reviews 12 port: 13 number: 9080
subset
virtaulservice/route/vs-reviews-subset.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - route: 10 - destination: 11 host: reviews 12 subset: v1
headers
request
add
virtaulservice/route/vs-reviews-headers-request-add.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - route: 10 - destination: 11 host: reviews 12 subset: v1 13 headers: 14 request: 15 add: 16 test: test 17
remove
virtaulservice/route/vs-reviews-headers-request-remove.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - route: 10 - destination: 11 host: reviews 12 subset: v1 13 headers: 14 request: 15 remove: 16 - test 17
set
virtaulservice/route/vs-reviews-headers-request-set.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - route: 10 - destination: 11 host: reviews 12 subset: v1 13 headers: 14 request: 15 set: 16 test: test 17
response
add
virtaulservice/route/vs-bookinfo-headers-response-add.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - bookinfo-gateway 10 http: 11 - match: 12 - uri: 13 exact: /productpage 14 - uri: 15 prefix: /static 16 - uri: 17 exact: /login 18 - uri: 19 exact: /logout 20 - uri: 21 prefix: /api/v1/products 22 route: 23 - destination: 24 host: productpage.istio.svc.cluster.local 25 port: 26 number: 9080 27 headers: 28 response: 29 add: 30 test: test
remove
virtaulservice/route/vs-bookinfo-headers-response-remove.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - bookinfo-gateway 10 http: 11 - match: 12 - uri: 13 exact: /productpage 14 - uri: 15 prefix: /static 16 - uri: 17 exact: /login 18 - uri: 19 exact: /logout 20 - uri: 21 prefix: /api/v1/products 22 route: 23 - destination: 24 host: productpage.istio.svc.cluster.local 25 port: 26 number: 9080 27 headers: 28 response: 29 remove: 30 - x-envoy-upstream-service-time
set
virtaulservice/route/vs-bookinfo-headers-response-set.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - bookinfo-gateway 10 http: 11 - match: 12 - uri: 13 exact: /productpage 14 - uri: 15 prefix: /static 16 - uri: 17 exact: /login 18 - uri: 19 exact: /logout 20 - uri: 21 prefix: /api/v1/products 22 route: 23 - destination: 24 host: productpage.istio.svc.cluster.local 25 port: 26 number: 9080 27 headers: 28 response: 29 set: 30 content-type: "text/html" 31 test: test 32 x-envoy-upstream-service-time: "1111" 33~ 34
weight
virtaulservice/route/vs-reviews-weight.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: reviews 5spec: 6 hosts: 7 - reviews 8 http: 9 - route: 10 - destination: 11 host: reviews 12 subset: v1 13 weight: 50 14 - destination: 15 host: reviews 16 subset: v3 17 weight: 50 18
timeout
virtaulservice/timeout/vs-http-timeout.yaml
1apiVersion: networking.istio.io/v1beta1 2kind: VirtualService 3metadata: 4 name: bookinfo 5spec: 6 exportTo: 7 - '*' 8 gateways: 9 - bookinfo-gateway 10 hosts: 11 - '*' 12 http: 13 - match: 14 - uri: 15 exact: /productpage 16 - uri: 17 prefix: /static 18 - uri: 19 exact: /login 20 - uri: 21 exact: /logout 22 - uri: 23 prefix: /api/v1/products 24 route: 25 - destination: 26 host: productpage 27 port: 28 number: 9080 29 timeout: 0.01s 30
tls
一个有序列表,对应的是透传 TLS 和 HTTPS 流量。路由过程通常利用 ClientHello 消息中的 SNI 来完成。TLS 路由通常应用在 https-、tls- 前缀的平台服务端口,或者经 Gateway 透传的 HTTPS、TLS 协议端口,以及使用 HTTPS 或者 TLS 协议的 ServiceEntry 端口上。注意:没有关联 VirtualService 的 https- 或者 tls- 端口流量会被视为透传 TCP 流量。
1创建证书
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj ‘/O=example Inc./CN=example.com’ -keyout example.com.key -out example.com.crt
openssl req -out nginx.example.com.csr -newkey rsa:2048 -nodes -keyout nginx.example.com.key -subj “/CN=nginx.example.com/O=some organization”
openssl x509 -req -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 0 -in nginx.example.com.csr -out nginx.example.com.crt
2创建secret
kubectl create secret tls nginx-server-certs --key nginx.example.com.key --cert nginx.example.com.crt -n istio
3创建nginx配置文件
1events { 2} 3 4http { 5 log_format main '$remote_addr - $remote_user [$time_local] $status ' 6 '"$request" $body_bytes_sent "$http_referer" ' 7 '"$http_user_agent" "$http_x_forwarded_for"'; 8 access_log /var/log/nginx/access.log main; 9 error_log /var/log/nginx/error.log; 10 11 server { 12 listen 443 ssl; 13 14 root /usr/share/nginx/html; 15 index index.html; 16 17 server_name nginx.example.com; 18 ssl_certificate /etc/nginx-server-certs/tls.crt; 19 ssl_certificate_key /etc/nginx-server-certs/tls.key; 20 } 21} 22
kubectl create configmap nginx-configmap --from-file=nginx.conf=./nginx.conf -nistio
4创建deploy
1apiVersion: v1 2kind: Service 3metadata: 4 name: my-nginx 5 labels: 6 run: my-nginx 7spec: 8 ports: 9 - port: 443 10 protocol: TCP 11 selector: 12 run: my-nginx 13--- 14apiVersion: apps/v1 15kind: Deployment 16metadata: 17 name: my-nginx 18spec: 19 selector: 20 matchLabels: 21 run: my-nginx 22 replicas: 1 23 template: 24 metadata: 25 labels: 26 run: my-nginx 27 spec: 28 containers: 29 - name: my-nginx 30 image: nginx 31 ports: 32 - containerPort: 443 33 volumeMounts: 34 - name: nginx-config 35 mountPath: /etc/nginx 36 readOnly: true 37 - name: nginx-server-certs 38 mountPath: /etc/nginx-server-certs 39 readOnly: true 40 volumes: 41 - name: nginx-config 42 configMap: 43 name: nginx-configmap 44 - name: nginx-server-certs 45 secret: 46 secretName: nginx-server-certs
5创建gateway
1apiVersion: networking.istio.io/v1beta1 2kind: Gateway 3metadata: 4 name: bookinfo-gateway 5spec: 6 selector: 7 istio: ingressgateway 8 servers: 9 - port: 10 number: 443 11 name: https-443 12 protocol: HTTPS 13 hosts: 14 - "nginx.example.com" 15 tls: 16 mode: PASSTHROUGH
6创建vs
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: nginx 5spec: 6 hosts: 7 - nginx.example.com 8 gateways: 9 - bookinfo-gateway 10 tls: 11 - match: 12 - port: 443 13 sniHosts: 14 - nginx.example.com 15 route: 16 - destination: 17 host: my-nginx 18 port: 19 number: 443
7访问url
https://nginx.example.com:39329/
match
destinationSubnets
1.7.0/virtaulservice/tls/vs-nginx-destinationSubnets.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: nginx 5spec: 6 hosts: 7 - nginx.example.com 8 gateways: 9 - bookinfo-gateway 10 tls: 11 - match: 12 - port: 443 13 sniHosts: 14 - nginx.example.com 15 destinationSubnets: 16 - 172.20.1.78/32 17 route: 18 - destination: 19 host: my-nginx 20 port: 21 number: 443 22
gateways
1.7.0/virtaulservice/tls/vs-nginx-gateways.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: nginx 5spec: 6 hosts: 7 - nginx.example.com 8 gateways: 9 - bookinfo-gateway 10 tls: 11 - match: 12 - port: 443 13 sniHosts: 14 - nginx.example.com 15 gateways: 16 - bookinfo-gateway 17 route: 18 - destination: 19 host: my-nginx 20 port: 21 number: 443 22
sourceLabels
1.7.0/virtaulservice/tls/vs-nginx-sourceLabels.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: nginx 5spec: 6 hosts: 7 - nginx.example.com 8 gateways: 9 - bookinfo-gateway 10 tls: 11 - match: 12 - port: 443 13 sniHosts: 14 - nginx.example.com 15 sourceLabels: 16 istio: ingressgateway 17 route: 18 - destination: 19 host: my-nginx 20 port: 21 number: 443
sourceNamespace
1.7.0/virtaulservice/tls/vs-nginx-sourceNamespace.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: nginx 5spec: 6 hosts: 7 - nginx.example.com 8 gateways: 9 - bookinfo-gateway 10 tls: 11 - match: 12 - port: 443 13 sniHosts: 14 - nginx.example.com 15 sourceNamespace: istio-system 16 route: 17 - destination: 18 host: my-nginx 19 port: 20 number: 443
tcp
一个针对透传 TCP 流量的有序路由列表。TCP 路由对所有 HTTP 和 TLS 之外的端口生效。进入流量会使用匹配到的第一条规则。
match
port
1部署deploy
kubectl apply -f tcp-echo-services.yaml -n istio
tcp-echo-services.yaml
1apiVersion: v1 2kind: Service 3metadata: 4 name: tcp-echo 5 labels: 6 app: tcp-echo 7spec: 8 ports: 9 - name: tcp 10 port: 9000 11 - name: tcp-other 12 port: 9001 13 # Port 9002 is omitted intentionally for testing the pass through filter chain. 14 selector: 15 app: tcp-echo 16--- 17apiVersion: apps/v1 18kind: Deployment 19metadata: 20 name: tcp-echo-v1 21spec: 22 replicas: 1 23 selector: 24 matchLabels: 25 app: tcp-echo 26 version: v1 27 template: 28 metadata: 29 labels: 30 app: tcp-echo 31 version: v1 32 spec: 33 containers: 34 - name: tcp-echo 35 image: docker.io/istio/tcp-echo-server:1.2 36 imagePullPolicy: IfNotPresent 37 args: [ "9000,9001,9002", "one" ] 38 ports: 39 - containerPort: 9000 40 - containerPort: 9001 41--- 42apiVersion: apps/v1 43kind: Deployment 44metadata: 45 name: tcp-echo-v2 46spec: 47 replicas: 1 48 selector: 49 matchLabels: 50 app: tcp-echo 51 version: v2 52 template: 53 metadata: 54 labels: 55 app: tcp-echo 56 version: v2 57 spec: 58 containers: 59 - name: tcp-echo 60 image: docker.io/istio/tcp-echo-server:1.2 61 imagePullPolicy: IfNotPresent 62 args: [ "9000,9001,9002", "two" ] 63 ports: 64 - containerPort: 9000 65 - containerPort: 9001
2添加service 端口
kubectl edit svc istio-ingressgateway -n istio-system
1 - name: tcp 2 port: 31400 3 protocol: TCP 4 targetPort: 31400 5
3 创建资源
kubectl apply -f tcp-echo-all-v1.yaml -n istio
tcp-echo-all-v1.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: Gateway 3metadata: 4 name: tcp-echo-gateway 5spec: 6 selector: 7 istio: ingressgateway 8 servers: 9 - port: 10 number: 31400 11 name: tcp 12 protocol: TCP 13 hosts: 14 - "*" 15--- 16apiVersion: networking.istio.io/v1alpha3 17kind: DestinationRule 18metadata: 19 name: tcp-echo-destination 20spec: 21 host: tcp-echo 22 subsets: 23 - name: v1 24 labels: 25 version: v1 26 - name: v2 27 labels: 28 version: v2 29--- 30apiVersion: networking.istio.io/v1alpha3 31kind: VirtualService 32metadata: 33 name: tcp-echo 34spec: 35 hosts: 36 - "*" 37 gateways: 38 - tcp-echo-gateway 39 tcp: 40 - match: 41 - port: 31400 42 route: 43 - destination: 44 host: tcp-echo 45 port: 46 number: 9000 47 subset: v1 48
4访问
telnet 192.168.198.154 37048
destinationSubnets
virtaulservice/tcp/vs-destinationSubnets.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: tcp-echo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - tcp-echo-gateway 10 tcp: 11 - match: 12 - destinationSubnets: 13 - 172.20.2.0/24 14 route: 15 - destination: 16 host: tcp-echo 17 port: 18 number: 9000 19 subset: v2
sourceSubnet
virtaulservice/tcp/vs-sourceSubnet.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: tcp-echo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - tcp-echo-gateway 10 tcp: 11 - match: 12 - sourceSubnet: 172.20.1.24 13 route: 14 - destination: 15 host: tcp-echo 16 port: 17 number: 9000 18 subset: v2
sourceLabels
virtaulservice/tcp/vs-sourceLabels.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: tcp-echo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - tcp-echo-gateway 10 tcp: 11 - match: 12 - sourceLabels: 13 app: istio-ingressgateway 14 route: 15 - destination: 16 host: tcp-echo 17 port: 18 number: 9000 19 subset: v2
sourceNamespace
virtaulservice/tcp/vs-sourceNamespace.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: tcp-echo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - tcp-echo-gateway 10 tcp: 11 - match: 12 - sourceNamespace: istio-system 13 route: 14 - destination: 15 host: tcp-echo 16 port: 17 number: 9000 18 subset: v2
gateways
virtaulservice/tcp/vs-gateways.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: tcp-echo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - tcp-echo-gateway 10 tcp: 11 - match: 12 - gateways: 13 - tcp-echo-gateway 14 route: 15 - destination: 16 host: tcp-echo 17 port: 18 number: 9000 19 subset: v2 20
route
destination
host
virtaulservice/tcp/vs-route-host.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: tcp-echo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - tcp-echo-gateway 10 tcp: 11 - route: 12 - destination: 13 host: tcp-echo 14 port: 15 number: 9000 16
port
virtaulservice/tcp/vs-route-port.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: tcp-echo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - tcp-echo-gateway 10 tcp: 11 - route: 12 - destination: 13 host: tcp-echo 14 port: 15 number: 9000
subset
virtaulservice/tcp/vs-route-subset.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: tcp-echo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - tcp-echo-gateway 10 tcp: 11 - route: 12 - destination: 13 host: tcp-echo 14 subset: v2 15 port: 16 number: 9000
weight
virtaulservice/tcp/tcp-echo-20-v2.yaml
1apiVersion: networking.istio.io/v1alpha3 2kind: VirtualService 3metadata: 4 name: tcp-echo 5spec: 6 hosts: 7 - "*" 8 gateways: 9 - tcp-echo-gateway 10 tcp: 11 - match: 12 - port: 31400 13 route: 14 - destination: 15 host: tcp-echo 16 port: 17 number: 9000 18 subset: v1 19 weight: 80 20 - destination: 21 host: tcp-echo 22 port: 23 number: 9000 24 subset: v2 25 weight: 20
三种协议路由规则对比
VirtualService 在http、tls、tcp这三个字段上分别定义了应用于HTTP、TLS和TCP三种协议的路由规则。从规则构成上都是先定义一组匹配条件,然后对满足条件的的流量执行对应的操作。因为协议的内容不同,路由匹配条件不同,所以执行的操作也不同。如下表所示对比了三种路由规则。从各个维度来看,HTTP路由规则的内容最丰富,TCP路由规则的内容最少,这也符合协议分层的设计。
