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

k8s笔记之创建Istio Gateway规则

创建Istio Gateway

  • 背景
  • 如何创建Istio Gateway
  • 规则配置方式
    • rewrite重写路径
    • 直接去除match,默认都转发到一个服务
    • 路由规则多种配置方式实践(即开头的完整版)
  • 涉及的命令补充
  • 注意事项

背景

为什么需要使用到Istio Gateway?充当k8s服务访问的外部流量访问入口,类似nginx一样的作用

如何创建Istio Gateway

1、检查是否已开启istio-ingressgateway服务

servicemesh:
enabled: true # 将“false”更改为“true”。
istio: https://istio.io/latest/docs/setup/additional-setup/customize-installation/components:ingressGateways:- name: istio-ingressgateway enabled: true # 将“false”更改为“true”

2、创建yaml配置文件

touch nginx-gateway.yaml

3、输入配置内容

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:name: mygateway
spec:selector:istio: ingressgateway # use istio default ingress gatewayservers:- port:number: 80name: httpprotocol: HTTPhosts:- forecast.example.com
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: mygateway
spec:hosts:- forecast.example.comgateways:- mygatewayhttp:- match:- uri:prefix: "/nginx/"  # 新路径, prefix 前缀匹配, 满足 /p1 的都要被重写rewrite:uri: "/"    # 老路径route:- destination:host: nginx-79zn9d  # 对应service中的名称,具有负责均衡- match:- uri:prefix: "/tomcat/"  # 新路径, prefix 前缀匹配, 满足 /p1 的都要被重写rewrite:uri: "/"    # 老路径route:- destination:host: tomcat-5tl05n # 对应service中的名称,具有负责均衡- match:- uri:prefix: "/myapp1/"  rewrite:uri: "/"route:- destination:host: my-app1       # 对应service中的名称,具有负责均衡- match:- uri:prefix: "/myapp2/"  rewrite:uri: "/"route:- destination:host: my-app2        # 对应service中的名称,具有负责均衡

4、执行创建,会同时创建gateway和VirtualService

kubectl apply -f nginx-gateway.yaml --namespace=project-demo

5、确定Istio入口ip和port (负载均衡器)

kubectl get svc istio-ingressgateway -n istio-system

6、最后客户端访问前,进行客户端host配置

ip【服务器 istio-ingressgateway的ip】 forecast.example.com

7、更新gateway,先导出->再修改->最后更新

kubectl get gw mygateway  -o yaml -n project-demo > /home/k8s/gateway-update.yaml
kubectl apply -f gateway-update.yaml

8、更新virtualservice

kubectl get virtualservice mygateway  -o yaml -n project-demo > /home/k8s/gatewaySvc-update.yaml
kubectl apply -f gatewaySvc-update.yaml

规则配置方式

rewrite重写路径

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:name: mygateway
spec:selector:istio: ingressgateway # use istio default ingress gatewayservers:- port:number: 80name: httpprotocol: HTTPhosts:- forecast.example.com
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: mygateway
spec:hosts:- forecast.example.comgateways:- mygatewayhttp:- match:- uri:prefix: "/nginx/"  # 新路径, prefix 前缀匹配, 满足 /p1 的都要被重写rewrite:uri: "/"    # 老路径route:- destination:host: nginx-79zn9d

直接去除match,默认都转发到一个服务

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:name: mygateway
spec:selector:istio: ingressgateway # use istio default ingress gatewayservers:- port:number: 80name: httpprotocol: HTTPhosts:- forecast.example.com
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: mygateway
spec:hosts:- forecast.example.comgateways:- mygatewayhttp:- route:- destination:host: nginx-79zn9d

路由规则多种配置方式实践(即开头的完整版)

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:name: mygateway
spec:selector:istio: ingressgateway # use istio default ingress gatewayservers:- port:number: 80name: httpprotocol: HTTPhosts:- forecast.example.com
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: mygateway
spec:hosts:- forecast.example.comgateways:- mygatewayhttp:- match:- uri:prefix: "/nginx/"  # 新路径, prefix 前缀匹配, 满足 /p1 的都要被重写rewrite:uri: "/"    # 老路径route:- destination:host: nginx-79zn9d  # 对应service中的名称,具有负责均衡- match:- uri:prefix: "/tomcat/"  # 新路径, prefix 前缀匹配, 满足 /p1 的都要被重写rewrite:uri: "/"    # 老路径route:- destination:host: tomcat-5tl05n # 对应service中的名称,具有负责均衡- match:- uri:prefix: "/myapp1/"  rewrite:uri: "/"route:- destination:host: my-app1       # 对应service中的名称,具有负责均衡- match:- uri:prefix: "/myapp2/"  rewrite:uri: "/"route:- destination:host: my-app2        # 对应service中的名称,具有负责均衡

涉及的命令补充

#networking.istio.io版本
kubectl api-versions | grep networking.istio.io#确定Istio入口ip和port (负载均衡器)
kubectl get svc istio-ingressgateway -n istio-system#检查有没有在相同的 IP和端口上定义 Kubernetes Ingress 资源
kubectl get ingress --all-namespaces#检查有没有在相同的端口上定义其它 Istio Ingress Gateway
kubectl get gateway --all-namespaces# 查看网关
kubectl get gw -A# 删除网关
-- kubectl delete gw my-gateway -n project-demo# 查看路由规则
kubectl get virtualservices my-VirtualService -n project-demo -o yaml# 删除virtualservice
kubectl delete virtualservice nginx-79zn9d -n project-demo# 更新gateway
kubectl get gw mygateway  -o yaml -n project-demo > /home/k8s/gateway-update.yaml
kubectl apply -f gateway-update.yaml# 更新virtualservice
kubectl get virtualservice mygateway  -o yaml -n project-demo > /home/k8s/gatewaySvc-update.yaml
kubectl apply -f gatewaySvc-update.yaml

注意事项

  • VirtualService中的metadata.name需要跟Gateway中的metadata.name一致
http://www.lryc.cn/news/413745.html

相关文章:

  • NAND行业回归盈利:AI与云存储需求驱动
  • 【限免】频控阵雷达:概念、原理与应用【附MATLAB代码】
  • 从0开始搭建vue + flask 旅游景点数据分析系统( 六):搭建后端flask框架
  • 学习硬件测试04:触摸按键+PWM 驱动蜂鸣器+数码管(P62~P67、P71、P72)
  • JS原型链
  • 《Java初阶数据结构》----5.<二叉树的概念及使用>
  • git查看记录详解
  • 检索增强生成RAG系列10--RAG的实际案例
  • 程序员自我提升的全面指南
  • 【golang】Golang手写元组 tuple | golang tuple
  • golang中struct的tag -简记
  • 分布式领域扩展点设计稿
  • 玩转微信公众号变现:从新手到专家的全攻略
  • JVM: 方法调用
  • 测试面试宝典(四十一)—— 接口自动化的优缺点
  • “火炬科企对接”先进计算产业推进会 | 麒麟信安受邀参加,并签署开源生态合作协议
  • 中文网址导航模版HaoWa1.3.1/模版网站wordpress导航主题
  • 图欧学习资源网创站以来的更新日志(截止至2022.5.6)不完全统计
  • 现代前端架构介绍(第二部分):如何将功能架构分为三层
  • LeetCode Easy|【21. 合并两个有序链表】
  • 大模型的架构参数是指定义模型基本结构和组成的各种参数,这些参数对模型的性能、训练效率和泛化能力具有重要影响。以下是对大模型架构参数的详细介绍
  • 人工智能会越来越闭源——对话东北大学副教授王言治 | Open AGI Forum
  • 【前端】(仅思路)如何在前端实现一个fc手柄,将手机作为游戏手柄设备。
  • 三十种未授权访问漏洞合集
  • 【Golang 面试 - 进阶题】每日 3 题(十五)
  • Java中实现文件上传
  • 一种别样的Unicode Python编码方式,完美转换表情和阿拉伯语
  • 小白的晋升之路
  • WebLogic:CVE-2017-10271[XML反序列化]
  • Day13--JavaWeb学习之Servlet后端渲染界面