kubectl-ports: list exposed ports in Kubernetes

Port
Photo by Cezary Kukowka on Unsplash

Finding the ports exposed by pods and services means digging through resource definitions or running several commands. I created kubectl-ports, a kubectl plugin that retrieves the exposed ports information and presents it in a table.

What is kubectl-ports?

kubectl-ports is a tool that leverages the kube-rs SDK to retrieve information about running pods in a Kubernetes cluster. It filters out the relevant port details from each pod and prints the final result in an easy-to-read table. You no longer need to comb through verbose output or chain commands.

Why I Built It

I frequently found myself needing to check what ports were exposed by various Kubernetes resources, whether for debugging network issues, verifying service configurations, or simply getting a high-level overview. The standard kubectl commands provided the information, but not in a friendly format. I wanted the port details at a glance.

Installation and Usage

kubectl-ports can be installed in two ways:

  1. Using cargo:
1
cargo install kubectl-ports
  1. SOON! Using kubectl krew, the plugin manager for kubectl:
1
kubectl krew install ports
  1. Manually compile it by yourself:
1
2
3
git clone https://github.com/widnyana/kubectl-ports-rs
cd kubectl-ports-rs
make build install clean

After installation, ensure you have kubectl configured to connect to your Kubernetes cluster.

Run the command followed by the resource type you want to list ports for. The currently supported resources are:

  • pod (default)
  • service or svc

For example, to list ports exposed by pods in the kube-system namespace:

1
kubectl ports -n kube-system

This will output a table like:

+===============+==========================================+========================+==================+===================+
|   Namespace   |                 Pod Name                 |     Container name     |  Container Port  |     Port Name     |
+===============+==========================================+========================+==================+===================+
|  kube-system  |  fluentbit-gke-46dgf                     |  fluentbit             |  2020/TCP        |  metrics          |
+---------------+------------------------------------------+------------------------+------------------+-------------------+
|  kube-system  |  fluentbit-gke-46dgf                     |  fluentbit-gke         |  2021/TCP        |  metrics          |
+---------------+------------------------------------------+------------------------+------------------+-------------------+
...

To list ports exposed by services:

1
kubectl ports -n kube-system svc

Which will output something like:

+===============+========================+=============+=================+=============+===============+================+=============+
|   Namespace   |      Service Name      |    Type     |   Cluster IP    |  Port Name  |  Target Port  |  Exposed Port  |  Node Port  |
+===============+========================+=============+=================+=============+===============+================+=============+
|  kube-system  |  default-http-backend  |  NodePort   |  172.23.5.200   |    http     |  8080/TCP     |  80/TCP        |  32725/TCP  |
+---------------+------------------------+-------------+-----------------+-------------+---------------+----------------+-------------+
...  

Benefits

What kubectl-ports gives you:

  1. Port discovery: No need to manually parse through the output of kubectl describe or other verbose commands. kubectl-ports extracts only the port information.

  2. Readable output: The table format makes it easy to quickly scan and find the port details you’re looking for.

  3. Namespace filtering: Use the -n flag to limit the output to resources in a specific namespace.

  4. Pods and services: list ports for either.

Conclusion

The project is open source. Contribute or suggest improvements on the GitHub repository.