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: nginxnamespace: test-istio
spec:selector:matchLabels:server: webapp: nginxreplicas: 1template:metadata:labels:server: webapp: nginxspec:containers:- name: nginximage: nginx:1.24-alpineports:- containerPort: 80

nginx应用service

apiVersion: v1
kind: Service
metadata:name: web-svcnamespace: test-istio
spec:ports:- name: portport: 80protocol: TCPtargetPort: 80selector:server: webapp: nginx

httpd的deployment

apiVersion: apps/v1
kind: Deployment
metadata:labels:app: httpdname: httpdnamespace: test-istio
spec:replicas: 1selector:matchLabels:app: httpdstrategy:rollingUpdate:maxSurge: 25%maxUnavailable: 25%type: RollingUpdatetemplate:metadata:labels:app: httpdserver: webspec:containers:- image: httpd:latestname: httpd

httpd应用的service


apiVersion: v1
kind: Service
metadata:name: web-httpdnamespace: test-istio
spec:ports:- name: portport: 80protocol: TCPtargetPort: 80selector:server: webapp: httpd

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

也可以直接用上面的nginx pod


apiVersion: apps/v1
kind: Deployment
metadata:name: curl-clientnamespace: test-istio
spec:selector:matchLabels:app: curl-clientreplicas: 1template:metadata:labels:app: curl-clientspec:containers:- name: testimage: curlimages/curl:latestcommand:- sleep- "36000"

故障注入

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

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

CRD代码


type HTTPFaultInjection struct {state         protoimpl.MessageStatesizeCache     protoimpl.SizeCacheunknownFields 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-vsnamespace: test-istio
spec:hosts:- test.comhttp:- fault:delay:percentage:value: 100fixedDelay: 5sroute:- destination:host: web-svcport: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-vsnamespace: test-istio
spec:hosts:- test.comhttp:- fault:abort:percentage:value: 100httpStatus: 500route:- destination:host: web-svcport:number: 80

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

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

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

超时和重试策略

超时

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


# httpd 的VirtualService
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: web-httpdnamespace: test-istio
spec:hosts:- example123.comhttp:- fault:delay:percentage:value: 100fixedDelay: 5sroute:- destination:host: web-httpdport:number: 80# nginx 的 VirtualService      
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: web-vsnamespace: test-istio
spec:hosts:- test.comhttp:- timeout: 3sroute:- destination:host: web-svcport: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-vsnamespace: test-istio
spec:hosts:- test.comhttp:- retries:attempts: 3perTryTimeout: 1sretryOn: 5xxroute:- destination:host: web-svcport:number: 80

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

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

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


apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: web-httpdnamespace: test-istio
spec:hosts:- example123.comhttp:- fault:abort:percentage:value: 50httpStatus: 503route:- destination:host: web-httpdport:number: 80

监听nginx的日志

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

换个窗口访问一下

重定向和重写

重定向

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

type HTTPRedirect struct {state         protoimpl.MessageStatesizeCache     protoimpl.SizeCacheunknownFields 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_DerivePortRedirectPort 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 wellScheme 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-vsnamespace: test-istio
spec:hosts:- web-svchttp:- 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-vsnamespace: test-istio
spec:hosts:- web-svchttp:- match:- uri:prefix: /home/rewrite:uri: /route:- destination:host: web-svcport:number: 80

进入client请求一下

重写到其他服务

将httpd服务重写到nginx

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: web-vsnamespace: test-istio
spec:hosts:- web-httpdhttp:- match:- uri:prefix: /rewrite:uri: /route:- destination:host: web-svcport:number: 80

请求一下

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

参考:

Istio / Virtual Service

原文

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

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

MATLAB_ESP32有限脉冲响应FIR无限脉冲响应IIR滤波器

要点 ESP32闪烁LED,计时LEDESP32基础控制:温控输出串口监控,LCD事件计数器,SD卡读写,扫描WiFi网络,手机控制LED,经典蓝牙、数字麦克风捕捉音频、使用放大器和喇叭、播放SD卡和闪存MP3文件、立体…

Vuex的mutations和actions区别

Vuex中的mutations用于同步操作,而actions则处理异步操作。以下是它们的具体区别: 同步与异步: Mutations是同步的,这意味着在mutation中的函数执行时,不能包含任何异步操作,如Promise或者setTimeout等。…

solidity编程

一.Solidity 简介 Solidity 是⼀种⽤于编写以太坊虚拟机( EVM )智能合约的 编程语⾔。我认为掌握 Solidity 是参与链上项⽬的必备技 能:区块链项⽬⼤部分是开源的,如果你能读懂代码,就可以 规避很多亏钱项⽬。…

省市区街道/乡镇四级联动vue3

最近优化了一个省.市.区/县、乡镇/街道的四级联动组件,技术栈是element vue3记录一下。 本来是这样的三级联动: 这个三级联动很简单,直接利用el-select组件把地区值带进去就行了,现在要优化成省.市.区/县、乡镇/街道的四级联动&…

GORM框架快速入门

GORM框架 gorm地址 :https://github.com/go-gorm/gorm 目前使用最广泛的一个go语言数据库框架 1、入门 数据库以目前使用最多的mysql为例。 //安装MySQL驱动 go get -u gorm.io/driver/mysql //安装gorm包 go get -u gorm.io/gorm //安装gin go get -u github.c…

Bicycles(变形dijkstra,动态规划思想)

Codeforces Round 918 (Div. 4) G. Bicycles G. Bicycles 题意: 斯拉夫的所有朋友都打算骑自行车从他们住的地方去参加一个聚会。除了斯拉维奇,他们都有一辆自行车。他们可以经过 n n n 个城市。他们都住在城市 1 1 1 ,想去参加位于城市…

Python程序打包成exe可执行文件的常用方法

在Python中,您可以使用一些工具将您的Python程序打包成可执行文件(.exe)。以下是一些常用的工具: PyInstaller: PyInstaller是一个流行的工具,它可以将Python脚本打包成独立的可执行文件,支持Windows、Linux和Mac。您可以使用以下命令安装PyInstaller: pip install pyin…

Python 快速入门篇

本文简介 点赞 关注 收藏 学会了 2024年是AI的元年,AI的爆火不仅推动了科技领域的进步,更让 Python 语言成为了这一变革中的关键角色。 Python 语言简单易懂,语法清晰明了,懂一点英语的都能学得会。很适合在职场摸爬滚打多年…

python 基础绘图函数 实例

简介 在 Python 中,有许多用于绘图的库。以下是一些常用的 Python 绘图库及其基本绘图函数的简要介绍: Matplotlib: matplotlib.pyplot.plot(x, y): 绘制线图。matplotlib.pyplot.scatter(x, y): 绘制散点图。matplotlib.pyplot.bar(x, height): 绘制条…

【STM32 物联网】AT指令与TCP,发送与接收数据

文章目录 前言一、连接TCP服务器1.1 配置Wifi模式1.2 连接路由器1.3 查询ESP8266设备IP地址1.4 连接TCP服务器 二、向服务器接收数据和发送数据2.1 发送数据2.2 接收数据 总结 前言 随着物联网(IoT)技术的迅速发展,越来越多的设备和系统开始…

总结一下linux性能检测和调优手段

1.perf 是 Linux 系统中性能分析工具,用于收集性能相关的信息。它可以用于查看 CPU 使用情况、内存性能、磁盘 I/O 等,以帮助开发者找到性能瓶颈。 以下是一些 perf 常见用法和示例: 1. CPU Profiling a. 查看 CPU 使用率 perf stat -e cpu…

高性能 Kafka 及常见面试题

Kafka 是一种分布式的,基于发布/订阅的消息系统,原本开发自 LinkedIn,用作 LinkedIn 的事件流(Event Stream)和运营数据处理管道(Pipeline)的基础。 基础原理详解可见 Kafka 基本架构及原理 基础…

基于 LVGL 使用 SquareLine Studio 快速设计 UI 界面

目录 简介注册与软件获取工程配置设计 UI导出源码板级验证更多内容 简介 SquareLine Studio 是一款专业的 UI 设计软件,它与 LVGL(Light and Versatile Graphics Library,轻量级通用图形库)紧密集成。LVGL 是一个轻量化的、开源的…

机器视觉运动控制一体机在光伏汇流焊机器人系统的解决方案

一、市场应用背景 汇流焊是光伏太阳能电池板中段加工工艺,其前道工序为串焊,在此环节流程中,需要在多个太阳能电池片表面以平行方式串焊多条焊带,形成电池串。串焊好的多组电池串被有序排列输送到汇流焊接工作台,通过…

Stable Video Diffusion(SVD)视频生成模型发布 1.1版

前言 近日,随着人工智能技术的飞速发展,图像到视频生成技术也迎来了新的突破。特别是Stable Video Diffusion(SVD)模型的最新版本1.1,它为我们带来了从静态图像生成动态视频的全新能力。本文将深入解析SVD 1.1版本的核…

人像背景分割SDK,智能图像处理

美摄科技人像背景分割SDK解决方案:引领企业步入智能图像处理新时代 随着科技的不断进步,图像处理技术已成为许多行业不可或缺的一部分。为了满足企业对于高质量、高效率人像背景分割的需求,美摄科技推出了一款领先的人像背景分割SDK&#xf…

从零开始学习Netty - 学习笔记 -Netty入门-ChannelFuture

5.2.2.Channel Channel 的基本概念 在 Netty 中,Channel 是表示网络传输的开放连接的抽象。它提供了对不同种类网络传输的统一视图,比如 TCP 和 UDP。 Channel 的生命周期 Channel 的生命周期包括创建、激活、连接、读取、写入和关闭等阶段。Netty 中…

创业者必读:跨境ERP搭建实用技巧大揭秘

随着全球化进程不断加快,跨境电商市场蓬勃发展,吸引着越来越多的创业者涉足。然而,跨境业务的复杂性和多样性也给企业管理带来了挑战。在这样的背景下,打造一个适合企业实际需求的ERP系统变得至关重要。ERP定制为跨境业务量身定制…

nvm下载node指定版本后npm不存在

一,项目背景 接手一个老的项目,需要使用旧的node版本,使用nvm下载12.11.0版本后发现npm命令不存在。 二,原因 查找资料发现是8.11以上版本的node版本对应的npm都没法自动安装,需要自己到npm官网( https://registry.…

Flutter(三):Stack、Positioned、屏幕相关尺寸、Navigator路由跳转

页面尺寸 通知栏高度:MediaQuery.of(context).padding.top顶部导航高度:kToolbarHeight底部导航高度:kBottomNavigationBarHeight屏幕宽:MediaQuery.of(context).size.width屏幕高:MediaQuery.of(context).size.height…