Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Kubernetes的Python Flask Restful应用程序-连接被拒绝_Python_Docker_Kubernetes_Flask Restful_Connection Refused - Fatal编程技术网

使用Kubernetes的Python Flask Restful应用程序-连接被拒绝

使用Kubernetes的Python Flask Restful应用程序-连接被拒绝,python,docker,kubernetes,flask-restful,connection-refused,Python,Docker,Kubernetes,Flask Restful,Connection Refused,我首先ssh进入主节点 当我运行kubectl get svc 我得到了NAME、TYPE、CLUSTER-IP、EXTERNAL-IP、PORT、AGE的输出: 服务yaml文件: apiVersion: apps/v1 kind: Deployment metadata: name: python-api labels: app: my-python-app type: back-end spec: replicas: 1 selector: matc

我首先ssh进入主节点

当我运行
kubectl get svc

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

服务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
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()
kubectl描述svc python应用程序服务

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>
Name:python应用程序服务
名称空间:默认值
标签:
注释:
选择器:app=mypython应用程序,type=backend
类型:负载平衡器
IP:10.110.157.42
端口:5000/TCP
目标端口:5000/TCP
节点端口:30008/TCP
终点:10.244.3.24:5000
会话关联:无
外部流量策略:群集
活动:

因此,我无法连接的原因是我从未在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

您是否可以共享命令输出:
kubectl description svc python应用程序服务
?@mk_sta I在到达Pod的端点10.244.3.24:5000的原始后扫描末尾添加了输出?
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>
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