【问题标题】:hosting multiple web apps using the istio ingress gateway使用 istio 入口网关托管多个 Web 应用程序
【发布时间】:2020-10-30 13:05:02
【问题描述】:

我们使用 istio 作为服务网格来保护我们的集群。我们通过 ingress 网关公开了几个 Web 应用程序,如下所示 ingress-gateway-id:80/app1/、ingress-gateway-id:80/app2/ 和 ingress-gateway-id:80/app3/。

我们有一个网关,可以在端口 80 上路由入口网关的流量。

对于每个应用程序,我们创建一个虚拟服务,将流量从(例如)ingress-gateway-id:80/app1/app1-api-uri/ 路由到 app1-service/app1-api-uri/

我们目前面临的主要问题是某些应用程序仅通过 /(例如)app2-service/ 工作,这迫使我们允许 / 通过虚拟服务并限制入口网关以仅允许一个应用程序通过入口网关(没有在标头中指定主机,因为我们所有的应用程序都是 Web 应用程序,因此在我们的用例中可以通过浏览器访问)。

我的问题是如何允许多个应用程序访问/通过我的入口网关(例如在同一个端口 80 上)而无需处理从客户端(在我们的例子中是浏览器)设置主机头?

【问题讨论】:

    标签: istio istio-sidecar


    【解决方案1】:

    如果您不想将您的域用作虚拟服务主机,我会说这里唯一的选择是

    • 在您的虚拟服务中使用重写。
    • 使用自定义标题

    有一个关于从 istio 文档中重写的示例。

    HTTPRewrite

    HTTPRewrite 可用于在将请求转发到目的地之前重写 HTTP 请求的特定部分。重写原语只能与 HTTPRouteDestination 一起使用。以下示例演示了如何在进行实际 API 调用之前将 api 调用 (/ratings) 的 URL 前缀重写为评级服务。

    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: ratings-route
    spec:
      hosts:
      - ratings.prod.svc.cluster.local
      http:
      - match:
        - uri:
            prefix: /ratings
        rewrite:
          uri: /v1/bookRatings
        route:
        - destination:
            host: ratings.prod.svc.cluster.local
            subset: v1
    

    有 2 个 nginx 部署的示例,均在 / 上提供服务。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx1
    spec:
      selector:
        matchLabels:
          run: nginx1
      replicas: 1
      template:
        metadata:
          labels:
            run: nginx1
            app: frontend
        spec:
          containers:
          - name: nginx1
            image: nginx
            ports:
            - containerPort: 80
            lifecycle:
              postStart:
                exec:
                  command: ["/bin/sh", "-c", "echo Hello nginx1 > /usr/share/nginx/html/index.html"]
    
    
    
    ---
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx2
    spec:
      selector:
        matchLabels:
          run: nginx2
      replicas: 1
      template:
        metadata:
          labels:
            run: nginx2
            app: frontend
        spec:
          containers:
          - name: nginx2
            image: nginx
            ports:
            - containerPort: 80
            lifecycle:
              postStart:
                exec:
                  command: ["/bin/sh", "-c", "echo Hello nginx2 > /usr/share/nginx/html/index.html"]
    
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
      name: nginx
      labels:
        app: frontend
    spec:
      ports:
      - port: 80
        protocol: TCP
      selector:
        app: frontend
    
    
    ---
    
    apiVersion: networking.istio.io/v1alpha3
    kind: Gateway
    metadata:
      name: comp-ingress-gateway
    spec:
      selector:
        istio: ingressgateway
      servers:
      - hosts:
        - '*'
        port:
          name: http
          number: 80
          protocol: HTTP
    
    ---
    
    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: nginxvirt
    spec:
      gateways:
      - comp-ingress-gateway
      hosts:
      - '*'
      http:
      - name: match
        match:
        - uri:
            prefix: /a
        rewrite:
          uri: /
        route:
        - destination:
            host: nginx.default.svc.cluster.local
            subset: v1
            port:
              number: 80
      - name: default
        match:
        - uri:
            prefix: /b
        rewrite:
          uri: /
        route:
        - destination:
            host: nginx.default.svc.cluster.local
            subset: v2
            port:
              number: 80
    ---
    
    
    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
      name: nginxdest
    spec:
      host: nginx.default.svc.cluster.local
      subsets:
      - name: v1
        labels:
          run: nginx1
      - name: v2
        labels:
          run: nginx2
      trafficPolicy:
        tls:
          mode: ISTIO_MUTUAL
    

    还有一个 curl 测试。

    curl -v xx.xxx.xxx.x/a 
    HTTP/1.1 200 OK
    Hello nginx1
    
    
    curl -v xx.xxx.xxx.x/b
    HTTP/1.1 200 OK
    Hello nginx2
    

    在 istio 文档中有一个关于自定义标头的 example

    【讨论】:

    • 谢谢雅库布。我的前端应用程序面临的问题是重定向。我正在使用一些技术(apache atlas),我想重新路由到它的 UI。流程如下: 1. ingress-gateway/atlas -> 被重定向到提供给登录的 Oauth -> 在登录后重定向到“/”上的 atlas。因此,我在登录后得到“/”。我需要使用前缀“/”重新路由到同一网关上的不同应用程序,这可能是不可能的。您提出的解决方案假设 apache atlas 将重定向到 ingress-gateway/atlas 以便访问 atlas 的 /。
    猜你喜欢
    • 2020-06-17
    • 2019-08-26
    • 2020-08-31
    • 2018-02-10
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 2015-05-07
    相关资源
    最近更新 更多