本文的请求通过Gateway进入Istio,并通过VirtualService对不同header的请求分别发给frontend-v1和frontend-v2微服务。
有关Gateway可以查看https://istio.io/docs/reference/config/networking/gateway/
本文需在完成《OpenShift 4 之Service Mesh教程(4)- 访问流量管理》后进行操作。在开始正式操作前需要运行以下命令将运行应用的my-istio-app项目里的内容清空即可。
$ scripts/teardown.sh
- 执行命令,创建frontend-v1和frontend-v2微服务和相关资源。
$ oc apply -f ocp/frontend-v1-deployment.yml
$ oc apply -f ocp/frontend-v2-deployment.yml
$ oc apply -f ocp/frontend-service.yml
$ oc apply -f ocp/frontend-route.yml
$ oc apply -f ocp/backend-v1-deployment.yml
$ oc apply -f ocp/backend-service.yml
- 运行命令,然后在Kiali的控制台查看请求的微服务调用路径。
$ scripts/run-50.sh
3. 执行命令创建Gateway。
$ oc apply -f istio-files/frontend-gateway.yml
注意:该Gateway运行在istio-system, 并通过istio=ingressgateway的label对应到route。
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: frontend-gateway
spec:
selector:
istio: ingressgateway # use istio default controller, in this example, default controller is istio-system
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- '*'
- 执行命令创建Virtual Service和Destination Rule。
$ oc apply -f istio-files/destination-rule-frontend-v1-v2.yml
$ oc apply -f istio-files/virtual-service-frontend-header-foo-bar-to-v1.yml
其中virtual-service-frontend-header-foo-bar-to-v1.yml包括以下基于header的路由条件:
...
- match:
- headers:
foo:
exact: bar
route:
- destination:
host: frontend
subset: v1
...
5.获取gateway访问地址,然后用缺省和指定header访问gateway,确认是流经不同的Frontend 。
$ export GATEWAY_URL=$(oc get route istio-ingressgateway -n istio-system -o jsonpath='{.spec.host}')
$ curl $GATEWAY_URL
Frontend version: v2 => [Backend: http://backend:8080, Response: 200, Body: Backend version:v1,Response:200,Host:backend-v1-6ddf9c7dcf-ppk77, Message: Hello World!!]
$ curl -H foo:bar $GATEWAY_URL
Frontend version: v1 => [Backend: http://backend:8080, Response: 200, Body: Backend version:v1,Response:200,Host:backend-v1-6ddf9c7dcf-ppk77, Message: Hello World!!]
- 在Kiali控制台中查看微服务调用路径。