<p>I've tried a number of approaches of accessing my Kubernetes pods.</p>
<ol>
<li><p>I'm able to run this on Docker and am able to access it through http://localhost:1880</p>
</li>
<li><p>I created the following deployment file to run on K8s:</p>
<pre><code>apiVersion: apps/v1
 kind: Deployment
 metadata:
   name: nodered2
   labels:
     app: nodered2
 spec:
   replicas: 2
   selector:
     matchLabels:
       app: nodered2
   template:
     metadata:
       labels:
         app: nodered2
     spec:
       hostNetwork: true
       containers:
       - name: nodered2
         image: localhost:1881/nodered2
         imagePullPolicy: IfNotPresent
         ports:
         - containerPort: 1881
</code></pre>
</li>
<li><p>I'm able to see the deployment</p>
</li>
</ol>
<p>PS C:\01. SolisPLC\01. Software\02. Kubernetes\02. NodeRed K8S&gt;kubectl get deployment</p>
<pre><code>NAME       READY   UP-TO-DATE   AVAILABLE   AGE
nodered2   2/2     2            2           31m
</code></pre>
<ol start="4">
<li>I've read that I need to forward the port as one of the ways to access the pod via my browser, so I've gotten stuck on this error:</li>
</ol>
<blockquote>
<p>PS C:\01. SolisPLC\01. Software\02. Kubernetes\02. NodeRed K8S&gt;
kubectl port-forward deployment/nodered2 1881:443 Forwarding from
127.0.0.1:1881 -&gt; 443 Forwarding from [::1]:1881 -&gt; 443 Handling connection for 1881 E0715 22:00:47.383640   10056 portforward.go:406]
an error occurred forwarding 1881 -&gt; 443: error forwarding port 443 to
pod af7fd02bc1d1374fcfed02adf08f9590dd82cb7230e6db523ff7211cf7dd9b2a,
uid : exit status 1: 2023/07/16 02:00:47 socat[15579] E connect(11,
AF=2 127.0.0.1:443, 16): Connection refused E0715 22:00:47.385258<br />
10056 portforward.go:234] lost connection to pod</p>
</blockquote>
<ol start="5">
<li>I've tried different ports, I've tried to add a load balancer (unsuccessfully), etc.</li>
</ol>
<p>Any help on this would be greatly appreciated; it seems that most tutorials stop at this working &quot;as expected&quot;.</p>
<p>NOTE: All of this is running on my local machine. I can see the deployment inside of the visual Docker app and it's running in VS Code tabs I have for Docker and K8s.</p>

Vladimir Romanov
<p>Below troubleshooting step will help you to resolve your issue:</p>
<p>1.Check which port the pod is listening to by using <code>kubectl describe pod</code>.</p>
<p>2.Ensure forwarding  matches the port number the application inside the pod is listening on.</p>
<p>3.You have mentioned that you can access through http://localhost:1880 but you have specified container port 1881, if it was a typo mistake check logs for more errors.</p>
<p>4.Try using pod instead of deployment in kubectl port forwarding :</p>
<pre><code>kubectl port-forward &lt;pod-name&gt; &lt;your-local-port&gt;:&lt;container-port&gt;
</code></pre>
<p>To get the container port run the below command:</p>
<pre><code>kubectl get pod &lt;pod_name&gt; --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{&quot;\n&quot;}}
</code></pre>
<p>You can find more info in this <a href="https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/" rel="nofollow noreferrer">official doc</a>.</p>
<p>5.As you are using <code>hostnetwork:true</code> try using the service eg: <a href="https://kubernetes.io/docs/concepts/services-networking/ingress/" rel="nofollow noreferrer">ingress,</a> you can find more information about it in this <a href="https://alesnosek.com/blog/2017/02/14/accessing-kubernetes-pods-from-outside-of-the-cluster/" rel="nofollow noreferrer">blog</a> authored by ales.</p>
