概述
本文概述了Istio中流量管理的来源服务路由和url重定向。
根据来源服务进行路由
来自不同版本的服务访问不同版本的目标服务,v1版本的 sleep flaskapp 发出的请求由flaskapp v1版本提供响应,其他版本由flaskapp v2版本负责。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: flaskapp
spec:
hosts:
- flaskapp
http:
- match:
- sourceLabels:
app: sleep
version: v1
route:
- destination:
host: flaskapp
subset: v1
- route:
- destination:
host: flaskapp
subset: v2
进入容器测试:oc rsh sleep-v1-56f9b9db86-lc6xp
for i in `seq 10`;do http --body http://flaskapp/env/version;done
v1
v1
v1
v1
v1
v1
v1
v1
v1
v1
进入容器测试:oc rsh sleep-v2-d596c4dc6-6hv7j
for i in `seq 10`;do http --body http://flaskapp/env/version;done
v2
v2
v2
v2
v2
v2
v2
v2
v2
v2
URI 重定向
还有另外一种对目标路由的分流方式,即根据 URL 进行重定向:
sleep服务发起对flaskapp服务/env/HOSTNAME路径的请求
- 如果请求来自版本v2,那么将其照实返回
- 如果请求来自版本v1,那么将其请求路径由/env/HOSTNAME修改为/env/version
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: flaskapp
spec:
hosts:
- flaskapp
http:
- match:
- sourceLabels:
app: sleep
version: v1
uri:
exact: "/env/HOSTNAME"
redirect:
uri: /env/version
- route:
- destination:
host: flaskapp
subset: v2
v1测试结果:oc rsh sleep-v1-56f9b9db86-lc6xp
env|grep HOSTNAME
HOSTNAME=sleep-v1-56f9b9db86-lc6xp
# http http://flaskapp/env/HOSTNAME
HTTP/1.1 301 Moved Permanently
content-length: 0
date: Tue, 16 Jun 2020 02:32:03 GMT
location: http://flaskapp/env/version
server: envoy
发现返回了301指令,在命令中加入跟随重定向的指令:
# http --follow http://flaskapp/env/HOSTNAME
HTTP/1.1 200 OK
content-length: 2
content-type: text/html; charset=utf-8
date: Tue, 16 Jun 2020 02:36:48 GMT
server: envoy
x-envoy-upstream-service-time: 4
v2
v2测试结果:oc rsh sleep-v2-d596c4dc6-6hv7j
# http http://flaskapp/env/HOSTNAME
HTTP/1.1 200 OK
content-length: 28
content-type: text/html; charset=utf-8
date: Tue, 16 Jun 2020 03:15:21 GMT
server: envoy
x-envoy-upstream-service-time: 6
flaskapp-v2-7d7f85c4bd-27vml
redirect 令会把URI进行整体替换,因此灵活性不高;另外301无法支持
Post方法。以下是httpbin设置一个使用Post 方法的重定向:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin
spec:
hosts:
- httpbin
http:
- match:
- uri:
exact: "/get"
redirect:
uri: /post
- route:
- destination:
host: httpbin
测试结果:
# http -f POST http://httpbin:8000/post data=nothing
HTTP/1.1 200 OK
access-control-allow-credentials: true
access-control-allow-origin: *
content-length: 586
content-type: application/json
date: Tue, 16 Jun 2020 07:25:51 GMT
server: envoy
x-envoy-upstream-service-time: 11
{
"args": {},
"data": "",
"files": {},
"form": {
"data": "nothing"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "12",
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"Host": "httpbin:8000",
"User-Agent": "HTTPie/2.1.0",
"X-B3-Parentspanid": "2813ee1b795f16c1",
"X-B3-Sampled": "0",
"X-B3-Spanid": "1787038c7b48edf1",
"X-B3-Traceid": "a0bbfba3e48bb8162813ee1b795f16c1"
},
"json": null,
"origin": "127.0.0.1",
"url": "http://httpbin:8000/post"
}
rewrite 方法和 redirect 方法的不同之处在于,在 rewrite 方法的 match 一节必须
含对目标的定义。并且, rewrite 方法不能和 redirect 方法共存。
参考
深入浅出Istio:Service Mesh快速入门与实践