istio学习记录——VirtualService详解


上一篇使用VirtualService进行了简单的流量控制,并通过Gateway将流量导入到了集群内。这一篇将更加深入的介绍 VirtualService。

k8s中有service,service能够对流量进行负载均衡,那为什么istio又引入了VirtualService呢,因为service的负载均衡只有简单的轮询和会话亲和,istio需要更为细致的流量控制,所以有了VirtualService。

VirtualService特性

流量路由规则:通过 VirtualService,你可以定义一组规则,用于决定如何将请求路由到后端服务。这可以基于多种条件,包括请求的主机名、路径、请求头等

版本控制:VirtualService 允许你指定请求应该路由到哪个后端服务的哪个版本。这对于实现流量的分阶段发布(canary deployment)或蓝绿部署(blue-green deployment)等非常有用。

超时和重试策略:你可以在 VirtualService 中定义超时和重试策略,以控制在请求失败时的行为。这有助于增加服务的可靠性和弹性。

故障注入:Istio 允许你通过 VirtualService 在服务之间注入故障,以测试系统在异常情况下的表现。这对于测试容错性和恢复能力非常有用。

重定向和重写:通过 VirtualService,你可以配置请求的重定向或重写规则。这使得可以对请求进行转发、修改路径或重定向到不同的 URL。

下面将一一演示这些特性的配置,路由规则和版本控制,在前面的文章中有介绍,这里不再重新演示

环境准备

注:test-istio 已经设置了istio的sidecar注入

所有的演示均在test-istio 命名空间进行,因为只有都注入了istio的sidecar,客户端才可以接收到来自 Pilot server 端有关 virtual service 的配置

nginx的deployment

镜像使用: nginx:1.24-alpine版本,标准版没有vi命令,后续操作需要vi修改配置

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: test-istio
spec:
  selector:
    matchLabels:
      server: web
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        server: web
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.24-alpine
        ports:
        - containerPort: 80

nginx应用service

apiVersion: v1
kind: Service
metadata:
  name: web-svc
  namespace: test-istio
spec:
  ports:
  - name: port
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    server: web
    app: nginx

httpd的deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: httpd
  name: httpd
  namespace: test-istio
spec:
  replicas: 1
  selector:
    matchLabels:
      app: httpd
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: httpd
        server: web
    spec:
      containers:
      - image: httpd:latest
        name: httpd

httpd应用的service


apiVersion: v1
kind: Service
metadata:
  name: web-httpd
  namespace: test-istio
spec:
  ports:
  - name: port
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    server: web
    app: httpd

一个curl的client端,用于请求使用

也可以直接用上面的nginx pod


apiVersion: apps/v1
kind: Deployment
metadata:
  name: curl-client
  namespace: test-istio
spec:
  selector:
    matchLabels:
      app: curl-client
  replicas: 1
  template:
    metadata:
      labels:
        app: curl-client
    spec:
      containers:
      - name: test
        image: curlimages/curl:latest
        command:
        - sleep
        - "36000"

故障注入

这里先介绍故障注入【因为后续的超时和重试需要借助故障注入进行演示】

VirtualService支持的故障注入:有延时注入,和中止注入

CRD代码


type HTTPFaultInjection struct {
  state         protoimpl.MessageState
  sizeCache     protoimpl.SizeCache
  unknownFields protoimpl.UnknownFields

  // Delay requests before forwarding, emulating various failures such as
  // network issues, overloaded upstream service, etc.
  Delay *HTTPFaultInjection_Delay `protobuf:"bytes,1,opt,name=delay,proto3" json:"delay,omitempty"`
  // Abort Http request attempts and return error codes back to downstream
  // service, giving the impression that the upstream service is faulty.
  Abort *HTTPFaultInjection_Abort `protobuf:"bytes,2,opt,name=abort,proto3" json:"abort,omitempty"`
}

注入延时

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web-vs
  namespace: test-istio
spec:
  hosts:
  - test.com
  http:
  - fault:
      delay:
        percentage:
          value: 100
        fixedDelay: 5s
    route:
    - destination:
        host: web-svc
        port:
          number: 80

示例表示:100%的请求都将进行延时,延时时间5s

验证延时

kubectl -n test-istio exec -it client-curl sh 
# 执行命令
curl -w '\nTotal time: %{time_total}\n' -s test.com

注入中止

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web-vs
  namespace: test-istio
spec:
  hosts:
  - test.com
  http:
  - fault:
      abort:
        percentage:
          value: 100
        httpStatus: 500
    route:
    - destination:
        host: web-svc
        port:
          number: 80

所有的请求都将请求失败,返回状态码 500

在将返回状态码修改为503试试

如果有兴趣,可以再尝试修改注入故障的百分比试试

超时和重试策略

超时

超时的示例将通过为nginx服务设置超时时间,然后为httpd注入超时时间,通过nginx转发请求到httpd来达到超时的效果【nginx服务需要在设置的超时时间内返回】


# httpd 的VirtualService
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web-httpd
  namespace: test-istio
spec:
  hosts:
  - example123.com
  http:
  - fault:
      delay:
        percentage:
          value: 100
        fixedDelay: 5s
    route:
    - destination:
        host: web-httpd
        port:
          number: 80

 # nginx 的 VirtualService      
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web-vs
  namespace: test-istio
spec:
  hosts:
  - test.com
  http:
  - timeout: 3s
    route:
    - destination:
        host: web-svc
        port:
          number: 80

nginx超时时间 3是,httpd的延时注入 5s

配置之前client请求一下,正常返回

下面进入nginx,修改配置,以达成转发到httpd的需求


vi /etc/nginx/conf.d/default.conf
# 替换为自己的pod
kubectl -n test-istio exec -it nginx-v2-5d65f8f449-kqh5v sh

# 修改 web-httpd 是httpd的service
location / {
      #  root   /usr/share/nginx/html;
      #  index  index.html index.htm;
      proxy_pass http://example123.com;
 }
 # 查看配置是否正确
 nginx -t
 
 # 重载配置
 nginx -s relaod

请求httpd

请求nginx

把超时时间修改为6是,再次尝试 ,请求可以正常返回了

注意:如果你在验证过程中,出现无法解析域名,或者解析域名但是访问test.com 时,返回一些莫名的数据,那么请尝试更换 example123.com 为其他值,在学习时我尝试了很多域名,由于设置了延时导致,代理到了网络中真实的域名,返回了各种网站,

这里也可以直接用service的名称来代替,这样可以不用响应莫名的网站

重试

重试:分为两种情况,1、请求超时,2、服务端响应错误

- retries:
      attempts: 3 # 配置重试次数
      perTryTimeout: 1s # 超时重试时间, 超过1s则重试
      retryOn: 5xx # 重试策略

可配置策略:  x-envoy-retry-on

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web-vs
  namespace: test-istio
spec:
  hosts:
  - test.com
  http:
  - retries:
      attempts: 3
      perTryTimeout: 1s
      retryOn: 5xx
    route:
    - destination:
        host: web-svc
        port:
          number: 80

示例配置重试三次,重试重试时间1s,重试策略所有5xx响应码

4s,本身1s+重试三次 3s

这里还是接着上面的配置进行的,就是nginx转发到httpd,httpd配置了5s延时,下面配置httpd的vs为故障注入——中止50% 


apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web-httpd
  namespace: test-istio
spec:
  hosts:
  - example123.com
  http:
  - fault:
      abort:
        percentage:
          value: 50
        httpStatus: 503
    route:
    - destination:
        host: web-httpd
        port:
          number: 80

监听nginx的日志

 kubectl -n test-istio logs -f nginx-5d65f8f449-kqh5v -c istio-proxy

换个窗口访问一下

重定向和重写

重定向

Redirect 指的是将请求到原目标服务的流量重定向到给另外一个目标服务,客户端请求时不用更改任何方式从而访问到重定向后的目标服务。

type HTTPRedirect struct {
  state         protoimpl.MessageState
  sizeCache     protoimpl.SizeCache
  unknownFields protoimpl.UnknownFields

  // On a redirect, overwrite the Path portion of the URL with this
  // value. Note that the entire path will be replaced, irrespective of the
  // request URI being matched as an exact path or prefix.
  Uri string `protobuf:"bytes,1,opt,name=uri,proto3" json:"uri,omitempty"`
  // On a redirect, overwrite the Authority/Host portion of the URL with
  // this value.
  Authority string `protobuf:"bytes,2,opt,name=authority,proto3" json:"authority,omitempty"`
  // Types that are assignable to RedirectPort:
  //
  //  *HTTPRedirect_Port
  //  *HTTPRedirect_DerivePort
  RedirectPort isHTTPRedirect_RedirectPort `protobuf_oneof:"redirect_port"`
  // On a redirect, overwrite the scheme portion of the URL with this value.
  // For example, `http` or `https`.
  // If unset, the original scheme will be used.
  // If `derivePort` is set to `FROM_PROTOCOL_DEFAULT`, this will impact the port used as well
  Scheme string `protobuf:"bytes,6,opt,name=scheme,proto3" json:"scheme,omitempty"`
  // On a redirect, Specifies the HTTP status code to use in the redirect
  // response. The default response code is MOVED_PERMANENTLY (301).
  RedirectCode uint32 `protobuf:"varint,3,opt,name=redirect_code,json=redirectCode,proto3" json:"redirect_code,omitempty"`
}

重定向的CRD定义,可以看出,支持配置

  • uri : 重定向路径

  • authority:重定向后的host

  • Scheme: 重定向的协议

  • RedirectCode:重定向的响应码

  • RedirectPort:重定向端口

这里将发送到nginx的请求重定向到httpd

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web-vs
  namespace: test-istio
spec:
  hosts:
  - web-svc
  http:
  - match:
    - uri:
        prefix: /
    redirect:
      uri: /
      authority: web-httpd

将向ningx的service 的请求重定向到httpd的service的请求,【这里使用的是前缀匹配,所有物理是访问 web-svc,还是web-svc/123, web-svc/456 都会重定向到 web-httpd】

curl -i参数是打印 响应体, -L参数是跟随 重定向

重写

将请求转发给目标服务前修改HTTP请求中指定部分的内容,目标服务也可以是服务本身【既是只对请求路径进行调整】

重写服务自身的接口路径

进入nginx修改nginx的配置

vi /etc/nginx/conf.d/default.conf

location /home/ {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
   
    }

    location / {
       proxy_pass http://web-httpd;
       proxy_http_version 1.1;
    }

重载nginx配置 nginx -s reload

配置请求路径为 /home/ 前缀时跳转到 / 【根路径代理到了httpd服务】


apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web-vs
  namespace: test-istio
spec:
  hosts:
  - web-svc
  http:
  - match:
    - uri:
        prefix: /home/
    rewrite:
      uri: /
    route:
    - destination:
        host: web-svc
        port:
          number: 80

进入client请求一下

重写到其他服务

将httpd服务重写到nginx

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web-vs
  namespace: test-istio
spec:
  hosts:
  - web-httpd
  http:
  - match:
    - uri:
        prefix: /
    rewrite:
      uri: /
    route:
    - destination:
        host: web-svc
        port:
          number: 80

请求一下

到这里VirtualService的主要功能就演示完成了,后续回继续介绍istio其他相关资源,例如DestinationRule

参考:

Istio / Virtual Service

原文

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

最新IE跳转Edge浏览器解决办法(2024.2.26)

最新IE跳转Edge浏览器解决办法(2024.2.26) 1. IE跳转原因1.1. 原先解决办法1.2. 最新解决办法1.3. 最后 1. IE跳转原因 关于IE跳转问题是由于在2023年2月14日,微软正式告别IE浏览器,导致很多使用Windows10系统的电脑在打开IE浏览…

300分钟吃透分布式缓存-17讲:如何理解、选择并使用Redis的核心数据类型?

Redis 数据类型 首先,来看一下 Redis 的核心数据类型。Redis 有 8 种核心数据类型,分别是 : & string 字符串类型; & list 列表类型; & set 集合类型; & sorted set 有序集合类型&…

多线程与高并发(1)- 线程基础、并发特性、锁、JUC工具

文章目录 第一章、线程基础知识一、基础概念1、什么是程序?2、什么是进程?3、什么是线程?4、什么是线程的切换(Context Switch)?5、单核CPU 设定多线程是否有意义?6、工作线程数是不是设置的越大…

【Linux】部署单机项目(自动化启动)---(图文并茂详细讲解)

目录 一 准备工作 1.1 连接服务器拷贝文件 1.2 解压 二 JDK安装 2.1 配置坏境变量 2.2 查看版本 三 Tomcat(自启动) 3.1 复制启动命令的位置 3.2 添加命令相关配置文件 3.2.1 配置jdk及tomcat目录 3.2.2 添加优先级 3.3 设置自启动命令 3.4 开放端口 四 My…

行业交流 | “建筑工业化—创新型建筑技术的应用”主题沙龙

11月9日下午,由环同济知识经济圈发展推进领导小组办公室指导,上海市杨浦区科委、同济科技园核心园、同济EMBA设计协会联合主办,环同济发展促进会协办的“建筑工业化——创新型建筑技术的应用”主题沙龙在我园区顺利举办。优积建筑科技发展(上…

32单片机基础:TIM定时中断

STM32中功能最强大,结构最复杂的一个外设——定时器 因为定时器的内容很多,所以本大节总共分为4个部分,8小节。 第一部分:主要讲定时器基本的定时功能,也就是定一个时间,然后让定时器每隔这个时间产生一个中断&#…

鸿蒙ArkTs开发WebView问题总结

1. 加载WebView页面报错"Can not read properties of null (reading getltem)" 解决: 在加载webview的controller中加入.domStorageAccess(true) build() {Column() {Row().width(100%).height(50rpx)Web({ src: src, controller: this.controller }).javaScriptAc…

【2.3深度学习开发任务实例】(1)神经网络模型的特点【大厂AI课学习笔记】

从本章开始,我把标题的顺序变了一下,大厂AI课笔记,放到后面。因为我发现App上,标题无法显示完全。 从本章开始,要学习深度学习开发任务的全部过程了。 我们将通过小汽车识别赛道上的标志牌,给出检测框&am…

Leetcoder Day25| 回溯part05:子集+排列

491.递增子序列 给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是2。 示例: 输入:[4, 7, 6, 7]输出: [[4, 6], [4, 7], [4, 6, 7], [6, 7], [7,7], [4,7,7]] 说明: 给定数组的长度不会超过15。数组中的整数范围是 [-100,100]。给定数…

Camtasia 2023 v23.4.2.51146 (x64) 中文激活授权版(附安装教程+激活补丁) 喀秋莎(屏幕录制剪辑) 常用快捷键

目录 功能特性 常用快捷键 一、关于文件设置 二、关于编辑设置 三、关于视图设置 四、关于录制设置 破解说明 Camtasia 2023免费版是一款由TechSmith公司官方进行汉化推出的最新版本,该软件集屏幕录制和视频剪辑功能于一体的软件,提供屏幕录制、区域录…

Maya笔记 设置工作目录

Maya会把素材场景等自动保存在工作目录里,我们可以自己定义工作目录 步骤1 创建workspace.mel文件 文件/设置项目 ——>选择一个文件夹,点击设置——>创建默认工作区 这一个后,可以在文件夹里看到.mel文件 步骤2 自动创建文件夹…

Groovy(第九节) Groovy 之单元测试

JUnit 利用 Java 对 Song 类进行单元测试 默认情况下 Groovy 编译的类属性是私有的,所以不能直接在 Java 中访问它们,必须像下面这样使用 setter: 编写这个测试用例余下的代码就是小菜一碟了。测试用例很好地演示了这样一点:用 Groovy 所做的一切都可以轻易地在 Java 程序…

使用 Debezium 和 RisingWave 对 MongoDB 进行持续分析

MongoDB 和流式 Join 的挑战 谷歌趋势显示,有关 MongoDB 流式计算的搜索率不断上升 作为一种操作型数据库,MongoDB 在提供快速数据操作和查询性能方面表现十分出色。然而,在维护实时视图或执行流处理任务的内置支持方面,它确实存…

uni-app之android原生插件开发

官网 uni小程序SDK 一 插件简介 1.1 当HBuilderX中提供的能力无法满足App功能需求,需要通过使用Andorid/iOS原生开发实现时,可使用App离线SDK开发原生插件来扩展原生能力。 1.2 插件类型有两种,Module模式和Component模式 Module模式&…

51单片机 wifi连接

一、基本概念 ESP8266是一款集成了WiFi功能的高性能芯片,广泛应用于物联网设备、智能家居、传感器网络等领域。以下是ESP8266的详细讲解: 1. 功能特点:ESP8266集成了TCP/IP协议栈,支持STA(Station)和AP&am…

OpenAI划时代大模型——文本生成视频模型Sora作品欣赏(八)

Sora介绍 Sora是一个能以文本描述生成视频的人工智能模型,由美国人工智能研究机构OpenAI开发。 Sora这一名称源于日文“空”(そら sora),即天空之意,以示其无限的创造潜力。其背后的技术是在OpenAI的文本到图像生成模…

虚拟机安装+固定ip地址

一、下载CentOS 二、安装CentOS 1、打开你的VMware Workstation Pro,并点击“创建新的虚拟机” 2、点选典型(推荐)(T),并点击“下一步” 3、点选稍后安装操作系统(S),并点击“下一步” 4、点选Linux,并点击“下一步” 6、点击“…

tomcat下载搭建

环境:centos7 打开环境先测试是否有网 ping www.baidu.com 在使用ifconfig命令查询ip地址 准备工作做好打开tomcat官网Apache Tomcat - Apache Tomcat 8 Software Downloads 找到tomcat8安装 复制链接 打开centos安装wget 进入到 /usr/local目录中 cd /usr/loc…

SpringMVC 学习(八)之文件上传与下载

目录 1 文件上传 2 文件下载 1 文件上传 SpringMVC 对文件的上传做了很好的封装,提供了两种解析器。 CommonsMultipartResolver:兼容性较好,可以兼容 Servlet3.0 之前的版本,但是它依赖了 commons-fileupload …

Linux 基础之 vmstat 命令详解

文章目录 一、前言二、使用说明2.1 vmstat [delay/count/d/D/t/w]2.2.vm模式的字段 一、前言 vmstat(VirtualMeomoryStatistics,虚拟内存统计)是一个不错的 Linux/Unix 监控工具,在性能测试中除了top外也是比较常用的工具之一,它可以监控操作…