Home > OS >  Minikube run flask docker fail with ERR_CONNECTION_RESET
Minikube run flask docker fail with ERR_CONNECTION_RESET

Time:02-22

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.

CodePudding user response:

Based on the error in the question:

pods status Error: ErrImageNeverPull.

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:

The imagePullPolicy for a container and the tag of the image affect when the kubelet attempts to pull (download) the specified image.

You need to switch it to IfNotPresent or Always.

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:

$ kubectl get pods
NAME     READY   STATUS    RESTARTS      AGE
ubuntu   1/1     Running   0             4d

CodePudding user response:

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

  • Related