<p>What do I need to do in order to get my local browser to and request a resource to a web service running inside Minikube instance running locally on my machine?</p>
<p>I am getting a <code>Connection refused</code> when trying to <code>kubectl port-forward</code>.</p>
<p>My workflow is:</p>
<ol>
<li>Creating Dockerfile with web service on</li>
<li>Start minikube in docker</li>
<li>Build docker image</li>
<li>Import image locally into Minikube</li>
<li>Created a deployment with one container and a NodePort service</li>
<li>Applied deployment/service</li>
<li>Ran kubectl port-forward (to hopefully forward requests to my container)</li>
<li>Open browser to 127.0.0.1:31000</li>
</ol>
<h1>Port Configuration Summary</h1>
<ul>
<li>Dockerfile:
<ul>
<li>Expose: 80</li>
<li>uvicorn: 80</li>
</ul>
</li>
<li>Deployment
<ul>
<li>NodePort Service:
<ul>
<li>Port: 80</li>
<li>Target Port: 80</li>
<li>Node Port: 31000</li>
</ul>
</li>
</ul>
</li>
<li>Kubectl Command: 8500:31000</li>
<li>Browser: 127.0.0.1:8500</li>
</ul>
<h1>Setup and run through</h1>
<h2>dev.dockerfile (Step 1)</h2>
<pre><code>FROM python:3.11-buster # Some Debian Python image...  I built my own

COPY ../sources/api/ /app/
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt

ENV PYTHONPATH=/app/

EXPOSE 80

CMD [&quot;uvicorn&quot;, &quot;app.main:app&quot;, &quot;--proxy-headers&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;80&quot;]
</code></pre>
<h2>Build Sequence (Steps 2 to 4)</h2>
<pre><code># 2 - start minikube
minikube start --bootstrapper=kubeadm --vm-driver=docker
minikube docker-env

## 3 - build image
docker build -f ../../service1/deploy/dev.dockerfile ../../service1 -t acme-app.service1:latest 

## 4 - load image into minikube
minikube image load acme-app.service1:latest
</code></pre>
<h2>Deployment (Step 5 and 6)</h2>
<p><strong>deployment.yaml</strong></p>
<pre class="lang-yaml prettyprint-override"><code>---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: acme-service-1-deployment
  namespace: acme-app-dev
  labels:
    app: service-1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: service-1
  template:
    metadata:
      labels:
        app: service-1
    spec:
      containers:
        - name: service1-container
          image: docker.io/library/acme-app.service1:latest
          imagePullPolicy: Never
          ports:
          - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: service-1-service
  namespace: acme-app-dev
spec:
  type: NodePort
  selector:
    app: service-1
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 31000
</code></pre>
<p><strong>Deploy</strong></p>
<pre><code>kubectl apply -f deployment.yaml
</code></pre>
<h1>kubectl port forward (Step 7)</h1>
<p><strong>Find Pod</strong></p>
<pre><code>kubectl get pods -n acme-app-dev
NAME                                         READY   STATUS    RESTARTS   AGE
acme-service-1-deployment-76748d7ff6-llcsr   1/1     Running   0          11s
</code></pre>
<p><strong>Port Forward to pod</strong></p>
<pre><code>port-forward acme-service-1-deployment-76748d7ff6-llcsr 8500:31000 -n acme-app-dev
Forwarding from 127.0.0.1:8500 -&gt; 31000
Forwarding from [::1]:8500 -&gt; 31000
</code></pre>
<h1>Test in Browser (Step 8)</h1>
<p>Open favorite browser and navigate to 127.0.0.1:31000.</p>
<p>The console running the port forward now outputs:</p>
<pre><code>E0123 14:54:16.208010   25932 portforward.go:406] an error occurred forwarding 8500 -&gt; 31000: error forwarding port 31000 to pod d4c0fa6cb16ce02335a05cad904fbf2ab7818e2073d7c7ded8ad05f193aa37e7, uid : exit status 1: 2023/01/23 14:54:16 socat[39370] E connect(5, AF=2 127.0.0.1:31000, 16): Connection refused
E0123 14:54:16.213268   25932 portforward.go:234] lost connection to pod
</code></pre>
<h1>What have I looked at?</h1>
<p>I've tried looking through the docs on kubernetes website as well as issues on here (yes there are similar).  This is pretty similar - although no marked answer and still an issue by the looks of it.  I couldn't see a solution for my issue here.</p>
<p><a href="https://stackoverflow.com/questions/67512043/nodeport-exposed-port-connection-refused">NodePort exposed Port connection refused</a></p>
<p>I am running Minikube on Windows and I'm just setting out on a kubernetes journey.</p>
<p>The image itself works in docker from a docker compose.  I can see the pod is up and running in minikube from the logs (minikube dashboard).</p>

Andez
<p>You got your wires crossed:</p>
<ul>
<li>The pod is listening on port 80</li>
<li>The NodePort service is listening on port 31000 on the node, but its underlying ClusterIP service is listening on port 80 as well.</li>
<li>You are trying to port-forward to port 31000 on the <strong>Pod</strong>. This will not work.</li>
</ul>
<p>Call one of the following instead:</p>
<ul>
<li><code>kubectl port-forward -n acme-app-dev deploy/acme-service-1-deployment 8500:80</code></li>
<li>or <code>kubectl port-forward -n acme-app-dev service/service-1-service 8500:80</code></li>
<li>or use <code>minikube service -n acme-app-dev service-1-service</code> and use the provided URL.</li>
</ul>
