Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minikube exposes random port for loadbalancer type. Can I define specific port?

After deploying nginx (or any application for that matter) , I try to expose it using the following config file:

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 80
  selector:
    app: nginx

The result:

C:\install\kube>kubectl get service -l app=nginx
NAME      TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
nginx     LoadBalancer   10.108.193.215   <pending>     80:31035/TCP   21h

Note the port number 31035 is randomly assigned. Is it possible to define a specific port?

Versions:

C:\install\kube>minikube version
minikube version: v0.24.1

C:\install\kube>kubectl version
Client Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.0", GitCommit:"6e937839ac04a38cac63e6a7a306c5d035fe7b0a", GitTreeState:"clean", BuildDate:"2017-09-28T22:57:57Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"windows/amd64"}
Server Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.0", GitCommit:"0b9efaeb34a2fc51ff8e4d34ad9bc6375459c4a4", GitTreeState:"clean", BuildDate:"2017-11-29T22:43:34Z", GoVersion:"go1.9.1", Compiler:"gc", Platform:"linux/amd64"}
like image 870
thinkbot Avatar asked Mar 07 '23 22:03

thinkbot


1 Answers

LoadBalancer service uses NodePort internally. Yes, it possible to specify a NodePort for a service. There are two ways to do it:

  1. Using nodePort: 31036 in the YAML spec file for the service.
  2. Run kubectl edit <service name>, edit the nodePort field under ports and save it.

Keep in mind that NodePort must be set to a number in the flag-configured range range 30000-32767. Otherwise, kubernetes throws an error. This NodePort range can be changed using the flag --service-node-port-range passed to kube-apiserver per https://kubernetes.io/docs/reference/generated/kube-apiserver/:

  --service-node-port-range portRange      A port range to reserve for services
                                           with NodePort visibility. 
                                           Example: '30000-32767'. Inclusive at both ends
                                           of the range. (default 30000-32767)
like image 127
Vikram Hosakote Avatar answered Mar 13 '23 14:03

Vikram Hosakote