Pods not communicating when running in cloud as opposed to minikube

3/15/2019

Edit - I've applied a LoadBalancer to each of my services and only my frontend is running the correct image. My other images are running flask, but I can't open the url's I have in my latest images. My images are hosted on Docker repo's and I'm running google cloud.

I'm trying to deploy my service that I have been running locally on minikube to the cloud. Everything works as expected when running locally, however, when I moved to the cloud it seems I cannot get a response from any of my pods when I get a request. I am probably doing something silly here as I have never attempted Kube on the cloud. Can someone shed some light? It appears kube cannot resolve the address of my service.

my deployment.yaml

apiVersion: v1
kind: Service
metadata:
  name: frontend
  labels:
    app: frontend
spec:
  ports:
  - port: 5000
    name: http
  selector:
    app: frontend
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: frontend
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: frontend
        version: v1
    spec:
      containers:
        - name: frontend
          image: jor2/frontend
          ports:
          - containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
  name: backend
  labels:
    app: backend
spec:
  ports:
  - port: 5000
    name: http
  selector:
    app: backend
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: backend
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: backend
        version: v1
    spec:
      containers:
        - name: backend
          image: jor2/backend
          ports:
          - containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
  name: admin-backend
  labels:
    app: admin-backend
spec:
  ports:
  - port: 5000
    name: http
  selector:
    app: admin-backend
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: admin-backend
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: admin-backend
        version: v1
    spec:
      containers:
        - name: admin-backend
          image: jor2/admin-backend
          ports:
          - containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
  name: utils-backend
  labels:
    app: utils-backend
spec:
  ports:
  - port: 5000
    name: http
  selector:
    app: backend
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: utils-backend
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: utils-backend
        version: v1
    spec:
      containers:
        - name: utils-backend
          image: jor2/utils-backend
          ports:
          - containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
  name: mongo
spec:
  ports:
    - port: 27017
      targetPort: 27017
  selector:
    name: mongo
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mongo
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: mongo
        version: v1
    spec:
      containers:
        - image: mongo
          name: mongo
          ports:
            - containerPort: 27017

frontend trying to send a request to backend service

from flask import Flask, render_template, request, flash, redirect, url_for, session, jsonify
import requests

app = Flask(__name__)

app.url_map.strict_slashes = False

HEADERS = dict()


@app.route("/view_users/", methods=['GET'])
def view_users():
    users = invoke_backend(page_name="view_users", service="utils")
    return jsonify(users)


def invoke_backend(page_name, id="", service="", headers={}):
    url = create_url(page_name, id, service)
    try:
        res = requests.get(url, headers=headers)
    except Exception:
        return "Error with {}.".format(url)
    try:
        response = res.json()
    except ValueError:
        return "Decoding JSON has failed."
    return response


def create_url(page_name, id="", service=""):
    if service == "admin":
        be_host = os.getenv('ADMIN_BACKEND_SERVICE_HOST', 'admin-backend')
        be_port = os.getenv('ADMIN_BACKEND_SERVICE_PORT', '5000')
        url = 'http://{host}:{port}/{page_name}/{id}'.format(host=be_host, port=be_port, page_name=page_name, id=id)
        return url
    elif service == "frontend":
        be_host = os.getenv('FRONTEND_SERVICE_HOST', 'frontend')
        be_port = os.getenv('FRONTEND_SERVICE_PORT', '5000')
        url = 'http://{host}:{port}/{page_name}/{id}'.format(host=be_host, port=be_port, page_name=page_name, id=id)
        return url
    elif service == "utils":
        be_host = os.getenv('UTILS_BACKEND_SERVICE_HOST', 'utils-backend')
        be_port = os.getenv('UTILS_BACKEND_SERVICE_PORT', '5000')
        url = 'http://{host}:{port}/{page_name}/{id}'.format(host=be_host, port=be_port, page_name=page_name, id=id)
        return url

    be_host = os.getenv('BACKEND_SERVICE_HOST', 'backend')
    be_port = os.getenv('BACKEND_SERVICE_PORT', '5000')
    url = 'http://{host}:{port}/{page_name}/{id}'.format(host=be_host, port=be_port, page_name=page_name, id=id)
    return url


if __name__ == '__main__':
    app.secret_key = 'secret123'
    app.run(debug=True, host='0.0.0.0')
-- Annihil8
flask
google-cloud-platform
kubernetes
mongodb

2 Answers

3/15/2019

is this your fask script running inside kubernetes cluster ?

service name port config in deployment looking perfect

can you try with mongo = MongoClient("mongodb://mongo:27017")

and if your flask script running inside the kubernetes then and then it can resolve the address of mongo

-- Harsh Manvar
Source: StackOverflow

3/15/2019

I had the wrong selector in the utils-backend deployment config. Narrowed my bug down to utils backend after testing and discovered the issue.

selector:
  app: backend

revision:

selector
  app: utils-backend
-- Annihil8
Source: StackOverflow