Python Flask-Restful application with Kubernetes - Connection refused

5/29/2019

I first ssh into the Master Node.

When I run kubectl get svc

I get the output for NAME, TYPE, CLUSTER-IP, EXTERNAL-IP, PORT(S), AGE:

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

I then run curl 10.110.157.52:5000 and I get the following message:

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

Below I posted my Dockerfile, deployment file, service file, and python application file. When I run the docker image, it works fine. However when I try to apply a Kubernetes service to the pod, I am unable to make calls. What am I doing wrong? Also please let me know if I left out any necessary information. Thank you!

Kubernetes was created with KubeAdm using Flannel CNI

Deployment yaml file:

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

Service yaml file:

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 application source - 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>
-- ShadyBears
connection-refused
docker
flask-restful
kubernetes
python

1 Answer

6/5/2019

So the reason I was unable to connect was because I never exposed the port in my Dockerfile.

My Dockerfile should have been:

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
-- ShadyBears
Source: StackOverflow