Mapping ports in Kubernetes to two Flask apps (Front & Back-end)

12/3/2018

I'm running two Flask apps, one for front-end and one for back-end. I have mapped the front-end to port 8001 and the back-end to port 8002, all runs well when using docker-compose. However, I'm having difficulty transferring my docker images to kubernetes (run locally using minikube). I have read docs and I thought I had mapped my ports correctly, but I'm still receiving an error

Error: 'dial tcp 172.17.0.5:8002: getsockopt: connection refused'
Trying to reach: 'http://172.17.0.5:8002/'

when trying to access the front-end or back-end pods via

http://127.0.0.1:8001/api/v1/namespaces/default/pods/hello-57d6475c59-ghmhs/proxy/

I feel like I'm doing something wrong with the way I'm mapping the ports, but I am not experienced enough with kubernetes to know what. I would appreciate if someone could spot it is I'm missing.

It says both my pods are running.

NAME                        READY   STATUS    RESTARTS   AGE
frontend-66d5699888-bbbct   1/1     Running   0          48m
hello-57d6475c59-ghmhs      1/1     Running   0          49m

I'm sorry to bombard you with code, but I didn't know how better to convey my effort cohesively. Most of it you can just ignore, it's just for context.

Front-end

from flask import Flask, jsonify
import requests
import json

app = Flask(__name__)


@app.route("/")
def home():
    url = "http://backend:5000/"
    res = requests.get(url)
    dictFromServer = res.json()
    return dictFromServer['message']


if __name__=='__main__':
    app.run(debug=True, host='0.0.0.0')

Back-end

from flask import Flask, jsonify

app = Flask(__name__)


@app.route('/')
def hello_world():
    return jsonify({ 'message': 'Hello World v1 - Demo'})


if __name__=='__main__':
    app.run(debug=True, host='0.0.0.0')

Dockerfile (same contents for front & back-end)

FROM python:3.6-alpine
WORKDIR /app
COPY requirement=s.txt /app
COPY app.py /app
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["app.py"]

frontend.conf

upstream hello {
    server hello;
}

server {
    listen 8001;

    location / {
        proxy_pass http://hello;
    }
}

frontend.yaml

apiVersion: v1
kind: Service
metadata:
  name: frontend
spec:
  selector:
    app: hello
    tier: frontend
  ports:
  - protocol: "TCP"
    port: 8001
    targetPort: 8001
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
spec:
  selector:
    matchLabels:
      app: hello
      tier: frontend
      track: stable
  replicas: 1
  template:
    metadata:
      labels:
        app: hello
        tier: frontend
        track: stable
    spec:
      containers:
      - name: nginx
        image: "jor2/hello-world-kubernetes_frontend"
        lifecycle:
          preStop:
            exec:
              command: ["/usr/sbin/nginx","-s","quit"]

hello-service.yaml

kind: Service
apiVersion: v1
metadata:
  name: hello
spec:
  selector:
    app: hello
    tier: backend
  ports:
  - protocol: TCP
    port: 8002
    targetPort: http

hello.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
spec:
  selector:
    matchLabels:
      app: hello
      tier: backend
      track: stable
  replicas: 1
  template:
    metadata:
      labels:
        app: hello
        tier: backend
        track: stable
    spec:
      containers:
        - name: hello
          image: "jor2/hello-world-kubernetes_backend"
          ports:
            - name: http
              containerPort: 8002
-- Annihil8
docker
kubernetes
minikube

1 Answer

12/3/2018

As stated by jasonlock in his comment, you can use Kompose.

I had previously never heard of Kompose, https://kubernetes.io/docs/tasks/configure-pod-container/translate-compose-kubernetes/, but in one command I could convert it from docker-compose to kubernetes.

-- Annihil8
Source: StackOverflow