VirtualService 和 Gateway 中的“hosts”属性是否基于 HTTP 的 Host 标头(第 7 层)?

Joh*_*ron 5 kubernetes istio

如果我编写如下所示的 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
Run Code Online (Sandbox Code Playgroud)

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

jt9*_*t97 3

回答你的问题

\n
\n

您是说“hosts”是否基于 HTTP“Host”标头?

\n
\n

是的,它基于 http 主机标头。

\n
\n

根据 istio文档

\n
\n

使用curl访问httpbin服务:

\n
\n
$ curl -s -I -HHost:httpbin.example.com "http://$INGRESS_HOST:$INGRESS_PORT/status/200"\nHTTP/1.1 200 OK\nserver: istio-envoy\n...\n
Run Code Online (Sandbox Code Playgroud)\n
\n

请注意,您使用 -H 标志将主机 HTTP 标头设置为 \xe2\x80\x9chttpbin.example.com\xe2\x80\x9d。这是必需的,因为您的入口网关配置为处理 \xe2\x80\x9chttpbin.example.com\xe2\x80\x9d,但在您的测试环境中,您没有该主机的 DNS 绑定,只是将请求发送到入口知识产权。

\n
\n
\n

我使用bookinfo 应用程序进行测试。

\n
kubectl label namespace default istio-injection=enabled\nkubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml\n
Run Code Online (Sandbox Code Playgroud)\n

将虚拟服务hosts从更改*example.com

\n
apiVersion: networking.istio.io/v1alpha3\nkind: Gateway\nmetadata:\n  name: bookinfo-gateway\nspec:\n  selector:\n    istio: ingressgateway # use istio default controller\n  servers:\n  - port:\n      number: 80\n      name: http\n      protocol: HTTP\n    hosts:\n    - "*"\n---\napiVersion: networking.istio.io/v1alpha3\nkind: VirtualService\nmetadata:\n  name: bookinfo\nspec:\n  hosts:\n  - "example.com"\n  gateways:\n  - bookinfo-gateway\n  http:\n  - match:\n    - uri:\n        exact: /productpage\n    - uri:\n        prefix: /static\n    - uri:\n        exact: /login\n    - uri:\n        exact: /logout\n    - uri:\n        prefix: /api/v1/products\n    route:\n    - destination:\n        host: productpage\n        port:\n          number: 9080\n
Run Code Online (Sandbox Code Playgroud)\n

用postman测试过,你也可以使用上面提到的curl。

\n

example.com 主机标头的示例 -> 状态 200。

\n

在此输入图像描述

\n
curl -v -HHost:example.com xx.xxx.xx.x/productpage\nHost:example.com\nHTTP/1.1 200 OK\n
Run Code Online (Sandbox Code Playgroud)\n

example2.com 主机标头的示例 -> 状态 404。

\n

在此输入图像描述

\n
curl -v -HHost:example2.com xx.xxx.xx.x/productpage\nHost:example2.com\nHTTP/1.1 404 Not Found\n
Run Code Online (Sandbox Code Playgroud)\n