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 # 关联的 Gateway
http:
- match:
- uri:
prefix: / # 匹配所有路径
route:
- destination:
host: nginx # 目标服务
port:
number: 80 # 目标端口
---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: nginx-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "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 端口
- 多个 hosts 字段
在 hosts 字段中列出多个域名或服务名称,适用于不同域名或服务共享相同的路由规则
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: multi-host
spec:
hosts:
- "example.com" # 第一个域名
- "foo.com" # 第二个域名
gateways:
- nginx-gateway # 关联的 Gateway
http:
- match:
- uri:
prefix: /
route:
- destination:
host: nginx # 目标服务
port:
number: 80
hosts 字段包含 example.com 和 foo.com,表示该虚拟服务适用于这两个域名。
所有匹配的流量都会被路由到 nginx 服务。
- 多个 http 路由规则
如果你需要为不同的域名或服务定义不同的路由规则,可以在 http 部分使用多个 match 块。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: multi-host
spec:
hosts:
- "example.com"
- "foo.com"
gateways:
- nginx-gateway
http:
- match:
- authority:
exact: example.com # 匹配 example.com
route:
- destination:
host: nginx
port:
number: 80
- match:
- authority:
exact: foo.com # 匹配 foo.com
route:
- destination:
host: foo-service
port:
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-gateway
http:
- match:
- uri:
prefix: /
route:
- destination:
host: nginx
port:
number: 80
weight: 70 # 70% 的流量
- destination:
host: foo-service
port:
number: 8080
weight: 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-gateway
http:
- match:
- uri:
prefix: /
route:
- destination:
host: nginx
port:
number: 80
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: foo-service
spec:
hosts:
- "foo.com"
gateways:
- nginx-gateway
http:
- match:
- uri:
prefix: /
route:
- destination:
host: foo-service
port:
number: 8080
每个 VirtualService 对应一个独立的服务。
这种方式更清晰,便于管理和维护
6、如果你需要匹配多个子域名,可以使用通配符 *。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: wildcard-host
spec:
hosts:
- "*.example.com" # 匹配所有子域名,如 foo.example.com, bar.example.com
gateways:
- nginx-gateway
http:
- match:
- uri:
prefix: /
route:
- destination:
host: nginx
port:
number: 80
*.example.com 会匹配所有以 example.com 结尾的子域名。
适用于需要统一处理多个子域名的场景。
总结
多个 hosts:适用于多个域名共享相同路由规则。
多个 http 路由规则:适用于为不同域名定义不同的路由规则。
多个 destination:适用于将流量分配到多个目标服务(如灰度发布)。
多个 VirtualService:适用于路由规则差异较大的场景。
通配符域名:适用于统一处理多个子域名。