使用istio虚拟服务公开3条路由

时间:2020-07-20 13:59:47

标签: azure kubernetes kubernetes-ingress istio

我们现在切换到 istio ,我需要将我的应用暴露给外部

在该应用中,我只有3条路线

  1. “ /”根路由
  2. “ /登录”
  3. “静态”-我的应用应提供一些静态文件

我们有gwhost,但由于某种原因我无法访问我的应用程序,你知道我在做什么错吗? vs-yaml

有没有办法公开所有路由,或者应该明确定义它们,如果这样的话,它会与routesmatch有点混淆吗?

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bher-virtualservice
  namespace: ba-trail
spec:
  gateways:
    - webb-system.svc.cluster.local
  hosts:
    - trialio.cloud.str
  http:
    - route:
        - destination:
            host: bsa
            port:
              number: 5000

2 个答案:

答案 0 :(得分:2)

如果是这样的话,如何将其与路线和匹配相混淆

我建议您看一下有关virtual services的istio文档,那里对此有很好的描述。


让我们从头开始,您拥有虚拟服务和网关,它们应该与您的应用程序位于同一命名空间中,或者您需要在两者中都指定它们。

据我所知您的虚拟服务不正确,我准备了适合您的示例。看下面的例子。

网关

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: bher-gateway
  namespace: ba-trail ?
spec:
  selector:
    istio: ingressgateway # use the default IngressGateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "trialio.cloud.str"

我看到您已经部署了gateway,如果它与虚拟服务不在同一个命名空间中,则应像下面的示例一样添加它。

选中spec.gateways部分

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: my-gateway
namespace: some-config-namespace

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo-Mongo
  namespace: bookinfo-namespace
spec:
  gateways:
  - some-config-namespace/my-gateway # can omit the namespace if gateway is in same
                                       namespace as virtual service.

虚拟服务

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bher-virtualservice
  namespace: ba-trail ?
spec:
  gateways:
    - bher-gateway ?  // name of your gateway 
  hosts:
    - trialio.cloud.str
  http:
    - match:
      - uri:
          prefix: "/"
      - uri:
          prefix: "/login"
      - uri:
          prefix: "/static"
      - uri:
          regex: '^.*\.(ico|png|jpg)$'
      route:
      - destination:
          host: bsa.ba-trail.svc.cluster.local ?  // name_of_your service.namespace.svc.cluster.local
          port:
            number: 5000

看看这个example

让我们分解应该路由到前端的请求:

确切路径 /应该路由到Frontend以获得Index.html

前缀路径 / static / *应该路由到Frontend以获得前端所需的任何静态文件,例如级联样式表 JavaScript文件

与正则表达式^。*。(ico | png | jpg)$匹配的路径应路由到Frontend,因为它是页面需要显示的图像。

http:
  - match:
    - uri:
        exact: /
    - uri:
        exact: /callback
    - uri:
        prefix: /static
    - uri:
        regex: '^.*\.(ico|png|jpg)$'
    route:
    - destination:
        host: frontend             
        port:
          number: 80

希望您觉得这很有用。如果您有任何疑问,请在评论中告诉我。

答案 1 :(得分:1)

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bher-virtualservice
  namespace: ba-trail
spec:
  gateways:
    - webb-system.svc.cluster.local.     #### don't look right , can write the gateway name only here.  ####
  hosts:
    - trialio.cloud.str
  http:
    - route:
        - destination:
            host: bsa
            port:
              number: 5000        ##  is your service working on this port 
    - rewrite:                    #### this should not be an issue but you can try adding this too
       uri: /