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

istio入门到精通-2

上部分讲到了hosts[*] 匹配所有的微服务,这部分细化一下

在 Istio 的 VirtualService 配置中,hosts 字段用于指定该虚拟服务适用的
目标主机或域名。如果使用具体的域名(如 example.com),则只有请求的主机
域名与 example.com 匹配时,该虚拟服务才会生效
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: nginx
spec:hosts:- "example.com"  # 仅适用于 example.com 域名gateways:- nginx-gateway  # 关联的 Gatewayhttp:- match:- uri:prefix: /  # 匹配所有路径route:- destination:host: nginx  # 目标服务port:number: 80  # 目标端口
---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:name: nginx-gateway
spec:selector:istio: ingressgatewayservers:- port:number: 80name: httpprotocol: HTTPhosts:- "example.com"  # Gateway 只接受 example.com 的流量

客户端访问 http://example.com 时,流量会被路由到 nginx 服务。
客户端访问 http://foo.com 时,流量不会被处理,因为 foo.com 不匹配 hosts: [“example.com”]

hosts: ["example.com"]**表示该虚拟服务仅适用于 example.com 域名。
如果客户端请求的域名是 example.com,Istio 会应用该虚拟服务的路由规则。
如果客户端请求的域名是 foo.com 或任何其他域名,则不会匹配该虚拟服务。
​**gateways: ["nginx-gateway"]**表示该虚拟服务与名为 nginx-gateway 的 Istio Gateway 关联。
只有通过 nginx-gateway 进入的流量才会被该虚拟服务处理。
​**http 路由规则**match 部分定义了匹配规则。这里使用 uri.prefix: / 表示匹配所有路径。
route 部分定义了路由目标。这里将流量路由到 nginx 服务的 80 端口
  1. ​多个 hosts 字段
    在 hosts 字段中列出多个域名或服务名称,适用于不同域名或服务共享相同的路由规则
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: multi-host
spec:hosts:- "example.com"  # 第一个域名- "foo.com"      # 第二个域名gateways:- nginx-gateway  # 关联的 Gatewayhttp:- match:- uri:prefix: /route:- destination:host: nginx  # 目标服务port:number: 80

hosts 字段包含 example.com 和 foo.com,表示该虚拟服务适用于这两个域名。
所有匹配的流量都会被路由到 nginx 服务。

  1. ​多个 http 路由规则
    如果你需要为不同的域名或服务定义不同的路由规则,可以在 http 部分使用多个 match 块。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: multi-host
spec:hosts:- "example.com"- "foo.com"gateways:- nginx-gatewayhttp:- match:- authority:exact: example.com  # 匹配 example.comroute:- destination:host: nginxport:number: 80- match:- authority:exact: foo.com  # 匹配 foo.comroute:- destination:host: foo-serviceport:number: 8080
解释:使用 authority 字段精确匹配域名。
访问 example.com 的流量会被路由到 nginx 服务。
访问 foo.com 的流量会被路由到 foo-service 服务。3. ​多个 destination 目标
如果你需要将流量路由到多个目标服务,可以在 route 部分列出多个 destination。yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: multi-destination
spec:hosts:- "example.com"gateways:- nginx-gatewayhttp:- match:- uri:prefix: /route:- destination:host: nginxport:number: 80weight: 70  # 70% 的流量- destination:host: foo-serviceport:number: 8080weight: 30  # 30% 的流量```
流量会根据权重分配,70% 的流量路由到 nginx,30% 的流量路由到 foo-service。
适用于灰度发布或负载均衡场景。5、多个 VirtualService
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: example-service
spec:hosts:- "example.com"gateways:- nginx-gatewayhttp:- match:- uri:prefix: /route:- destination:host: nginxport:number: 80
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: foo-service
spec:hosts:- "foo.com"gateways:- nginx-gatewayhttp:- match:- uri:prefix: /route:- destination:host: foo-serviceport:number: 8080每个 VirtualService 对应一个独立的服务。
这种方式更清晰,便于管理和维护   6、如果你需要匹配多个子域名,可以使用通配符 *。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: wildcard-host
spec:hosts:- "*.example.com"  # 匹配所有子域名,如 foo.example.com, bar.example.comgateways:- nginx-gatewayhttp:- match:- uri:prefix: /route:- destination:host: nginxport:number: 80
*.example.com 会匹配所有以 example.com 结尾的子域名。
适用于需要统一处理多个子域名的场景。总结
​多个 hosts:适用于多个域名共享相同路由规则。
​多个 http 路由规则:适用于为不同域名定义不同的路由规则。
​多个 destination:适用于将流量分配到多个目标服务(如灰度发布)。
​多个 VirtualService:适用于路由规则差异较大的场景。
​通配符域名:适用于统一处理多个子域名。
http://www.lryc.cn/news/547527.html

相关文章:

  • 第5章:vuex
  • [Python入门学习记录(小甲鱼)]第5章 列表 元组 字符串
  • Docker 学习(四)——Dockerfile 创建镜像
  • Java多线程与高并发专题——为什么 Map 桶中超过 8 个才转为红黑树?
  • LeetCode hot 100—二叉树的中序遍历
  • 代码随想录算法训练营第35天 | 01背包问题二维、01背包问题一维、416. 分割等和子集
  • 与中国联通技术共建:通过obdiag分析OceanBase DDL中的报错场景
  • IDEA 接入 Deepseek
  • 斗地主小游戏
  • 如何改变怂怂懦弱的气质(2)
  • C# OnnxRuntime部署DAMO-YOLO人头检测
  • 基于GeoTools的GIS专题图自适应边界及高宽等比例生成实践
  • 各种DCC软件使用Datasmith导入UE教程
  • 尚硅谷爬虫note15
  • 云原生系列之本地k8s环境搭建
  • 关于tomcat使用中浏览器打开index.jsp后中文显示不正常是乱码,但英文正常的问题
  • mysql foreign_key_checks
  • 开发环境搭建-06.后端环境搭建-前后端联调-Nginx反向代理和负载均衡概念
  • REST API前端请求和后端接收
  • 道可云人工智能每日资讯|《奇遇三星堆》VR沉浸探索展(淮安站)开展
  • 服务器数据恢复—raid5阵列中硬盘掉线导致上层应用不可用的数据恢复案例
  • 【Pandas】pandas Series swaplevel
  • esp32s3聊天机器人(二)
  • pyside6学习专栏(九):在PySide6中使用PySide6.QtCharts绘制6种不同的图表的示例代码
  • DVI分配器2进4出,2进8出,2进16出,120HZ
  • 迷你世界脚本文字板接口:Graphics
  • 5分钟速览深度学习经典论文 —— attention is all you need
  • Cursor + IDEA 双开极速交互
  • HDFS的设计架构
  • 为wordpress自定义一个留言表单并可以在后台进行管理的实现方法