<p>I am new to K8s, and I am facing issues trying to connect to K8s NodePort service from outside the cluster.</p>
<p>I am unable to load the nginx default page when I try accessing it from my local machine using the URL: http://localhost:31008</p>
<p>I understand this is a common problem and I referred the below solutions,</p>
<p><a href="https://stackoverflow.com/questions/66212319/cannot-access-nodeport-service-outside-kubernetes-cluster">Cannot access NodePort service outside Kubernetes cluster</a></p>
<p><a href="https://stackoverflow.com/questions/62918016/cannot-access-microk8s-service-from-browser-using-nodeport-service">Cannot access Microk8s service from browser using NodePort service</a></p>
<p>However none of them are working for me.</p>
<p>Any guidance on this issue would be really appreciated. Thank you.</p>
<p>Setup:</p>
<p>Server OS: Ubuntu Server on AWS</p>
<p>K8s: minikube</p>
<p>Below is my deployment YAML:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: front-end
      name: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: front-end
        name: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
</code></pre>
<p>Below is my Service YAML:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  selector:
    app: front-end
    name: nginx
  ports:
  - port: 8080
    targetPort: 8080
    nodePort: 31008
</code></pre>
<p>Below is the output of the command <code>kubectl get all</code>,</p>
<p><a href="https://i.stack.imgur.com/VTQHj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VTQHj.png" alt="enter image description here" /></a></p>

<p>There is an issue in the target port config as <strong>Nginx</strong> run on default port <strong>80</strong></p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  selector:
    app: front-end
    name: nginx
  ports:
  - port: 8080
    targetPort: 80
    nodePort: 31008
</code></pre>
<p>The <strong>target port</strong> should be <code>80</code></p>
<p>Config of Nginx :</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 2
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80
</code></pre>
<p>Ref document : <a href="https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/</a></p>
