Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubectl - list all port forwards across all services?

I'm new to kubernetes and am wondering if there's a way for me to see a list of all currently configured port forwards using kubectl ?

I see there's a kubectl port-forward command, but it doesn't seem to list them, just set one up.

like image 757
Brad Parks Avatar asked Nov 15 '19 14:11

Brad Parks


People also ask

What is kubectl port forwarding?

GitOps. Using Kubectl port forward allows you to quickly access your Kubernetes clusters directly from your local computer. This article will help you understand how exactly kubectl port forward works.

How do I stop kubectl port forwarding?

The port is only forwarded while the kubectl process is running, so you can just kill the kubectl process that's forwarding the port. In most cases that'll just mean pressing CTRL+C in the terminal where the port-forward command is running.

What is Kubefwd?

kubefwd is a command line utility built to port forward multiple services within one or more namespaces on one or more Kubernetes clusters. kubefwd uses the same port exposed by the service and forwards it from a loopback IP address on your local workstation.


2 Answers

kubectl get svc --all-namespaces -o go-template='{{range .items}}{{range.spec.ports}}{{if .nodePort}}{{.nodePort}}{{"\n"}}{{end}}{{end}}{{end}}'

Port forwards are listed in the services the above command should loop through all of them and print them out

like image 180
Dylan Avatar answered Oct 22 '22 09:10

Dylan


In addition to Dylan's answer, small adjustment to use with jq.

kubectl get svc -o json | jq '.items[] | {name:.metadata.name, p:.spec.ports[] } | select( .p.nodePort != null ) | "\(.name): localhost:\(.p.nodePort) -> \(.p.port) -> \(.p.targetPort)"'
"web1: localhost:30329 -> 8080 -> 8080"
"web2: localhost:30253 -> 8080 -> 8080"
like image 21
Oleg Butuzov Avatar answered Oct 22 '22 07:10

Oleg Butuzov