minikube: Get the URL to use for a given NodePort

2 min read

To avoid having to create an Ingress it is quite handy to use NodePort for testing purposes. But how do we get the URL we can use to connect to a NodePort on a minikube cluster? minikube uses it's own networking layer so it is not as obvious a looking for listening ports using netstat

Let's assume we have created the following NodePort service:

apiVersion: v1
kind: Service
    app: demo
  name: demo
  namespace: default
  - name: 80-80
    port: 80
    protocol: TCP
    targetPort: 80
    app: demo
  type: NodePort

We can apply it as usual:

$ kubectl apply -f demo.yaml
service/demo created

But we won't get an external IP out of the kubectl get svc command, we are just going to be able to see that it has an assigned port by looking at the PORT(S) column:

$ kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
demo         NodePort   <none>        80:30431/TCP   9d
kubernetes   ClusterIP      <none>        443/TCP        9d

To get the URL we will be able to use on out browser we will have to use the minikube CLI. The command we will need to use is minikube service: It's going to tell us the URL to use:

$ minikube service --url demo

Using this URL we will be able to reach the pods behind the service we have created

Posted on 05/02/2021