I am new in Kubernetes, and I want to run the simple flask program on docker in Kubernetes. The image in docker could work successfully, but when I start the K8s.yaml with kubectl apply -f k8s.yaml
and execute minikube service flask-app-service
the web result reply fail with ERR_CONNECTION_REFUSED, and pods status Error: ErrImageNeverPull
.
app.py:
# flask_app/app/app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"
if __name__ == '__main__':
app.debug = True
app.run(debug=True, host='0.0.0.0')
Dockerfile:
FROM python:3.9
RUN mkdir /app
WORKDIR /app
ADD ./app /app/
RUN pip install -r requirement.txt
EXPOSE 5000
CMD ["python", "/app/app.py"]
K8s.yaml:
---
apiVersion: v1
kind: Service
metadata:
name: flask-app-service
spec:
selector:
app: flask-app
ports:
- protocol: "TCP"
port: 5000
targetPort: 5000
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-app
spec:
selector:
matchLabels:
app: flask-app
replicas: 3
template:
metadata:
labels:
app: flask-app
spec:
containers:
- name: flask-app
image: flask_app:latest
imagePullPolicy: Never
ports:
- containerPort: 5000
After deploying I try to connect to http://127.0.0.1:51145
from a browser, but it fails to connect with an ERR_CONNECTION_REFUSED
message. I have a screenshot showing a more detailed Chinese-language error message if that detail is helpful.
update:
After switch imagePullPolicy
from never
to Always
or IfNotPresent
, the pod still can’t run
I try the docker images
command it show the image exist:
But when I pull image with docker pull, it show me the error:
After docker login still not work:
p.s. I follow the website to pratice: https://lukahuang.com/running-flask-on-minikube/
2
Answers
Why are you using the same port for all the containers? I don’t think that will work. You need to assign different ports to each containers or better still create different instances like 3737:5000, 2020:5000 so the external port can be anything while the internal port remains as 5000. That should work I think
Based on the error in the question:
pod doesn’t start because you have
imagePullPolicy: Never
in your deployment manifest. Which means that if the image is missing, it won’t be pulled anyway.This is from official documentation:
You need to switch it to
IfNotPresent
orAlways
.See more in image pull policy.
After everything is done correctly, pod status should be
running
and then you can connect to the pod and get the response back. See the example output: