配置请求路由
默认是轮询模式
第一版本
第二版本
第三版本
1、将所有微服务默认版本设置为v1
istioctl create -f samples/bookinfo/networking/destination-rule-all-mtls.yaml
Created config destination-rule/default/productpage at revision 8446
Created config destination-rule/default/reviews at revision 8447
Created config destination-rule/default/ratings at revision 8448
Created config destination-rule/default/details at revision 8449
istioctl create -f samples/bookinfo/networking/virtual-service-all-v1.yaml
Created config virtual-service/default/productpage at revision 8574
Created config virtual-service/default/reviews at revision 8575
Created config virtual-service/default/ratings at revision 8576
Created config virtual-service/default/details at revision 8577
2、查看创建的路由规则
istioctl get virtualservices -o yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"networking.istio.io/v1alpha3","kind":"VirtualService","metadata":{"annotations":{},"name":"bookinfo","namespace":"default"},"spec":{"gateways":["bookinfo-gateway"],"hosts":["*"],"http":[{"match":[{"uri":{"exact":"/productpage"}},{"uri":{"exact":"/login"}},{"uri":{"exact":"/logout"}},{"uri":{"prefix":"/api/v1/products"}}],"route":[{"destination":{"host":"productpage","port":{"number":9080}}}]}]}}
creationTimestamp: null
name: bookinfo
namespace: default
resourceVersion: "6273"
spec:
gateways:
- bookinfo-gateway
hosts:
- '*'
http:
- match:
- uri:
exact: /productpage
- uri:
exact: /login
- uri:
exact: /logout
- uri:
prefix: /api/v1/products
route:
- destination:
host: productpage
port:
number: 9080
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
creationTimestamp: null
name: details
namespace: default
resourceVersion: "8577"
spec:
hosts:
- details
http:
- route:
- destination:
host: details
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
creationTimestamp: null
name: productpage
namespace: default
resourceVersion: "8574"
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
creationTimestamp: null
name: ratings
namespace: default
resourceVersion: "8576"
spec:
hosts:
- ratings
http:
- route:
- destination:
host: ratings
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
creationTimestamp: null
name: reviews
namespace: default
resourceVersion: "8575"
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
---
3、浏览器中打开http://$GATEWAY_URL/productpage,刷新只显示第一版本
4、为特定用户请求路由到reviews:v2
istioctl replace -f samples/bookinfo/networking/virtual-service-reviews-test-v2.yaml
Updated config virtual-service/default/reviews to revision 9186
5、查看规则
istioctl get virtualservice reviews -o yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
creationTimestamp: null
name: reviews
namespace: default
resourceVersion: "9186"
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: jason
route:
- destination:
host: reviews
subset: v2
- route:
- destination:
host: reviews
subset: v1
---
6、以jason登录可以看到星星
7、原理
在此任务中,您首先使用 Istio 将 100% 的请求流量都路由到了 Bookinfo 服务的 v1 版本。 然后再设置了一条路由规则,该路由规则在 productpage
服务中添加基于请求的 “end-user” 自定义 header 选择性地将特定的流量路由到了 reviews 服务的 v2 版本。
Istio 对 Pod 和服务的要求
要成为服务网格的一部分,Kubernetes 集群中的 Pod 和服务必须满足以下几个要求:
-
需要给端口正确命名:服务端口必须进行命名。端口名称只允许是
<协议>[-<后缀>-]
模式,其中<协议>
部分可选择范围包括http
、http2
、grpc
、mongo
以及redis
,Istio 可以通过对这些协议的支持来提供路由能力。例如name: http2-foo
和name: http
都是有效的端口名,但name: http2foo
就是无效的。如果没有给端口进行命名,或者命名没有使用指定前缀,那么这一端口的流量就会被视为普通 TCP 流量(除非显式的用Protocol: UDP
声明该端口是 UDP 端口)。 -
关联服务:Pod 必须关联到 Kubernetes 服务,如果一个 Pod 属于多个服务,这些服务不能再同一端口上使用不同协议,例如 HTTP 和 TCP。
-
Deployment 应带有
app
以及version
标签:在使用 KubernetesDeployment
进行 Pod 部署的时候,建议显式的为Deployment
加上app
以及version
标签。每个 Deployment 都应该有一个有意义的app
标签和一个用于标识Deployment
版本的version
标签。app
标签在分布式跟踪的过程中会被用来加入上下文信息。Istio 还会用app
和version
标签来给遥测指标数据加入上下文信息。