网站首页 > 开源技术 正文
Istio的自定义资源有五十多个,今天先对Network部分的VirtualService的学习做个记录。
介绍
- 首先献上官方文档:https://istio.io/latest/zh/docs/reference/config/networking/virtual-service/
- VirtualService主要是定义了服务的路由规则,如果流量满足了我们设定的匹配规则,则会根据我们的设定将流量发送到服务注册表中的服务/版本
- 样例
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews-route
spec:
# host为必要字段,表示目标主机,尽量使用FQDN来进行服务引用,同个命名空间可以使用服务的短名称
hosts:
- reviews.prod.svc.cluster.local
# HTTP的有序路由规则列表,对httpx,http2x,grpc,都是有效的,匹配规则从上往下
http:
- name: "reviews-v2-routes"
# 匹配规则,有根据uri,header,host,port,queryParams,scheme等
# 匹配规则有:exact精准匹配,prefix前缀匹配,regex正则匹配
match:
- uri:
exact: "/wpcatalog"
- uri:
prefix: "/consumercatalog"
# 重写HTTP URI和Authority标头。重写将在转发之前执行。
rewrite:
uri: "/newcatalog"
# 重定向,重定向不能与重写一起使用
redirect:
uri: /v1/bookRatings
authority: newratings.default.svc.cluster.local
route:
- destination:
host: reviews.prod.svc.cluster.local
subset: v2
# 权重
weight: 80
- name: "reviews-v1-route"
route:
- destination:
host: reviews.prod.svc.cluster.local
subset: v1
# cors
corsPolicy:
allowOrigin:
- example.com
allowMethods:
- POST
- GET
allowCredentials: false
allowHeaders:
- X-Foo-Bar
maxAge: "24h"
# 应用到路由的网关,忽略默认使用默认网关
gateways:
- mygateway
# 透传TLS和HTTPS的流量的路由规则列表
tls:
- match:
- port: 443
sniHosts:
- login.bookinfo.com
route:
- destination:
host: login.prod.svc.cluster.local
# 透传tcp流量的有序路由规则列表,对所有HTTP和TLS之外的端口生效
tcp:
- match:
- port: 27017
route:
- destination:
host: mongo.backup.svc.cluster.local
port:
number: 5555
演示
创建两个对应的http server
- httpd:
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd
labels:
server: httpd
app: web
spec:
replicas: 1
selector:
matchLabels:
server: httpd
app: web
template:
metadata:
labels:
server: httpd
app: web
spec:
containers:
- name: busybox
image: busybox
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "-c", "echo 'this is httpd' > /var/www/index.html; httpd -f -p 8080 -h /var/www"]
---
apiVersion: v1
kind: Service
metadata:
name: httpd-svc
spec:
selector:
server: httpd
ports:
- name: http
port: 8080
targetPort: 8080
protocol: TCP
- tomcat
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat
labels:
server: tomcat
app: web
spec:
replicas: 1
selector:
matchLabels:
server: tomcat
app: web
template:
metadata:
labels:
server: tomcat
app: web
spec:
containers:
- name: tomcat
image: docker.io/kubeguide/tomcat-app:v1
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
name: tomcat-svc
spec:
selector:
server: tomcat
ports:
- name: http
port: 8080
targetPort: 8080
protocol: TCP
- Busy box进入容器中测试
apiVersion: apps/v1
kind: Deployment
metadata:
name: hexiaohong-client
spec:
replicas: 1
selector:
matchLabels:
app: hexiaohong-client
template:
metadata:
labels:
app: hexiaohong-client
spec:
containers:
- name: busybox
image: busybox
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "-c", "sleep 3600"]
---
apiVersion: v1
kind: Service
metadata:
name: web-svc
spec:
selector:
app: hexiaohong-client
ports:
- name: http
port: 8080
targetPort: 8080
protocol: TCP
进入busybox测试
- httpd
- tomcat
创建VirtualService实现流控
创建VirtualService
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: web-svc-vs
spec:
hosts:
- web-svc.default.svc.cluster.local
http:
- route:
- destination:
host: httpd-svc
weight: 80
- destination:
host: tomcat-svc
weight: 20
查看VirtualService:
# kubectl get virtualservices.networking.istio.io
NAME GATEWAYS HOSTS AGE
bookinfo ["bookinfo-gateway"] ["*"] 31h
web-svc-vs ["web-svc.default.svc.cluster.local"] 43s
80%流量到httpd,20%到tomcat
创建VirtualService实现路由
- 创建带匹配条件的VirtualService
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: web-svc-vs
spec:
hosts:
- web-svc.default.svc.cluster.local
http:
- match:
- headers:
to:
exact: httpd
route:
- destination:
host: httpd-svc
- route:
- destination:
host: tomcat-svc
携带header:'to: httpd'将流量导入httpd,否则则到tomcat
样例来自学习九析大佬的教程
猜你喜欢
- 2024-12-24 App Store上的这款新应用可模拟38种不同的复古游戏平台
- 2024-12-24 嘿伙计,把你的头戴式显示器取下来,来感受下迪斯尼的Cave
- 2024-12-24 微软更新Mixed Reality Link 和 App for Quest
- 2024-12-24 一分钟了解 Citrix Virtual Apps and Desktops 架构和端口
- 2024-12-24 打开新视界大门 JINS睛姿可以用APP试戴眼镜啦
- 2024-12-24 专业的教你,如何知道手机上的app有没有随意获取你的隐私
- 2024-12-24 从零开始搭建HTTPS服务 搭建http文件服务器
- 2024-12-24 Facebook 将在 iOS、Android 平台推出虚拟实境 App
- 2024-12-24 如何用Virtual Desktop畅玩PCVR游戏
- 2024-12-24 在 M1/M2 Mac 上,让 Windows 11 免费“跑”起来
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)