Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out which ports are available in Kubernetes?

Tags:

kubernetes

I want to run several services on different ports in a kubernetes cluster and I would like to know how to check which ports are available and wouldn't cause any conflicts with my services. I would also like to know the names of the services on each port so I can understand my configuration better.

like image 809
JVE999 Avatar asked Oct 24 '25 00:10

JVE999


1 Answers

There was similar question related to verify which NodePorts are already in use. You can find it here.

This command will display all ports from all namespaces which are NodePort type and are already in use.

$ kubectl get svc --all-namespaces -o go-template='{{range .items}}{{range.spec.ports}}{{if .nodePort}}{{.nodePort}}{{"\n"}}{{end}}{{end}}{{end}}'
30007
30107
30207
30307
30407
30676

However, pleas keep in mind that Kubernetes will not allow you to use second time this same NodePort.

$ cat<<eof|kubectl apply -f -
> apiVersion: v1
> kind: Service
> metadata:
>   name: my-service-test
> spec:
>   type: NodePort
>   selector:
>     app: MyApp
>   ports:
>     - port: 80
>       targetPort: 80
>       nodePort: 30307
> eof
The Service "my-service-test" is invalid: spec.ports[0].nodePort: Invalid value: 30307: provided port is already allocated

In addition, there are some very specific scenarios, when you would like to use ports outside default range mentioned in K8s docs. There is a workaround, if you will add a special flag --service-node-port-range with requested range, admission controller allow you to create NodePort with Ports 80 and 443. For detailed information how to do it, check this answer.

like image 136
PjoterS Avatar answered Oct 26 '25 17:10

PjoterS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!