【问题标题】:Proxy backend through frontend using istio or nginx使用 istio 或 nginx 通过前端代理后端
【发布时间】:2020-08-23 11:07:27
【问题描述】:

我正在尝试找出将 Istio 集成到我的应用程序中的最佳方式,该应用程序由一个 React 前端(由 Nginx 提供服务)和一个 Django Rest Framework API 组成。我能够使用以下 nginx 配置和特定于 istio 的 kubernetes 文件使其工作:

server {
    listen 80;
    root /app/build;

    location / {
        try_files $uri $uri/ /index.html;
    }
}
# Source: myapp/gateway.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: myapp-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
    - port:
        number: 80
        name: http
        protocol: HTTP
      hosts:
        - '*'
    - port:
        number: 443
        name: https
        protocol: HTTP
      hosts:
        - '*'
---
# Source: myapp/virtual-service.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: myapp
spec:
  hosts:
    - '*'
  gateways:
    - myapp-gateway
  http:
    - match:
        - port: 80
      route:
        - destination:
            host: frontend-svc
            port:
              number: 80
    - match:
        - port: 443
      route:
        - destination:
            host: backend-svc
            port:
              number: 8000

前端可以通过localhost:443 访问后端。请注意,由于some issue regarding the istio gateway not working with any port other than 80 and 443,我在端口 443(而不是 8000)上为后端提供服务。

无论如何,这种方法将前端和后端都暴露在集群之外,这感觉有点矫枉过正。无论如何设置这个,所以只有前端被显式暴露,我可以通过前端代理后端?使用 istio 还是 nginx?

我可能离这里很远,但听起来这可能很棘手,因为客户端正在调用后端。我必须想办法在集群内部进行调用并将其返回给客户端?

【问题讨论】:

  • 您可以将虚拟服务配置为进行基于路径的路由,这可能是一种更好的处理方式(将两个服务都放在端口 443,正确配置 TLS,并路由到前端或后端基于 URL 路径)。如果您不想直接公开后端,您在此处确定的任何层都可以代理后端,但如果像 React 应用程序这样的东西直接调用它,您必须以某种形式公开它。

标签: django reactjs nginx kubernetes istio


【解决方案1】:

最后通过基于路径的路由解决了这个问题(感谢@DavidMaze 的有用评论):

# Source: myapp/gateway.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: myapp-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
    - port:
        number: 80
        name: http
        protocol: HTTP
      hosts:
        - '*'
---
# Source: myapp/virtual-service.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: myapp
spec:
  hosts:
    - '*'
  gateways:
    - myapp-gateway
  http:
    - match:
        - uri:
            prefix: '/api'
      route:
        - destination:
            host: backend-svc
            port:
              number: 8000
    - route:
        - destination:
            host: frontend-svc
            port:
              number: 80

【讨论】:

    【解决方案2】:

    据我所知,它应该像这样工作。

    user -> istio ingressgateway -> istio virtual service -> frontend service -> nginx -> backend service
    

    Istio 虚拟服务应该是这样的,所以只有前端被暴露,然后你配置你的 nginx 通过前端代理后端。

    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: myapp
    spec:
      hosts:
        - '*'
      gateways:
        - myapp-gateway
      http:
      - route:
        - destination:
            host: frontend-svc
            port:
              number: 80
    

    首先,我建议查看有关 Connect a Front End to a Back End Using a Service 的 kubernetes 文档,更具体地说,查看连接前端和后端服务的 nginx configuration


    还有一些可能有帮助的 django + react 教程:

    【讨论】:

      猜你喜欢
      • 2021-02-28
      • 2021-03-02
      • 2021-10-13
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 2022-07-17
      • 2021-05-19
      • 1970-01-01
      相关资源
      最近更新 更多