k8s笔记之创建Istio Gateway规则

embedded/2024/10/18 18:25:18/

创建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.ppmy.cn/embedded/88924.html

相关文章

vulhub:nginx解析漏洞CVE-2013-4547

此漏洞为文件名逻辑漏洞,该漏洞在上传图片时,修改其16进制编码可使其绕过策略,导致解析为 php。当Nginx 得到一个用户请求时,首先对 url 进行解析,进行正则匹配,如果匹配到以.php后缀结尾的文件名&#xff…

springCloud组件专题(五) --- seata

一.Seata介绍 1. seata是什么 是一款开源的分布式事务解决方案,供了 AT、TCC、SAGA 和 XA 事务模式。 2.分布式事务中的概念 2.1. 二阶段提交 二阶段提交的含义就是将事务的提交分成两个步骤,分别为: 准备阶段:事务协调者询问所…

JAVA基础知识点3 (String 和 StringBuffer 以及 StringBuilder 的特点以及区别)

1,String 和 StringBuffer 以及 StringBuilder 的特点 (1)String的特点:String是final修饰的字符序列是不可改变的, 是字符串常量,一旦初始化就不可以被更改,因此是线程安全的 因为是常量每次对其操作都会…

利用开源可视化报表工具进入流程化办公!

很多客户朋友都希望能实现流程化办公,因为只有这样才能帮助企业顺利降本、增效、提质,利用好企业内部数据资源,打破信息孤岛壁垒,实现高效发展。低代码技术平台、开源可视化报表工具优势功能特点多,是提质高效的办公利…

mac|运行别人的SpringBoot+Vue项目

一、运行vue 1、查看项目版本,在package.json中查看,在dependencies中的vue就是对应的版本 2、查看本机的vue版本:vue -V 3、删除node_modules 、package-lock.json 4、运行npm install 如果卡住不动,可能是因为网络问题&…

[Docker][Docker Container]详细讲解

目录 1.什么是容器?2.容器命令1.docker creatre2.docker run3.docker ps4.docker logs5.docker attach6.docker exec7.docker start8.docker stop9.docker restart10.docker kill11.docker top12.docker stats13.docker container inspect14.docker port15.docker c…

LeeCode Practice Journal | Day31_GA05

56. 合并区间 题目:56. 合并区间 - 力扣(LeetCode) 题解:代码随想录 (programmercarl.com) 思路很清晰,对数组的操作稀烂,细节上也出现很多问题 solution public class Solution {public int[][] Merge(in…

高级java每日一道面试题-2024年8月03日-web篇-forward和redirect有什么区别?

如果有遗漏,评论区告诉我进行补充 面试官: forward和redirect有什么区别? 我回答: 在Java Web开发中,forward和redirect是Servlet容器提供的两种用于页面跳转的技术。它们的主要区别在于客户端感知的方式、URL地址的变化、请求对象的共享等方面。下面详细介绍两…