5.3 weight
-
部署
demoapp v10
和v11
版本--- apiVersion: apps/v1 kind: Deployment metadata:labels:app: demoappv10version: v1.0name: demoappv10 spec:progressDeadlineSeconds: 600replicas: 3selector:matchLabels:app: demoappversion: v1.0template:metadata:labels:app: demoappversion: v1.0spec:containers:- image: ikubernetes/demoapp:v1.0imagePullPolicy: IfNotPresentname: demoappenv:- name: "PORT"value: "8080"ports:- containerPort: 8080name: webprotocol: TCPresources:limits:cpu: 50m --- apiVersion: apps/v1 kind: Deployment metadata:labels:app: demoappv11version: v1.1name: demoappv11 spec:progressDeadlineSeconds: 600replicas: 2selector:matchLabels:app: demoappversion: v1.1template:metadata:labels:app: demoappversion: v1.1spec:containers:- image: ikubernetes/demoapp:v1.1imagePullPolicy: IfNotPresentname: demoappenv:- name: "PORT"value: "8080"ports:- containerPort: 8080name: webprotocol: TCPresources:limits:cpu: 50m --- apiVersion: v1 kind: Service metadata:name: demoapp spec:ports:- name: httpport: 8080protocol: TCPtargetPort: 8080selector:app: demoapptype: ClusterIP ---
-
定义subset
apiVersion: networking.istio.io/v1beta1 kind: DestinationRule metadata:name: demoapp spec:host: demoappsubsets:- name: v10labels:version: v1.0- name: v11labels:version: v1.1
-
定义基于weight的virtualservice
apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata:name: demoapp spec:hosts:- demoapphttp:- name: weight-based-routingroute:- destination:host: demoappsubset: v10weight: 90- destination:host: demoappsubset: v11weight: 10
-
测试
访问
http://demoapp:8080
kiali查看拓扑
5.4 rewrite
-
定义
demoapp v1.0
和demoapp v1.1
版本和subset
的dr规则。参考weight中定义 -
定义rewrite的virtualservice
apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata:name: demoapp spec:hosts:- demoapphttp:- name: rewritematch:- uri:prefix: /canaryrewrite:uri: /route:- destination:host: demoappsubset: v11- name: defaultroute:- destination:host: demoappsubset: v10
-
测试
访问:
http://demoapp:8080/canary
访问:
http://demoapp:8080
5.5 header
-
定义
demoapp v1.0
和demoapp v1.1
版本和subset
的dr规则。参考weight中定义 -
定义header规则
apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata:name: demoapp spec:hosts:- demoapphttp:- name: canarymatch:- headers:x-canary:exact: "true"route:- destination:host: demoappsubset: v11headers:request:set: User-Agent: Chrome #set字段修改request的value的值response:add:x-canary: "true" # 在reponse中利用add字段进行添加- name: defaultheaders:response:add:X-Envoy: testroute:- destination:host: demoappsubset: v10
-
测试功能
按照定义的header的vs规则:
-
当请求
demoapp:8080
会匹配上default规则,并且在response的header加上x-Envoy: test
的标头/ $ curl -I demoapp:8080 HTTP/1.1 200 OK content-type: text/html; charset=utf-8 content-length: 114 server: envoy date: Tue, 22 Aug 2023 09:14:02 GMT x-envoy-upstream-service-time: 2 x-envoy: test
-
当请求
demoapp:8080
加上x-canary: true
的标头,会在reponse中增加标头,以及会将request的User-Agent: Chrome
修改/ $ curl -I -H "x-canary: true" demoapp:8080 HTTP/1.1 200 OK content-type: text/html; charset=utf-8 content-length: 116 server: envoy date: Tue, 22 Aug 2023 09:18:06 GMT x-envoy-upstream-service-time: 2 x-canary: true
/ $ curl -H "x-canary: true" demoapp:8080/user-agent User-Agent: Chrome / $
-
5.6 fault injection
-
定义
demoapp v1.0
和demoapp v1.1
版本和subset
的dr规则。参考weight中定义 -
定义fault injection的规则
apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata:name: demoapp spec:hosts:- demoapphttp:- name: canarymatch:- uri:prefix: /canaryrewrite:uri: /route:- destination:host: demoappsubset: v11fault: # 故障注入的一种方式abort,会在发往v11版本的20%的流量上注入abort故障,返回的http code为555abort:percentage:value: 20httpStatus: 555- name: defaultroute:- destination:host: demoappsubset: v10fault:delay: # 故障注入的另外一种方式延迟,会在发往v10的版本上20%的流量上注入delay故障,延迟时间为3秒percentage:value: 20fixedDelay: 3s
-
测试
-
当访问
demoapp:8080/canary
的时候,按照上诉定义的规则,会有20%的比例注入abort故障:/ $ curl demoapp:8080/canary iKubernetes demoapp v1.1 !! ClientIP: 127.0.0.6, ServerName: demoappv11-77755cdc65-vxmgv, ServerIP: 172.16.140.87! / $ curl demoapp:8080/canary iKubernetes demoapp v1.1 !! ClientIP: 127.0.0.6, ServerName: demoappv11-77755cdc65-k2xzl, ServerIP: 172.16.196.145! / $ curl demoapp:8080/canary iKubernetes demoapp v1.1 !! ClientIP: 127.0.0.6, ServerName: demoappv11-77755cdc65-k2xzl, ServerIP: 172.16.196.145! / $ curl demoapp:8080/canary iKubernetes demoapp v1.1 !! ClientIP: 127.0.0.6, ServerName: demoappv11-77755cdc65-k2xzl, ServerIP: 172.16.196.145! / $ curl demoapp:8080/canary iKubernetes demoapp v1.1 !! ClientIP: 127.0.0.6, ServerName: demoappv11-77755cdc65-vxmgv, ServerIP: 172.16.140.87! / $ curl demoapp:8080/canary iKubernetes demoapp v1.1 !! ClientIP: 127.0.0.6, ServerName: demoappv11-77755cdc65-vxmgv, ServerIP: 172.16.140.87! / $ curl demoapp:8080/canary iKubernetes demoapp v1.1 !! ClientIP: 127.0.0.6, ServerName: demoappv11-77755cdc65-k2xzl, ServerIP: 172.16.196.145! / $ curl demoapp:8080/canary iKubernetes demoapp v1.1 !! ClientIP: 127.0.0.6, ServerName: demoappv11-77755cdc65-k2xzl, ServerIP: 172.16.196.145! / $ curl demoapp:8080/canary iKubernetes demoapp v1.1 !! ClientIP: 127.0.0.6, ServerName: demoappv11-77755cdc65-k2xzl, ServerIP: 172.16.196.145! / $ curl demoapp:8080/canary iKubernetes demoapp v1.1 !! ClientIP: 127.0.0.6, ServerName: demoappv11-77755cdc65-k2xzl, ServerIP: 172.16.196.145! / $ curl demoapp:8080/canary fault filter abort/ $
-
当访问
demoapp:8080
的时候,按照规则,会有20%的流量注入延迟3秒的故障/ $ curl demoapp:8080 iKubernetes demoapp v1.0 !! ClientIP: 127.0.0.6, ServerName: demoappv10-b5d9576cc-f9vfv, ServerIP: 172.16.140.88! / $ curl demoapp:8080 iKubernetes demoapp v1.0 !! ClientIP: 127.0.0.6, ServerName: demoappv10-b5d9576cc-fglcx, ServerIP: 172.16.140.86! / $ curl demoapp:8080 iKubernetes demoapp v1.0 !! ClientIP: 127.0.0.6, ServerName: demoappv10-b5d9576cc-qfnw5, ServerIP: 172.16.196.146! / $ curl demoapp:8080 iKubernetes demoapp v1.0 !! ClientIP: 127.0.0.6, ServerName: demoappv10-b5d9576cc-fglcx, ServerIP: 172.16.140.86! / $ curl demoapp:8080 iKubernetes demoapp v1.0 !! ClientIP: 127.0.0.6, ServerName: demoappv10-b5d9576cc-f9vfv, ServerIP: 172.16.140.88! / $ curl demoapp:8080 iKubernetes demoapp v1.0 !! ClientIP: 127.0.0.6, ServerName: demoappv10-b5d9576cc-f9vfv, ServerIP: 172.16.140.88! / $ curl demoapp:8080 ^C
-
5.7 retry
-
定义
demoapp v1.0
和demoapp v1.1
版本和subset
的dr规则。参考weight中定义 -
定义
proxy
的deployment
和gateway
以及virtualservice
apiVersion: apps/v1 kind: Deployment metadata:name: proxy spec:progressDeadlineSeconds: 600replicas: 1selector:matchLabels:app: proxytemplate:metadata:labels:app: proxyspec:containers:- env:- name: PROXYURLvalue: http://demoapp:8080image: ikubernetes/proxy:v0.1.1imagePullPolicy: IfNotPresentname: proxyports:- containerPort: 8080name: webprotocol: TCPresources:limits:cpu: 50m --- apiVersion: v1 kind: Service metadata:name: proxy spec:ports:- name: http-80port: 80protocol: TCPtargetPort: 8080selector:app: proxy --- apiVersion: networking.istio.io/v1beta1 kind: Gateway metadata:name: proxy-gatewaynamespace: istio-system # 要指定为ingress gateway pod所在名称空间 spec:selector:app: istio-ingressgatewayservers:- port:number: 80name: httpprotocol: HTTPhosts:- "fe.icloud2native.com" ---
-
在
demoapp
中定义fault
apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata:name: demoapp spec:hosts:- demoapphttp:- name: canarymatch:- uri:prefix: /canaryrewrite:uri: /route:- destination:host: demoappsubset: v11fault: # 故障注入的一种方式abort,会在发往v11版本的20%的流量上注入abort故障,返回的http code为555abort:percentage:value: 50httpStatus: 555- name: defaultroute:- destination:host: demoappsubset: v10fault:delay: # 故障注入的另外一种方式延迟,会在发往v10的版本上20%的流量上注入delay故障,延迟时间为3秒percentage:value: 50fixedDelay: 3s
-
定义
retry
机制apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata:name: proxy spec:hosts:- "fe.icloud2native.com" # 对应于gateways/proxy-gatewaygateways:- istio-system/proxy-gateway # 相关定义仅应用于Ingress Gateway上- mesh # 应用在网格内所有的sidercar上http:- name: defaultroute:- destination:host: proxytimeout: 1sretries:attempts: 5 # 加上第一次请求,在log里面看到的应该是请求了6次。perTryTimeout: 1s # 每次重试超过1s,就会发起第二次重试retryOn: 5xx,connect-failure,refused-stream
-
测试
上述定义的规则是:后端demoapp注入了abort和delay的故障,前端服务
proxy
请求demoapp的时候,在proxy中定义了retry
,当请求后端出现5xx,connect-failure,refused-stream
的时候,会进行retry机制。在前端的log里面看到的应该是请求了6次。
5.8 Traffic mirror
场景:有时候想要用线上真实流量来测试将要上线的服务,这样更能模拟出真实的线上测试效果。所以会将线上真实流量mirror到测试环境。
-
定义
demoapp v1.0
和demoapp v1.1
版本和subset
的dr规则。参考weight中定义 -
定义traffic mirror的virtualservice规则
apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata:name: demoapp spec:hosts:- demoapphttp:- name: traffic-mirrorroute:- destination:host: demoappsubset: v10mirror:host: demoappsubset: v11
-
测试
我们在客户端请求
curl demoapp:8080
,虽然请求到了v10 版本,但是我们在v11版本的pod里面看到了有流量进入。