<p>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:</p>
<pre><code>from flask import Flask

app = Flask(__name__)


@app.route(&quot;/&quot;)
def hello():
    return &quot;Hello world from python&quot;


if __name__ == &quot;__main__&quot;:
    app.run(debug=True, host=&quot;0.0.0.0&quot;)
</code></pre>
<p>my docker file:</p>
<pre><code>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 [&quot;python3&quot;, &quot;hello_world.py&quot;]
</code></pre>
<p>my YAML file:</p>
<pre><code>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
</code></pre>
<p>Now, when I run the following:</p>
<pre><code>kubectl get pods
</code></pre>
<p>I get</p>
<pre><code>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
</code></pre>
<p>Then next:</p>
<pre><code>kubectl get svc
</code></pre>
<p>I get...</p>
<pre><code>hello-world-service   ClusterIP   10.97.58.104   &lt;none&gt;        5000/TCP   12h
kubernetes            ClusterIP   10.96.0.1      &lt;none&gt;        443/TCP    3d13h
</code></pre>
<p>I then typed:</p>
<pre><code>kubectl exec -it hello-world-deployment-67d4d6c95c-2cpvx bash
curl localhost:5000
</code></pre>
<p>I get the message:</p>
<pre><code>Hello world from python
</code></pre>
<p>This works as well:</p>
<pre><code>docker run -p 5001:5000 hello-python
</code></pre>
<p>I couldn't use 5000:5000 as it was already bound for some reason.</p>
<p>But I'm not able to connect it via kubectl:</p>
<pre><code>curl 10.97.58.104:5000
</code></pre>
<p>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 <code>mrajancsr/playground:1</code> as that's what I pulled from my private repo and it's still not working.</p>

Rajan Subramanian
<p><code>kubectl</code> is telling you that your service <code>hello-world-service</code> has a <code>ClusterIP</code> of <code>10.97.58.104</code>.</p>
<p>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.</p>
<p>To access directly your pod via the service you can use <code>kubectl port-forward svc/hello-world-service &lt;host-port&gt;:&lt;service-port&gt;</code> where in your case the service port is <code>5000</code> and the <code>host-port</code> can be any available port that you wish.</p>
<p>If you cannot allocate port 5000 as the host port you might have an instance of your application that is running.</p>
<p>So to sum up:</p>
<pre><code>kubectl port-forward svc/hello-world-service 5001:5000
curl http://localhost:5001
Hello world from python
</code></pre>
