使用Kubernetes Ingress资源(nginx)在不同路径上的两个Flask应用程序 [英] Two Flask apps on different paths with Kubernetes Ingress resource (nginx)

查看:197
本文介绍了使用Kubernetes Ingress资源(nginx)在不同路径上的两个Flask应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标如下:我在两个单独的Docker容器中有两个Flask应用程序,我想通过同一IP地址上的不同路径访问它们,例如:127.0.0.1/app1,127.0.0.1/app2(但带有真实的IP地址).

我想用Kubernetes做到这一点.

我有一个正在运行的Kubernetes集群(Azure Kubernetes服务),为两个Docker容器中的每一个都提供了Deployment and Service.每个应用程序的Pod运行正常. 我还在集群中安装了一个Ingress控制器(Nginx),现在我正在尝试使其与单个Ingress资源一起使用.

如果我按以下方式进行操作,则它可以完美地适用于1个单个应用程序(其中一个都可以在IP地址/上运行):

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-name
  namespace: my-namespace
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: service1 (or service2)
          servicePort: 5000

但是当我尝试以下操作时,它不起作用:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-name
  namespace: my-namespace
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /app1
        backend:
          serviceName: service1 
          servicePort: 5000
      - path: /app2
        backend:
          serviceName: service2 
          servicePort: 5000

对于这两个应用程序,我可以看到Flask应用程序呈现的html页面,但它们的功能都不起作用.

除了路径似乎并不总是可以正常工作(当我尝试连接到IP-address/app1或IP-address/app1/时,有时有时会重定向到IP-address/)之外,问题是以下(我认为):

每个Flask应用程序都有一个"/predict"路由,该路由仅接受POST请求,并在该请求中对应用程序进行相应的调用(每个应用程序都是对给定输入进行预测的AI应用程序).

两个应用程序的调用均基于IP地址/预测,而不是IP地址/app1/预测或IP地址/app2/预测.另外,由于此路径问题,无法访问静态文件.

我不知道这是否是正确的方法?我也尝试过使用重写目标",但是还没有找到解决方案.

我希望有人可以向我解释我在做错什么.

解决方案

您可以考虑提供 捕获组 完全在Ingress清单内,并指定特定的正则表达式,以定义源Nginx 尾随斜杠,以指示Nginx引擎以正确投放一些静态内容.

My goal is the following: I have two Flask apps in two separate Docker containers and I want to access them via different paths on the same IP address, like this: 127.0.0.1/app1, 127.0.0.1/app2 (but with a real IP address).

I want to do this with Kubernetes.

I have a Kubernetes cluster running (Azure Kubernetes Service), with a Deployment and Service for each of the two Docker containers. The pod for each app is running fine. I also installed an ingress controller (Nginx) in my cluster and now I am trying to make it work with a single Ingress resource.

If I do it as follows, it works perfectly for 1 single app (either one of them works on IP-address/):

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-name
  namespace: my-namespace
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: service1 (or service2)
          servicePort: 5000

But when I try the following it doesn't work:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-name
  namespace: my-namespace
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /app1
        backend:
          serviceName: service1 
          servicePort: 5000
      - path: /app2
        backend:
          serviceName: service2 
          servicePort: 5000

I am able to see the html page that gets rendered by the Flask app, for both applications on their respective paths, but none of the functionalities work.

Other than the fact that the paths don't always seem to work (I sometimes get redirected to IP-address/ when I try to connect to IP-address/app1 or IP-address/app1/), the problem is the following (I think):

Each of the Flask applications have a "/predict" route, that only accepts POST requests, where the respective calls for the apps are made (Each application is an AI application that makes a prediction for a given input).

The calls for both apps are made to IP-address/predict, instead of IP-address/app1/predict or IP-address/app2/predict. Also the static files cannot be accessed because of this path problem.

I don't know if this is the right way to do it? I tried playing around with the 'rewrite-target' as well, but haven't figured out a solution.

I hope someone can explain me what I'm doing wrong.

解决方案

You may consider to supply capture groups entirely within Ingress manifest and specify particular regular expression that will define the source Nginx rewrite rule.

I would expect to get it working as desired, after some adjustments in the genuine Ingress manifest:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-name
  namespace: my-namespace
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - http:
      paths:
      - path: /app1(/|$)(.*)
        backend:
          serviceName: service1 
          servicePort: 5000
      - path: /app2(/|$)(.*)
        backend:
          serviceName: service2 
          servicePort: 5000

Probably, you would also need to add Trailing slash in the target URL, instructing Nginx engine to properly serve some static content.

这篇关于使用Kubernetes Ingress资源(nginx)在不同路径上的两个Flask应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆