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:
- Using cargo:
| |
- SOON! Using kubectl krew, the plugin manager for kubectl:
| |
- Manually compile it by yourself:
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)serviceorsvc
For example, to list ports exposed by pods in the kube-system namespace:
| |
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:
| |
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:
Port discovery: No need to manually parse through the output of
kubectl describeor other verbose commands.kubectl-portsextracts only the port information.Readable output: The table format makes it easy to quickly scan and find the port details you’re looking for.
Namespace filtering: Use the
-nflag to limit the output to resources in a specific namespace.Pods and services: list ports for either.
Conclusion
The project is open source. Contribute or suggest improvements on the GitHub repository.