将 Kubernetes 中的端口映射到两个 Flask 应用程序(前端和后端)
Mapping ports in Kubernetes to two Flask apps (Front & Back-end)
我正在 运行安装两个 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/'
尝试通过
访问前端或后端时 pods
http://127.0.0.1:8001/api/v1/namespaces/default/pods/hello-57d6475c59-ghmhs/proxy/
我觉得我在映射端口的方式上做错了,但我对 kubernetes 的经验不足,不知道是什么。如果有人能发现我失踪了,我将不胜感激。
它说我的 pods 都是 运行ning。
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"]
你好-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
正如 jasonlock 在他的评论中所说,您可以使用 Kompose。
我以前从未听说过 Kompose,https://kubernetes.io/docs/tasks/configure-pod-container/translate-compose-kubernetes/,但在一个命令中我可以将它从 docker-compose 转换为 kubernetes。
我正在 运行安装两个 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/'
尝试通过
访问前端或后端时 podshttp://127.0.0.1:8001/api/v1/namespaces/default/pods/hello-57d6475c59-ghmhs/proxy/
我觉得我在映射端口的方式上做错了,但我对 kubernetes 的经验不足,不知道是什么。如果有人能发现我失踪了,我将不胜感激。
它说我的 pods 都是 运行ning。
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"]
你好-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
正如 jasonlock 在他的评论中所说,您可以使用 Kompose。
我以前从未听说过 Kompose,https://kubernetes.io/docs/tasks/configure-pod-container/translate-compose-kubernetes/,但在一个命令中我可以将它从 docker-compose 转换为 kubernetes。