minikube expose service on specific port

Viewed 4446

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.

1 Answers

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.

Related