【06】VirtualService高级流量功能

5.3 weight

  1. 部署demoapp v10v11版本

    ---
    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
    ---
  2. 定义subset

    apiVersion: networking.istio.io/v1beta1
    kind: DestinationRule
    metadata:name: demoapp
    spec:host: demoappsubsets:- name: v10labels:version: v1.0- name: v11labels:version: v1.1
    
  3. 定义基于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
  4. 测试

    访问http://demoapp:8080

    在这里插入图片描述

    kiali查看拓扑
    在这里插入图片描述

5.4 rewrite

  1. 定义demoapp v1.0demoapp v1.1版本和subset的dr规则。参考weight中定义

  2. 定义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
  3. 测试

    访问:http://demoapp:8080/canary

    在这里插入图片描述

    访问:http://demoapp:8080

    在这里插入图片描述

5.5 header

  1. 定义demoapp v1.0demoapp v1.1版本和subset的dr规则。参考weight中定义

  2. 定义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
  3. 测试功能

    按照定义的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

  1. 定义demoapp v1.0demoapp v1.1版本和subset的dr规则。参考weight中定义

  2. 定义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
    
  3. 测试

    • 当访问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

  1. 定义demoapp v1.0demoapp v1.1版本和subset的dr规则。参考weight中定义

  2. 定义proxydeploymentgateway 以及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"
    ---
    
  3. 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
    
  4. 定义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
    
  5. 测试

    上述定义的规则是:后端demoapp注入了abort和delay的故障,前端服务proxy请求demoapp的时候,在proxy中定义了retry,当请求后端出现5xx,connect-failure,refused-stream的时候,会进行retry机制。在前端的log里面看到的应该是请求了6次。

5.8 Traffic mirror

场景:有时候想要用线上真实流量来测试将要上线的服务,这样更能模拟出真实的线上测试效果。所以会将线上真实流量mirror到测试环境。

  1. 定义demoapp v1.0demoapp v1.1版本和subset的dr规则。参考weight中定义

  2. 定义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
    
  3. 测试

    我们在客户端请求curl demoapp:8080,虽然请求到了v10 版本,但是我们在v11版本的pod里面看到了有流量进入。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/171724.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++编写的多线程自动爬虫程序

以下是一个使用C编写的爬虫程序&#xff0c;用于爬取Python进行多线程跑数据的内容。本示例使用了Python的requests库来发送HTTP请求&#xff0c;并使用cheeseboy的爬虫ipIP库来设置爬虫ip信息。以下是详细代码和步骤&#xff1a; #include <iostream> #include <stri…

MySQL主从延时问题

过线上 MySQL 维护经验的童鞋都知道&#xff0c;主从延迟往往是一个让人头疼不已的问题。 不仅仅是其造成的潜在问题比较严重&#xff0c;而且主从延迟原因的定位尤其考量 DBA 的综合能力&#xff1a;既要熟悉复制的内部原理&#xff0c;又能解读主机层面的资源使用情况&#…

SQL-----STUDENT

【学生信息表】 【宿舍信息表】 【宿舍分配表】 为了相互关联&#xff0c;我们需要在表中添加外键。在宿舍分配表中添加用于关联学生信息表的外键 student_id&#xff0c;以及用于关联宿舍信息表的外键 dormitory_id&#xff1b; sql代码 -- 创建学生信息表 CREATE TABLE st…

详细的完美转发

不要假装努力&#xff0c;结果不会陪你演戏。文章目录 完美转发的使用场景完美转发 完美转发的使用场景 请看下面的这个代码 #include<iostream> using namespace std; void func(int&& t) {cout<<"int&&"<<endl;return; } void…

牛客网刷题笔记231112 最小k位数+二叉树层序遍历+SQL异常邮件概率

算法题牛客网NC119 最小的k个数 题目&#xff1a; 用了一下python列表的便利&#xff0c;不知道在面试时允许用不。当然最简单的方法其实是直接sort()一下取前k位数即可。本次写的思路如下&#xff1a; 用一个最大容量为k的列表存储结果&#xff0c;遍历n个元素&#xff0c;当…

漏洞复现--IP-guard flexpaper RCE

免责声明&#xff1a; 文章中涉及的漏洞均已修复&#xff0c;敏感信息均已做打码处理&#xff0c;文章仅做经验分享用途&#xff0c;切勿当真&#xff0c;未授权的攻击属于非法行为&#xff01;文章中敏感信息均已做多层打马处理。传播、利用本文章所提供的信息而造成的任何直…

绿盟远程安全评估系统 RSAS 使用体验-难用

最近领导让我用公司采购的RSAS对产品进行漏洞扫描&#xff0c;学习并使用了这个软件&#xff0c;体验就是真的很难用。使用遇到问题时&#xff0c;咨询售后服务&#xff0c;机器人需要有公司认证&#xff0c;不能随便问问题&#xff0c;也是无语了。咨询客服&#xff0c;客服回…

TCP与UDP

文章目录 TCP与UDP传输层的作用端口号UDPTCPUDP首部的格式TCP首部格式 TCP与UDP TCP/IP中有两个具有代表性的传输层协议&#xff0c;它们分别是TCP和UDP。TCP提供可靠的通信传输&#xff0c;而UDP则常被用于让广播和细节控制交给应用的通信传输。总之&#xff0c;根据通信的具…

条码管理在WMS仓储管理系统中的应用

在当今快节奏的商业环境中&#xff0c;仓储管理对于企业的运营和成本控制具有重要意义。为了提高管理效率和准确性&#xff0c;越来越多的企业开始采用条码管理WMS系统。本文将介绍这一系统的应用场景、条码引入WMS仓储管理系统的步骤以及其在仓储管理中的应用价值&#xff0c;…

Windows如何正确设置PHP环境变量以在Git Bash中运行命令

1、随便找一个目录&#xff0c;鼠标右键打开git bash here 2、cd的根目录 3、找到php安装目录 4、 在根目录下打开 vim .bash_profile &#xff0c;添加环境变量&#xff0c;php地址根据自己的本地地址而定 PATH$PATH:/d/phpstudy_pro/Extensions/php/php7.3.4nts 添加后保存…

【数据结构】Lambda

⭐ 作者&#xff1a;小胡_不糊涂 &#x1f331; 作者主页&#xff1a;小胡_不糊涂的个人主页 &#x1f4c0; 收录专栏&#xff1a;浅谈数据结构 &#x1f496; 持续更文&#xff0c;关注博主少走弯路&#xff0c;谢谢大家支持 &#x1f496; Lambda表达式 1. 背景1.1 语法1.2 函…

二、Linux用户管理

Linux是一个多用户多任务的操作系统&#xff0c;任何一个要使用系统资源的用户&#xff0c;都必须向系统管理员申请一个账户&#xff0c;然后用这个账户进入系统。 每个Linux用户至少属于一个用户组。 用户家目录home下&#xff0c;有各个用户分别创建的家目录&#xf…