1

I am new to kubernate and minikube , I am trying to use service of type NodePort to be able to access the pods from host machine browser, the issue that minikube ignores the specified port in the service yaml file and instead minikube exposes a new port number randomly.

here is my configuration

apiVersion: v1
kind: Service
metadata:
  name: test
spec:
  type: NodePort
  selector:
    app: test
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30036 ==> here is port

as you can see the specified port is 30036, and that what I get when describe the service

kubectl describe services test
-----------------------------------

Name:                     test
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=test
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.104.58.44
IPs:                      10.104.58.44
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30036/TCP
Endpoints:                192.168.50.2:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

but when try to run minikube service --url, I am getting a different port number which I think it is randomly generated

minikube service test --url
------------------------------
* Starting tunnel for service test.
|-----------|------------|-------------|------------------------|
| NAMESPACE |    NAME    | TARGET PORT |          URL           |
|-----------|------------|-------------|------------------------|
| default   | test       |             | http://127.0.0.1:56758 |
|-----------|------------|-------------|------------------------|
http://127.0.0.1:56758

and here is the output of

kubectl get svc -A
-------------------------
NAMESPACE     NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGE
default       test         NodePort    10.104.58.44   <none>        80:30036/TCP             4h4m
default       kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP                  3d1h
kube-system   kube-dns     ClusterIP   10.96.0.10     <none>        53/UDP,53/TCP,9153/TCP   3d1h

so how can I make minikube use the specified port "30036" instead of generating new random one.

Mtaraby
  • 167
  • 1
  • 13
  • Whats is the output of `kubectl get svc -A`? – CodeWizard Oct 24 '21 at 11:53
  • @CodeWizard I updated the question based on your request – Mtaraby Oct 24 '21 at 12:17
  • Did you find a resolution to this? I'm running into the same problem and it's highly irritating. – JimmyJames Nov 17 '21 at 19:21
  • One thing I found is that when I set the valid port range on minikube start, kubectl apply will balk if I set nodePort outside of that range. It doesn't do that if I uses the default range and use a port outside of that. But it doesn't change that I keep getting random ports assigned. – JimmyJames Nov 17 '21 at 19:23

1 Answers1

-1

According to the documentation:

Valid ports for minikube of type nodePort by default are 30000-32767.If you want a specific port number, you can specify a value in the nodePort field. The control plane will either allocate you that port or report that the API transaction failed. This means that you need to take care of possible port collisions yourself. You also have to use a valid port number, one that's inside the range configured for NodePort use.

Note that this Service is visible as <NodeIP>:spec.ports[*].nodePort and .spec.clusterIP:spec.ports[*].port. If the --nodeport-addresses flag for kube-proxy or the equivalent field in the kube-proxy configuration file is set, <NodeIP> would be filtered node IP(s).

When starting minikube this way:

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

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

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

Check out a similar case and refer to this link for more information.

Fariya Rahmat
  • 2,123
  • 3
  • 11
  • 1
    I think the OP's port range was inside the default range and the modification you suggested isn't relevant to their problem. – Ali Tou Oct 25 '21 at 07:29
  • Are you able to curl target port $ curl 192.168.50.2:30036. Refer the [stackpost](https://stackoverflow.com/questions/60556096/unable-to-get-clusterip-service-url-from-minikube) for more information.Let me know if this helps. – Fariya Rahmat Nov 22 '21 at 09:09