Skip to main content
Kubernetes

Kubernetes NodePort Service is not Exposed on Minikube Cluster - Solved

Aakash Verma

Problem Statement

I have defined Deployment and Service on my machine in deployment.yaml as shown below.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3  
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app
          image: docker.io/username/my-app
          ports:
            - containerPort: 8000
---
apiVersion: v1
kind: Service
metadata:
  name: my-app-svc
spec:
  selector:
    app: my-app
  type: NodePort 
  ports:
  - name: http
    port: 8000
    targetPort: 8000
    nodePort: 30080
    protocol: TCP
kubectl apply -f deployment.yaml

After applying the deployment, I am not able to access the service endpoints from inside the browser.

Solution

Don't worry. Let's resolve it for you. Please run the below command on your machine and you're done.

minikube service my-app-svc  --url

You will probably see the below output.

http://127.0.0.1:62711
❗  Because you are using a Docker driver on darwin, the terminal needs to be open to run it.

Now, keep this terminal open and open your browser to verify if the service is accessible or not.

Paste the URL http://127.0.0.1:62711/your-api-end-point and hit enter. There you go. You are probably seeing the expected output. Please make sure that you change the URL accordingly.

Okay. What did happen?

minikube service my-apps-svc --url runs as a process, creating a tunnel to the cluster. The command exposes the service directly to any program running on the host operating system.

Keeping the previous terminal open, fire another command to verify if ssh happened successfully.

ps -ef | grep [email protected]

Note the output. You will see something like this.

ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -N [email protected] -p 62222 -i /Users/aakashverma/.minikube/machines/minikube/id_rsa -L 62711:10.104.197.48:8000

Note this 62711:10.104.197.48:8000 => TUNNEL_PORT:SERVICE_CLUSTER_IP:TARGET_PORT

minikube service my-app-svc --url, this command exposes the service directly to any program running on the host operating system.

I hope you enjoyed this.

Thank you.