无法从本地访问在 minikube 上运行的 Flask 应用程序

时间:2021-05-01 02:02:17

标签: python docker flask kubernetes minikube

我创建了一个在容器中运行的 Flask 应用。

Dockerfile

FROM python:3.7
LABEL MAINTAINER="Anirban"
RUN mkdir /app
WORKDIR /app
COPY . /app/
RUN pip install -r requirements.txt
RUN python setup.py install
ENV PORT 5000
ENV SERVER_NAME "0.0.0.0:5000"
EXPOSE 5000
CMD [ "python", "userapp/app.py" ]

我的应用结构

[project] 
    |
    --userapp
    |    |
    |     --app.py
    --Dockerfile

烧瓶配置

def initialize_app(flask_app):
    flask_app.register_blueprint(SWAGGERUI_BLUEPRINT, url_prefix=SWAGGER_URL)
    flask_app.register_blueprint(user_endpoints.REQUEST_API)
    flask_app.config['SERVER_NAME'] = os.environ.get('SERVER_NAME', settings.FLASK_SERVER_NAME)


if __name__ == "__main__":
    initialize_app(app)
    app.run(debug=os.environ.get('FLASK_DEBUG', settings.FLASK_DEBUG))

部署

apiVersion: apps/v1
kind: Deployment
metadata:
  name: userauthapp
  labels:
    app: userauthapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: userauthapp
  template:
    metadata:
      labels:
        app: userauthapp
    spec:
      containers:
        - name: userauthapp
          image: <my image>
          ports:
            - containerPort: 5000
          imagePullPolicy: Always

负载均衡器

apiVersion: v1
kind: Service
metadata:
  name: userauthapp-lb-svc
spec:
  selector:
    app: userauthapp
  ports:
    - protocol: "TCP"
      port: 8080
      targetPort: 5000
  type: LoadBalancer

当我在 minikube 上部署它们时,我可以看到它们运行如下:

NAME                               READY   STATUS    RESTARTS   AGE
pod/userauthapp-6cc76bd66b-vst5n   1/1     Running   0          38s

NAME                         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
service/kubernetes           ClusterIP      10.96.0.1        <none>        443/TCP          267d
service/userauthapp-lb-svc   LoadBalancer   10.107.117.254   <pending>     8080:31145/TCP   2s

但我无法访问它们:curl 10.107.117.254:8080/(这是健康检查 api)或 curl 10.107.117.254:8080/swagger。有没有人知道它有什么问题?

1 个答案:

答案 0 :(得分:1)

我知道了这个问题。因为它在 minikube 上运行,所以我需要点击 minikube IP。下面的命令给出了 minikube IP:

minikube ip

下面的命令返回IP和对应的服务端口

minikube service --url userauthapp-lb-svc