Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

minikube expose service on specific port

Tags:

minikube

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.

like image 614
Alexander Zeitler Avatar asked Nov 13 '18 01:11

Alexander Zeitler


People also ask

How do you expose a port in minikube?

Using minikube service with tunnel Services of type NodePort can be exposed via the minikube service <service-name> --url command. It must be run in a separate terminal window to keep the tunnel open. Ctrl-C in the terminal can be used to terminate the process at which time the network routes will be cleaned up.

What port does minikube use?

Now you can use minikube to retrieve a URL that is accessible outside of the container. This URL will allow you to access the hello-app service that is running on port 8080 inside your cluster.

How do I check my minikube service?

minikube service help Simply type service help [path to command] for full details.

How do I access a Kubernetes service from outside?

Ways to connect You have several options for connecting to nodes, pods and services from outside the cluster: Access services through public IPs. Use a service with type NodePort or LoadBalancer to make the service reachable outside the cluster. See the services and kubectl expose documentation.


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.

like image 164
Alexander Zeitler Avatar answered Apr 05 '23 20:04

Alexander Zeitler