首页 > 解决方案 > VirtualService 和 Gateway 中的“hosts”属性是否基于 HTTP 的 Host 标头(第 7 层)?

问题描述

如果我编写如下所示的 Gateway 和 VirtualService 条目,主机属性匹配哪些标准来确定是否应将传入请求路由到服务?是 HTTP 请求中的“主机”标头,还是其他?

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: default-gateway
  namespace: web
spec:
  selector:
    istio: ingressgateway
  servers:
  - hosts:
    - example.com
    port:
      name: www
      number: 80
      protocol: HTTP
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web
  namespace: web
spec:
  gateways:
  - default-gateway
  hosts:
  - example.com
  http:
  - route:
    - destination:
        host: webserver # assume this is the name of the Service for our deployed container
---
# Assume a Service and Deployment exist for the above Service

换一种说法 - 如果我们忽略 DNS - 可以使用哪些“主机”标头通过集群/负载平衡器 IP 访问服务/部署?

标签: kubernetesistio

解决方案


如果您可以使用任何 dns 或集群/负载均衡器 IP 访问集群,则可以将 example.com DNS 更改为 *. 或其他方法将所有 DNS 像列表一样放在hosts块中。

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: default-gateway
  namespace: web
spec:
  selector:
    istio: ingressgateway
  servers:
  - hosts:
    - '*'
    port:
      name: www
      number: 80
      protocol: HTTP
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: web
  namespace: web
spec:
  gateways:
  - default-gateway
  hosts:
  - '*'
  http:
  - route:
    - destination:
        host: webserver # assume this is the name of the Service for our deployed container
---
# Assume a Service and Deployment exist for the above Service

如果您仅使用一个外部 DNS 部署多个服务,则需要使用uri块进行匹配,例如在您的虚拟服务中,您可以放置​​:

apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: web
      namespace: web
    spec:
      gateways:
      - default-gateway
      hosts:
      - '*'
      http:
      - match:
        - uri:
            prefix: /test/service1
        rewrite:
         uri: / 
      - route:
        - destination:
            host: webserver # assume this is the name of the Service for our deployed container

在上面的示例中,您可以从任何主机标头访问,但匹配不同服务的标准是http 匹配中的 uri 块

我希望这个对你有用。:)


推荐阅读