首页 > 解决方案 > 主机匹配在 istio 网关中不起作用

问题描述

我按照本教程安装了 istio 并部署了示例bookinfo应用程序。

他们有以下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

我更改了and部分hosts中的字段,并在我的文件中添加了一个条目。gatewayVirtualService/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 not found。我错过了什么?

PS:我正在使用istio1.0.5

标签: google-kubernetes-enginegatewayistio

解决方案


您已经将路由设置为 path /productpage,那么您的 destination.host 在您的情况下VirtualService应该与您的VirtualService姓名匹配,即“bookinfo”

然后运行 ​​curl 命令

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

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


推荐阅读