簡體   English   中英

使用 istio 或 nginx 通過前端代理后端

[英]Proxy backend through frontend using istio or nginx

我正在嘗試找出將 Istio 集成到我的應用程序中的最佳方法,該應用程序由一個 React 前端(由 Nginx 提供)和一個 Django Rest 框架 ZDB974238714CA8ADE634A7CE1D03 組成。 我能夠使用以下 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訪問后端。 請注意,我在端口 443(而不是 8000)上為后端提供服務,因為有關 istio 網關的一些問題不能與 80 和 443 以外的任何端口一起使用

無論如何,這種方法將前端和后端都暴露在集群之外,感覺有點矯枉過正。 無論如何設置這個,所以只有前端顯式暴露,我可以通過前端代理后端? 使用 istio 還是 nginx?

我可能離這里很遠,但聽起來這可能很棘手,因為客戶端正在調用后端。 我必須想辦法在集群內部進行調用並將其返回給客戶端?

據我了解,它應該像這樣工作。

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

首先,我建議查看有關使用服務將前端連接到后端的 kubernetes 文檔,更具體地說,查看將前端與后端服務連接起來的nginx 配置


還有一些 django + react 教程可能會有所幫助:

最后通過基於路徑的路由解決了這個問題(感謝@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM