skip to Main Content

I am new to K8s, and I am facing issues trying to connect to K8s NodePort service from outside the cluster.

I am unable to load the nginx default page when I try accessing it from my local machine using the URL: http://localhost:31008

I understand this is a common problem and I referred the below solutions,

Cannot access NodePort service outside Kubernetes cluster

Cannot access Microk8s service from browser using NodePort service

However none of them are working for me.

Any guidance on this issue would be really appreciated. Thank you.

Setup:

Server OS: Ubuntu Server on AWS

K8s: minikube

Below is my deployment YAML:

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

Below is my Service YAML:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  selector:
    app: front-end
    name: nginx
  ports:
  - port: 8080
    targetPort: 8080
    nodePort: 31008

Below is the output of the command kubectl get all,

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    For those who are using docker + minikube and facing the above problem, the solution is provided here.


  2. There is an issue in the target port config as Nginx run on default port 80

    apiVersion: v1
    kind: Service
    metadata:
      name: nginx-service
    spec:
      type: NodePort
      selector:
        app: front-end
        name: nginx
      ports:
      - port: 8080
        targetPort: 80
        nodePort: 31008
    

    The target port should be 80

    Config of Nginx :

    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
    

    Ref document : https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search