<p>I first ssh into the Master Node.</p>

<p>When I run <code>kubectl get svc</code></p>

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

<pre><code>python-app-service   LoadBalancer   10.110.157.42    &lt;pending&gt;     5000:30008/TCP   68m
</code></pre>

<p>I then run <code>curl 10.110.157.52:5000</code>
and I get the following message:</p>

<p><code>curl: (7) Failed connect to 10.110.157.42:5000; Connection refused</code></p>

<p>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!</p>

<p>Kubernetes was created with KubeAdm using Flannel CNI</p>

<p>Deployment yaml file:</p>

<pre><code>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
</code></pre>

<p>Service yaml file:</p>

<pre><code>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
</code></pre>

<p>Python application source - restful.py:</p>

<pre><code>#!/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/&lt;int:path_user_id&gt;")
api.add_resource(UserList, "/users")

if __name__ == "__main__":
    app.run()
</code></pre>

<p>Dockerfile:</p>

<pre><code>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
</code></pre>

<p><code>kubectl describe svc python-app-service</code></p>

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

ShadyBears
<p>So the reason I was unable to connect was because I never exposed the port in my Dockerfile.</p>

<p>My Dockerfile should have been:</p>

<pre><code>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
</code></pre>
