英文:
Why is my Flask app running on port 5000 even though I specified another containerPort in deployment.yaml file?
问题
我有一个Python应用程序
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/")
def index():
return jsonify({"hey": "test6"})
以及一个deployment.yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: mqttdemob
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: mqttdemob
strategy: {}
template:
metadata:
labels:
app: mqttdemob
spec:
containers:
- image: ****/fluxdemo:main-584db7b6-1687862454 # {"$imagepolicy": "flux-system:mqttdemob"}
name: app
ports:
- containerPort: 6000
以及一个service.yaml文件
apiVersion: v1
kind: Service
metadata:
name: mqttdemob
spec:
selector:
app: mqttdemob
ports:
- protocol: TCP
port: 6000
targetPort: 6000
nodePort: 30400
externalIPs:
- 1.2.4.122
type: NodePort
当我使用这些文件部署时,我希望Flask应用程序在端口6000上运行,然后将其转发到30400。
但是,当我运行
kubectl exec <pod name> -- netstat -tulpn
它输出
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN 1/python
这表明它没有使用端口6000,而是使用了端口5000。
我做错了什么,如何确保Flask使用端口6000而不是5000?
英文:
I have a python app
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/")
def index():
return jsonify({"hey":"test6"})
And a deployment.yaml file
apiVersion: apps/v1
kind: Deployment
metadata:
name: mqttdemob
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: mqttdemob
strategy: {}
template:
metadata:
labels:
app: mqttdemob
spec:
containers:
- image: ****/fluxdemo:main-584db7b6-1687862454 # {"$imagepolicy": "flux-system:mqttdemob"}
name: app
ports:
- containerPort: 6000
And a service.yaml file
apiVersion: v1
kind: Service
metadata:
name: mqttdemob
spec:
selector:
app: mqttdemob
ports:
- protocol: TCP
port: 6000
targetPort: 6000
nodePort: 30400
externalIPs:
- 1.2.4.122
type: NodePort
When I deploy using these files I would want the Flask app to run on port 6000 which then would be forwarded to 30400.
But when I run
kubectl exec <pod name> -- netstat -tulpn
It outputs
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN 1/python
Such that it is not using port 6000 but port 5000.
What am I doing wrong and how can I make sure that Flask will use port 6000 instead of 5000?
答案1
得分: 2
containerPort
仅供信息参考,不配置你的应用程序运行的端口。
你的 Flask 应用程序正在使用其默认端口。
尝试在启动服务器的主文件中更改它:
port = 6000 # 你可以将它更改为任何未使用的端口。
if __name__ == '__main__':
app.run(host='0.0.0.0', port)
上述情况中的 "未使用" 意味着当前没有在使用的端口。
英文:
containerPort
is only informational and doesn't configure the port your app is running on.
You flask application is using its default port.
Try changing it in you main file where you are starting your server using:
port = 6000 #you can change it to any unused port.
if __name__ == '__main__':
app.run(host='0.0.0.0', port)
- unused in the above case means the port which is currently not in use.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论