Single container Python app in Kubernetes



examples/k8s/echo_get.py
from flask import Flask, request
app = Flask(__name__)

@app.route("/")
def main():
    return '''
     <form action="/echo" method="GET">
         <input name="text">
         <input type="submit" value="Echo">
     </form>
     '''

@app.route("/echo")
def echo():
    return "You said: " + request.args.get('text', '')

If we have Python and Flask installed we can run the application locally:


FLASK_APP=echo_get flask run


examples/k8s/Dockerfile_echo_get
FROM python:3.7
RUN pip install flask
ENV FLASK_APP echo_get
WORKDIR /opt
COPY  echo_get.py .
CMD ["flask", "run", "--host", "0.0.0.0"]


docker build -t myflask:latesr -f Dockerfile_echo_get .

and then we can run it: (tryflask is just an arbitrary name)


docker run --rm -it -p5000:5000 --name tryflask myflask

Add the docker image to the local Minikube docker registry:


minikube image load myflask:latest

List the images there


minikube image list


examples/k8s/echo_get.yaml
apiVersion: v1
kind: Pod
metadata:
  name: echo-get
spec:
  containers:
    - name: echo-get-container
      image: myflask
      imagePullPolicy: Never
      ports:
      - protocol: TCP
        containerPort: 5000

kubectl apply -f echo_get.yaml