当前位置: 首页 > news >正文

[单master节点k8s部署]28.Istio流量管理(四)

金丝雀发布实验

部署两个pod,他们分别是canary-v1和canary-v2。

随后进行service的部署:

apiVersion: v1
kind: Service
metadata:name: canary-svc34namespace: default
spec:selector:apply: canaryports:- port: 80protocol: TCPtargetPort: 80

进行gateway和virtualservice以及destinationRule的部署。

apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:name: canary-gateway34
spec:selector:istio: ingressgatewayservers:- port: number: 80name: httpprotocol: HTTPhosts:-  "*"

可以看到上面的service是TCP协议的,而这里的gateway是HTTP协议的。因为service处于传输层,他只关注传输层流量,而istio处于应用层,它可以基于http的协议执行高级的流量管理,比如路由、负载均衡和超时等等。

实际上,virtualService处理的是来自service的流量,因此这里的host名称应该和service名称对应,所以这里的host名称是:canary-svc34.default.svc.cluster.local

piVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:name: canary
spec:gateways:-  canary-gateway34hosts:- "*"http:- route:- destination:host: canary-svc34.default.svc.cluster.localport:number: 80subset: v1weight: 50- destination:host: canary-svc34.default.svc.cluster.localport: number: 80subset: v2weight: 50
---
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:name: canary
spec:host: canary-svc34.default.svc.cluster.localsubsets:- name: v1labels:app: v1- name: v2labels:app: v2

此时查看svc,是一个clusterIP,而这个服务在我们的istio的服务网格里面,所以我们可以通过

gateway、service、VirtualSerivce

Gateway 负责外部流量的入口,它的端口用于监听外部请求(如 80443 或 31400)。这个端口不需要与 Kubernetes Service 的端口匹配。

VirtualService 负责流量路由,它将 Gateway 或其他服务的流量转发到集群内的 Kubernetes  Service。这里的 destination.port.number 必须与 Kubernetes Service 的端口匹配,因为这是服务监听流量的端口。如果你的 Kubernetes Service 是 canary-svc34,它监听的是 80 端口,那么 VirtualService 的 destination.port.number 也应该是 80

virtualService资源清单
headers匹配
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: reviews
spec:hosts:- reviewshttp:- match:- headers:end-user:exact: jason  # 如果请求头中 end-user 的值为 "jason"route:- destination:host: reviewssubset: v2  # 将流量路由到 reviews 服务的 v2 版本- route:- destination:host: reviewssubset: v3  # 其他流量路由到 reviews 服务的 v3 版本

在virtualservice中可以设置很多的规则,比如上面这个例子中,就对用户请求头中的信息进行了匹配。注意这里http是一个层级,下面有两个并列的规则,一个是- match,另一个是 - route。

一般来说http请求头中会包含以下字段,都可以用来设置istio的match规则:

Host: 指定目标服务器的主机名。
User-Agent: 表明请求是由哪个客户端发出的(例如浏览器类型)。
Authorization: 用于携带身份验证信息。
Cookie: 用于传递与会话相关的数据。

但用户也可以自定义http请求头,设置end-user信息,一个可能的格式如下:

fetch('https://example.com/reviews', {method: 'GET',headers: {'end-user': 'jason'  // 手动添加 end-user 请求头}
}).then(response => {// 处理响应
});

那么这样就可以进行流量的分流,只允许他们去review的v2版本。

从内容上讲除了headers匹配,还有uri匹配,queryParams 匹配(参数匹配)、方法匹配(GET/POST),从规则上讲,有prefix匹配、exact匹配和regex匹配(正则匹配)。

prefix匹配
kind: VirtualService
metadata:name: bookinfo
spec:hosts:- bookinfo.comhttp:- match:- uri:prefix: /reviewsroute:- destination:host: reviews- match:- uri:prefix: /ratingsroute:- destination:host: ratings

这里的uri可能格式如下:

GET /ratings/456 HTTP/1.1
Host: bookinfo.com

则可以匹配到/ratings规则,进入host为ratings的服务。

参数匹配
https://bookinfo.com/reviews?sort=asc&filter=popular

在这个 URL 中,/reviews 是路径。?sort=asc&filter=popular 是查询参数部分。sort=asc 是一个查询参数,表示进行升序排序。filter=popular 是另一个查询参数,表示只显示受欢迎的结果。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: bookinfo
spec:hosts:- bookinfo.comhttp:- match:- uri:prefix: /reviewsheaders:User-Agent:exact: "Mozilla/5.0"queryParams:sort:exact: ascroute:- destination:host: reviews- match:- uri:exact: /ratings/123method:exact: GETroute:- destination:host: ratings
正则匹配
- match:- uri:regex: "/reviews/[0-9]+"

这个配置表示:所有 URI 以 /reviews/ 开头,后面跟着一串数字的请求都会匹配规则,比如 /reviews/123、/reviews/456 等。

authority匹配
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: example-virtualservice
spec:hosts:- "*"http:- match:- authority:exact: "api.example.com"route:- destination:host: api-service
scheme匹配
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: example-virtualservice
spec:hosts:- "service.example.com"http:- match:- scheme:exact: "https"route:- destination:host: secure-service
sourceLabel匹配
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: example-virtualservice
spec:hosts:- "service.example.com"http:- match:- sourceLabels:app: "frontend"version: "v1"route:- destination:host: backend-service
端口匹配
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: example-virtualservice
spec:hosts:- "service.example.com"http:- match:- uri:prefix: "/api"port: 8080route:- destination:host: api-serviceport:number: 8080

http://www.lryc.cn/news/453231.html

相关文章:

  • Windows 11 安装配置 Git 教程
  • Go基础学习11-测试工具gomock和monkey的使用
  • PHP基础教程
  • Python或R时偏移算法实现
  • 华为云LTS日志上报至观测云最佳实践
  • Python--加载Hugging Face模型文件异常处理
  • 补码加/减运算的具体示例
  • macOS编译和运行prometheus2.54
  • flume系列之:flume jmx页面导出flume、java进程等全部指标
  • (17)MATLAB使用伽马(gamma)分布生成Nakagami-m分布的方法1
  • NFT 是什么?
  • mysql的学习
  • 微服务之间的相互调用的几种常见实现方式对比
  • FPGA时序分析和约束学习笔记-(1、FPGA基本原理)
  • 华为仓颉语言入门(9):for-in表达式
  • Vue3中使用axios
  • 国创——VR虚拟陪伴
  • 【Android 源码分析】Activity生命周期之onPause
  • ​IAR全面支持国科环宇AS32X系列RISC-V车规MCU
  • Java题集(从入门到精通)04
  • 《西北师范大学学报 (自然科学版)》
  • Oracle SQL语句没有过滤条件,究竟是否会走索引??
  • Java中参数传递:按值还是按引用?
  • Linux忘记root用户密码怎么重设密码
  • 【Web】复现n00bzCTF2024 web题解(全)
  • 仿RabbitMQ实现消息队列客户端
  • CSS | 面试题:你知道几种移动端适配方案?
  • 【web安全】——XSS漏洞
  • JAVA基础语法 Day11
  • 知识图谱入门——7:阶段案例:使用 Protégé、Jupyter Notebook 中的 spaCy 和 Neo4j Desktop 搭建知识图谱