<p>I am going to access a Kubernetes NodePort service in MacOS in the browser, running on minikube, but I can't.</p>
<p>This is the Service definition file:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30004
  selector:
    app: myapp
</code></pre>
<p>And this is the Pod difinition file:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: nginx-2
  labels:
    env: production
    app: myapp
spec:
  containers:
    - name: nginx
      image: nginx
</code></pre>
<p>And this is the Deployment definition file:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
  labels:
    tier: frontend
    app: myapp
spec:
  selector:
    matchLabels:
      env: production
  replicas: 6
  template:
    metadata:
      name: nginx-2
      labels:
        env: production
    spec:
      containers:
        - name: nginx
          image: nginx
</code></pre>
<p>I cannot access the NodePort service using this URL on the browser:
http://localhost:30004</p>
<p>Also by entering minikube ip instead of localhost in the top command, a timeout happens.</p>
<p>And finally, by running the below command:</p>
<pre><code>minikube service myapp-service --url
</code></pre>
<p>A sample output like this is generated:</p>
<p><a href="http://127.0.0.1:53751" rel="nofollow noreferrer">http://127.0.0.1:53751</a>
❗  Because you are using a Docker driver on darwin, the terminal needs to be open to run it.</p>
<p>But an the below error is shown to the user:
<strong>The connection was reset</strong></p>
<p><strong>Update</strong> : <em>The issue is because the label app:myapp was not mentioned in the Deployment definition file -&gt; template section</em></p>

Pezhman Parsaee
<p>Look, your are not exposing your localhost to the nodePort. You are exposing the NodePort on your Kupernetes cluster.</p>
<p>So you have to access it http://nodeip:nodePort.</p>
<p>Think about what is localhost.</p>
<p>You have localhost on your Pc.
You have localhost on the VM (minkube node).
You have localhost in each container running inside your cluster.</p>
<p>If you want to use your Pc localhost to access the port inside a container in a Pod you can do this:</p>
<pre><code>kubectl port-forward svc/serviceName reachablePortFromyourPc:containerPort
</code></pre>
<p>For example:</p>
<pre><code>kubectl port-forward svc/serviceName 80:80
</code></pre>
<p>This starts a port forwarding. As long it is running you can access it from your browser.</p>
<p>This is good only for testing.</p>
<p>To access the NodePort use.</p>
<pre><code>minikubeIp:NodePort
</code></pre>
