how to undo a kubectl port-forward

Kubernetes

Kubernetes Problem Overview


If I forwarded a port using

kubectl port-forward mypod 9000:9000

How can I undo that so that I can bind port 9000 with another program?
Additionally, how can I test to see what ports are forwarded?

Kubernetes Solutions


Solution 1 - Kubernetes

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.

Solution 2 - Kubernetes

If it was launch in the background you can use the fg command to access it and then use ctrl + C

Solution 3 - Kubernetes

$ ps -ef|grep port-forward

wyyl1      2013886       1  0 02:18 ?        00:00:10 kubectl port-forward svc/prometheus 9090:9090
wyyl1      2253978 2178606  0 07:58 pts/2    00:00:00 grep --color=auto port-forward

$ kill -9 2013886

Solution 4 - Kubernetes

If it runs in background, consider using pkill to kill processes by their names. Example:

pkill -f "port-forward"

Solution 5 - Kubernetes

ps aux | grep -i kubectl | grep -v grep | awk {'print $2'} | xargs kill

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrideswitchView Question on Stackoverflow
Solution 1 - KubernetesAlex RobinsonView Answer on Stackoverflow
Solution 2 - KubernetesDoctorView Answer on Stackoverflow
Solution 3 - KubernetesHuang JinlongView Answer on Stackoverflow
Solution 4 - KubernetesfizmaxView Answer on Stackoverflow
Solution 5 - KubernetesanrajmeView Answer on Stackoverflow