将Kubernetes中的端口映射到两个Flask应用程序(前端和后端)

时间:2018-12-03 02:15:21

标签: docker kubernetes minikube

我正在运行两个Flask应用程序,一个用于前端,一个用于后端。我已经将前端映射到端口8001,将后端映射到端口8002,当使用docker-compose时,它们都运行良好。但是,我很难将Docker映像传输到kubernetes(使用minikube在本地运行)。我已经阅读了文档,并认为自己已经正确映射了端口,但仍然收到错误消息

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

当尝试通过

访问前端或后端窗格时
http://127.0.0.1:8001/api/v1/namespaces/default/pods/hello-57d6475c59-ghmhs/proxy/

我感觉我在映射端口的方式上做错了事,但是我对kubernetes的经验不足以知道什么。如果有人能找到我不见了,我将不胜感激。

它说我的两个吊舱都在运行。

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

很抱歉用代码轰炸您,但是我不知道如何更好地凝聚力。您大部分都可以忽略,只是出于上下文。

前端

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')

后端

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(前端和后端的内容相同)

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

1 个答案:

答案 0 :(得分:0)

正如jasonlock在他的评论中所述,您可以使用Kompose。

我以前从未听说过Kompose https://kubernetes.io/docs/tasks/configure-pod-container/translate-compose-kubernetes/,但是在一个命令中,我可以将其从docker-compose转换为kubernetes。