主机匹配在istio网关中不起作用

时间:2019-02-11 17:25:28

标签: google-kubernetes-engine gateway istio

我按照本教程安装istio并部署了示例bookinfo应用程序。 https://cloud.google.com/kubernetes-engine/docs/tutorials/installing-istio

它们具有以下ingress-gateway.yml文件

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: bookinfo-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo
spec:
  hosts:
  - "*"
  gateways:
  - bookinfo-gateway
  http:
  - match:
    - uri:
        exact: /productpage
    - uri:
        exact: /login
    - uri:
        exact: /logout
    - uri:
        prefix: /api/v1/products
    route:
    - destination:
        host: productpage
        port:
          number: 9080

当我执行kubectl apply -f ingress-gateway.yml时,它可以很好地工作,并且我可以在http://<ip>/productpage上访问该应用程序

但是,如果我想在特定域上访问它,例如bookinfo.com

我更改了hostsgateway部分中的VirtualService字段,并在我的/etc/hosts文件中添加了一个条目。

因此,它更改为以下内容

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: bookinfo-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "bookinfo.com"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo
spec:
  hosts:
  - "bookinfo.com"
  gateways:
  - bookinfo-gateway
  http:
  - match:
    - uri:
        exact: /productpage
    - uri:
        exact: /login
    - uri:
        exact: /logout
    - uri:
        prefix: /api/v1/products
    route:
    - destination:
        host: productpage
        port:
          number: 9080

当我尝试访问http://bookinfo.com/productpage时,显示未找到404。我想念什么?

PS:我正在使用istio1.0.5

2 个答案:

答案 0 :(得分:0)

如果您正在执行curl http://bookinfo.com/productpage,则只有在完成该IP地址的主机文件输入后,它才有效。

或者更好的方法是在curl请求中显式发送标头:

curl <ip:port>/productpage --header 'Host: bookinfo.com'

如果您仍然无法执行此操作,请让我现在。

答案 1 :(得分:0)

您已经将路径设置为/ productpage的路径,那么在您的情况下,您的VirtualService中的destination.host应该与您的VirtualService名称匹配,例如“ bookinfo”

然后运行curl命令

curl -I -HHost:bookinfo.com http:// $ INGRESS_HOST:$ INGRESS_PORT / productpage

请注意,您使用-H标志将主机HTTP标头设置为“ bookinfo.com”。这是必需的,因为您的入口网关已配置为处理“ bookinfo.com”。