【问题标题】:Python Flask-Restful application with Kubernetes - Connection refused带有 Kubernetes 的 Python Flask-Restful 应用程序 - 连接被拒绝
【发布时间】:2019-06-05 21:54:29
【问题描述】:

我首先 ssh 进入主节点。

当我运行kubectl get svc

我得到了 NAME、TYPE、CLUSTER-IP、EXTERNAL-IP、PORT(S)、AGE 的输出:

python-app-service   LoadBalancer   10.110.157.42    <pending>     5000:30008/TCP   68m

然后我运行curl 10.110.157.52:5000 我收到以下消息:

curl: (7) Failed connect to 10.110.157.42:5000; Connection refused

下面我发布了我的 Dockerfile、部署文件、服务文件和 python 应用程序文件。当我运行 docker 映像时,它工作正常。但是,当我尝试将 Kubernetes 服务应用到 pod 时,我无法拨打电话。我究竟做错了什么?如果我遗漏了任何必要的信息,请告诉我。谢谢!

Kubernetes 是使用 Flannel CNI 使用 KubeAdm 创建的

部署yaml文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: python-api
  labels:
    app: my-python-app
    type: back-end
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-python-app
      type: backend
  template:
    metadata:
      name: python-api-pod
      labels:
        app: my-python-app
        type: backend
    spec:
      containers:
      - name: restful-python-example
        image: mydockerhub/restful-python-example
        ports:
        - containerPort: 5000

服务 yaml 文件:

apiVersion: v1
kind: Service
metadata:
  name: python-app-service
spec:
  type: LoadBalancer
  ports:
  - port: 5000
    targetPort: 5000
    nodePort: 30008
  selector:
    app: my-python-app
    type: backend

Python 应用源码-restful.py:

#!/usr/bin/python3

from flask import Flask, jsonify, request, abort
from flask_restful import Api, Resource
import jsonpickle

app = Flask(__name__)
api = Api(app)

# Creating an empty dictionary and initializing user id to 0.. will increment every time a person makes a POST request.
# This is bad practice but only using it for the example. Most likely you will be pulling this information from a
# database.
user_dict = {}
user_id = 0


# Define a class and pass it a Resource. These methods require an ID
class User(Resource):
    @staticmethod
    def get(path_user_id):
        if path_user_id not in user_dict:
            abort(400)

        return jsonify(jsonpickle.encode(user_dict.get(path_user_id, "This user does not exist")))

    @staticmethod
    def put(path_user_id):
        update_and_add_user_helper(path_user_id, request.get_json())

    @staticmethod
    def delete(path_user_id):
        user_dict.pop(path_user_id, None)


# Get all users and add new users
class UserList(Resource):
    @staticmethod
    def get():
        return jsonify(jsonpickle.encode(user_dict))

    @staticmethod
    def post():
        global user_id
        user_id = user_id + 1
        update_and_add_user_helper(user_id, request.get_json())


# Since post and put are doing pretty much the same thing, I extracted the logic from both and put it in a separate
# method to follow DRY principles.
def update_and_add_user_helper(u_id, request_payload):
    name = request_payload["name"]
    age = request_payload["age"]
    address = request_payload["address"]
    city = request_payload["city"]
    state = request_payload["state"]
    zip_code = request_payload["zip"]
    user_dict[u_id] = Person(name, age, address, city, state, zip_code)


# Represents a user's information
class Person:
    def __init__(self, name, age, address, city, state, zip_code):
        self.name = name
        self.age = age
        self.address = address
        self.city = city
        self.state = state
        self.zip_code = zip_code


# Add a resource to the api. You need to give the class name and the URI.
api.add_resource(User, "/users/<int:path_user_id>")
api.add_resource(UserList, "/users")

if __name__ == "__main__":
    app.run()

Dockerfile:

FROM python:3
WORKDIR  /usr/src/app
RUN  pip install flask
RUN  pip install flask_restful
RUN  pip install jsonpickle
COPY  . .
CMD  python restful.py

kubectl describe svc python-app-service

Name:                     python-app-service
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=my-python-app,type=backend
Type:                     LoadBalancer
IP:                       10.110.157.42
Port:                     <unset>  5000/TCP
TargetPort:               5000/TCP
NodePort:                 <unset>  30008/TCP
Endpoints:                10.244.3.24:5000
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

【问题讨论】:

  • 你能分享命令输出吗:kubectl describe svc python-app-service
  • @mk_sta 我在原帖末尾添加了输出
  • 你能到达 Pod 的端点 10.244.3.24:5000 吗?

标签: python docker kubernetes flask-restful connection-refused


【解决方案1】:

所以我无法连接的原因是因为我从未在我的 Dockerfile 中暴露端口。

我的 Dockerfile 应该是:

FROM python:3
WORKDIR  /usr/src/app
RUN  pip install flask
RUN  pip install flask_restful
RUN  pip install jsonpickle
COPY  . .
EXPOSE 5000
CMD  python restful.py

【讨论】:

    猜你喜欢
    • 2013-03-02
    • 2020-11-06
    • 2021-10-22
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    相关资源
    最近更新 更多