Ubuntu: Cannot access Kubernetes service from outside cluster

10/20/2021

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,

https://stackoverflow.com/questions/66212319/cannot-access-nodeport-service-outside-kubernetes-cluster

https://stackoverflow.com/questions/62918016/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

-- stack underflow
docker
kubernetes
kubernetes-service

2 Answers

10/21/2021

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

-- stack underflow
Source: StackOverflow

10/20/2021

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/

-- Harsh Manvar
Source: StackOverflow