Home > Enterprise >  unable to get kubectl to run flask app in minikube
unable to get kubectl to run flask app in minikube

Time:04-10

I have been going around the StackOverflow and couldn't figure out why my kubectl won't connect to the simple flask example in the Kubernetes documentation. Here's my file:

from flask import Flask

app = Flask(__name__)


@app.route("/")
def hello():
    return "Hello world from python"


if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0")

my docker file:

FROM python:3.9

RUN mkdir myapp
RUN cd myapp

COPY hello_world.py .
COPY requirements.txt .
RUN pip install -r requirements.txt
EXPOSE 5000

CMD ["python3", "hello_world.py"]

my YAML file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world-deployment
  labels:
    app: hello
spec:
  replicas: 4
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
        - name: hello-world-container
          image: mrajancsr/playground:1
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 5000

---
apiVersion: v1
kind: Service
metadata:
  name: hello-world-service
spec:
  selector:
    app: hello
  ports:
    - protocol: TCP
      port: 5000
      targetPort: 5000

Now, when I run the following:

kubectl get pods

I get

hello-world-deployment-67d4d6c95c-2cpvx   1/1     Running   0          12m
hello-world-deployment-67d4d6c95c-dm78v   1/1     Running   0          12m
hello-world-deployment-67d4d6c95c-f62w7   1/1     Running   0          10m
hello-world-deployment-67d4d6c95c-xlr7w   1/1     Running   0          12m

Then next:

kubectl get svc

I get...

hello-world-service   ClusterIP   10.97.58.104   <none>        5000/TCP   12h
kubernetes            ClusterIP   10.96.0.1      <none>        443/TCP    3d13h

I then typed:

kubectl exec -it hello-world-deployment-67d4d6c95c-2cpvx bash
curl localhost:5000

I get the message:

Hello world from python

This works as well:

docker run -p 5001:5000 hello-python

I couldn't use 5000:5000 as it was already bound for some reason.

But I'm not able to connect it via kubectl:

curl 10.97.58.104:5000

I then read that the reason the kubectl is not working is because perhaps I need to push my docker image and have it pull? So I created a docker repository and pushed my image and hence you can see the label as mrajancsr/playground:1 as that's what I pulled from my private repo and it's still not working.

CodePudding user response:

kubectl is telling you that your service hello-world-service has a ClusterIP of 10.97.58.104.

The IP of your service is internal to the cluster which runs on a different network, you cannot address it directly from your host network.

To access directly your pod via the service you can use kubectl port-forward svc/hello-world-service <host-port>:<service-port> where in your case the service port is 5000 and the host-port can be any available port that you wish.

If you cannot allocate port 5000 as the host port you might have an instance of your application that is running.

So to sum up:

kubectl port-forward svc/hello-world-service 5001:5000
curl http://localhost:5001
Hello world from python
  • Related