istio-http流量管理(2)
作者:互联网
参考
4. 金丝雀部署
金丝雀部署:在发布新版本时,部署的新版本并不对外开放,而选择一小部分用户为测试目标,这部分用户对服务的访问会指向特定的版本,通过对这些金丝雀用户的使用情况的观察,来确定新版本服务的发布效果,在确定结果之前,所有其他用户都继续使用原有版本。
4.1 金丝雀部署
cat flaskapp-default-vs-canary.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: flaskapp
spec:
hosts:
- flaskapp.default.svc.cluster.local
http:
- match:
- headers:
lab:
exact: canary
route:
- destination:
host: flaskapp.default.svc.cluster.local
subset: v2
- route:
- destination:
host: flaskapp.default.svc.cluster.local
subset: v1
注意点:
- match 字段提供来丰富的匹配功能,匹配范围不仅包括
http header
, 还包含uri
,scheme
,method
,authority
,端口
,来源标签
及gateway
等; - exact 代表完全匹配,另有 prefix 代表前缀, regex 代表正则表达式的匹配方式
4.2 验证
bash-4.4# for i in `seq 10` ; do http --body http://flaskapp.default/env/version lab:canary ; done
5. 根据来源服务进行路由
cat flaskapp-default-vs-src.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: flaskapp
spec:
hosts:
- flaskapp.default.svc.cluster.local
http:
- match:
- sourceLabels:
app: sleep
version: v1
route:
- destination:
host: flaskapp.default.svc.cluster.local
subset: v1
- route:
- destination:
host: flaskapp.default.svc.cluster.local
subset: v2
# 应用
kubectl apply -f flaskapp-default-vs-src.yaml
# 验证
# sleep v1
kubectl exec -it -c sleep $(kubectl get pod -l app=sleep,version=v1 -o jsonpath='{.items..metadata.name}') /bin/bash
bash-4.4# for i in `seq 10` ; do http --body http://flaskapp.default/env/version; done
# sleep v2
kubectl exec -it -c sleep $(kubectl get pod -l app=sleep,version=v2 -o jsonpath='{.items..metadata.name}') /bin/bash
bash-4.4# for i in `seq 10` ; do http --body http://flaskapp.default/env/version; done
6. 对URI进行重定向
6.1 redirect
cat flaskapp-default-vs-uri.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: flaskapp
spec:
hosts:
- flaskapp.default.svc.cluster.local
http:
- match:
- sourceLabels:
app: sleep
version: v1
uri:
exact: "/env/HOSTNAME"
redirect:
uri: "/env/version"
- route:
- destination:
host: flaskapp.default.svc.cluster.local
subset: v2
# 应用
kubectl apply -f flaskapp-default-vs-uri.yaml
# 验证 sleep v2,正常返回
kubectl exec -it -c sleep $(kubectl get pod -l app=sleep,version=v2 -o jsonpath='{.items..metadata.name}') -- \
http http://flaskapp.default/env/HOSTNAME
HTTP/1.1 200 OK
content-length: 28
content-type: text/html; charset=utf-8
date: Wed, 12 Jun 2019 08:05:25 GMT
server: envoy
x-envoy-upstream-service-time: 1
flaskapp-v2-59b5b6cb94-jdfp4
# 验证 sleep v1
# 返回 "301" 重定向代码
kubectl exec -it -c sleep $(kubectl get pod -l app=sleep,version=v1 -o jsonpath='{.items..metadata.name}') -- \
http http://flaskapp.default/env/HOSTNAME
HTTP/1.1 301 Moved Permanently
content-length: 0
date: Wed, 12 Jun 2019 08:06:17 GMT
location: http://flaskapp.default/ev/version
server: envoy
# "--follow" 跟随重定向指令,重验证
kubectl exec -it -c sleep $(kubectl get pod -l app=sleep,version=v1 -o jsonpath='{.items..metadata.name}') -- \
http --follow http://flaskapp.default/env/HOSTNAME
HTTP/1.1 200 OK
content-length: 2
content-type: text/html; charset=utf-8
date: Wed, 12 Jun 2019 08:09:26 GMT
server: envoy
x-envoy-upstream-service-time: 2
v2
注意点:
redirect
指令会把 URI 整体替换,灵活性不高;301
指令无法支持Post
方法, istio 提供了rewrite
方式来提供这种在调用前进行 URI 重写的支持。
6.2 rewrite
- yaml
cat httpbin-default-vs.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin
spec:
hosts:
- httpbin.default.svc.cluster.local
http:
- match:
- uri:
exact: "/get"
rewrite:
uri: "/post"
route:
- destination:
host: httpbin.default.svc.cluster.local
- route:
- destination:
host: httpbin.default.svc.cluster.local
- 测试
# 访问测试
kubectl exec -it -c sleep $(kubectl get pod -l app=sleep,version=v2 -o jsonpath='{.items..metadata.name}') -- http -f POST http://httpbin:8000/get data=nothing
# 返回信息
HTTP/1.1 405 Method Not Allowed
access-control-allow-credentials: true
access-control-allow-origin: *
allow: GET, OPTIONS, HEAD
content-length: 178
content-type: text/html
date: Wed, 12 Jun 2019 12:39:28 GMT
server: envoy
x-envoy-upstream-service-time: 5
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>
- 注意点:
rewrite
方法与redirect
方法的不同之处在于: 在rewrite
方法的match
字段必须包含对目标的定义;rewrite
方法与redirect
方法不能共存。
标签:kubectl,http,default,istio,流量,version,sleep,flaskapp 来源: https://www.cnblogs.com/netonline/p/13237722.html