minikube: Get the URL to use for a given NodePort

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?

Let's assume we are going to create the following NodePort service:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: demo
  name: demo
  namespace: default
spec:
  ports:
  - name: 80-80
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    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, just it's assigned port:

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

For minikube what we can do is to use minikube service to get the URL to use:

$ minikube service --url demo
http://192.168.49.2:30431

Posted on 05/02/2021