6

Is it possible to expose a service on a specific port using minikube?

kubectl expose deployment my-deployment --type=NodePort --port=80 does not throw an error but when calling

minikube service my-deployment --url

it results in something like:

http://192.168.99.100:31512 and it is not available on port 80 but on port 31512 instead.

Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124

1 Answers1

17

Valid ports for minikube of type nodePort by default are 30000-32767 according to https://kubernetes.io/docs/concepts/services-networking/service/#nodeport

I was able to specify a particular port (here: 30000 in that range using this services.yaml:

apiVersion: v1
kind: Service
metadata:
  name: my-deployment 
  labels:
    app: my-deployment 
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30000
    protocol: TCP
  selector:
    app: my-deployment 

When starting minikube this way:

minikube start --extra-config=apiserver.service-node-port-range=80-30000, port 80 can be used as well:

apiVersion: v1
kind: Service
metadata:
  name: my-deployment 
  labels:
    app: my-deployment 
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 80
    protocol: TCP
  selector:
    app: my-deployment 

minikube service my-deployment --url now returns http://192.168.99.100:80 as expected and the application is available on port 80.

Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124
  • what if I want to change that range for the node ports outside of minikube? I'm trying this https://kubernetes.io/docs/tasks/administer-cluster/reconfigure-kubelet/ , changing the port, but even after restarting the kubelet service, nothing changes – tuxErrante Jan 28 '21 at 19:25
  • I'm pretty sure I followed this and it's not working. That is, it keeps giving me a random port and not the one specified in 'nodePort'. Is it possible something has changed with this? – JimmyJames Nov 17 '21 at 19:15
  • This answer solved my issue: https://stackoverflow.com/a/55110218/1708543 – JimmyJames Nov 17 '21 at 22:08