kubectl port-forward在连接丢失时不返回



kubectl port-forwardhelp表示The forwarding session ends when the selected pod terminates, and rerun of the command is needed to resume forwarding.

虽然当pod终止时它不会自动重新连接,但命令也不返回,只是挂起错误:

E0929 11:57:50.187945   62466 portforward.go:400] an error occurred forwarding 8000 -> 8080: error forwarding port 8080 to pod a1fe1d167955e1c345e0f8026c4efa70a84b9d46029037ebc5b69d9da5d30249, uid : network namespace for sandbox "a1fe1d167955e1c345e0f8026c4efa70a84b9d46029037ebc5b69d9da5d30249" is closed
Handling connection for 8000
E0929 12:02:44.505938   62466 portforward.go:400] an error occurred forwarding 8000 -> 8080: error forwarding port 8080 to pod a1fe1d167955e1c345e0f8026c4efa70a84b9d46029037ebc5b69d9da5d30249, uid : failed to find sandbox "a1fe1d167955e1c345e0f8026c4efa70a84b9d46029037ebc5b69d9da5d30249" in store: not found

我希望它返回,这样我就可以处理这个错误,并使脚本将重新运行它。

有任何方法或解决方法如何做到这一点?

根据GitHub上Kubernetes问题页面上描述的信息,我可以假设这是您的情况下的正常行为:端口转发连接不能在pod删除时取消,因为服务器端REST连接器内部没有连接管理。

从kubectl一直到kubelet保持连接,即使pod不存在。

我们将在端口转发上代理websocket连接kubectl-> kubeapisserver ->kubelet。

递归函数?

kpf(){ kubectl port-forward $type/$object $LOCAL:$REMOTE $ns || kpf; }

也检查这个

最新更新