kubectl 端口转发超时问题
kubectl port forwarding timeout issue
在使用 kubectl port-forward 函数时,我能够成功地将本地端口转发到远程端口。但是,似乎在闲置几分钟后连接断开。不知道为什么会这样。
这是用于转发的命令:
kubectl --namespace somenamespace port-forward somepodname 50051:50051
错误信息:
Forwarding from 127.0.0.1:50051 -> 50051
Forwarding from [::1]:50051 -> 50051
E1125 17:18:55.723715 9940 portforward.go:178] lost connection to pod
希望能够保持连接
似乎有 5 分钟的超时,可以用 kubelet
参数覆盖:
https://github.com/kubernetes/kubernetes/issues/19231
If you want to pass something higher than 5 minutes (or unlimited) into your kubelets, you can specify the streaming-connection-idle-timeout
. E.g.
--streaming-connection-idle-timeout=4h
to set it to 4 hours. Or:
--streaming-connection-idle-timeout=0
to make it unlimited.
(DEPRECATED: This parameter should be set via the config file specified by the Kubelet's --config flag. See https://kubernetes.io/docs/tasks/administer-cluster/kubelet-config-file/ for more information.)
将kube的streaming-connection-idle-timeout
设置为0应该是一个正确的解决方案,但是如果你不想改变什么,可以使用while-do构造
格式:while true; do <<YOUR COMMAND HERE>>; done
所以只需在 CLI 中输入:while true; do kubectl --namespace somenamespace port-forward somepodname 50051:50051; done
应该可以让 kubectl 在连接丢失时重新连接
为了windows做出这样的蝙蝠(求大神见谅)
:1
oc port-forward PODNAME 8003:8080
goto 1
我通过保持连接有效解决了这个问题,例如使用 curl 或 nc.
转发端口:
kubectl --namespace somenamespace port-forward somepodname 50051:50051
在另一个终端中,通过每 10 秒连接到端口来保持连接:
while true ; do nc -vz 127.0.0.1 50051 ; sleep 10 ; done
如果您 运行 您的 Kubernetes 集群位于负载均衡器(如 HAProxy)后面,可能会发生 kubelet 中配置的超时大于 HAProxy 中配置的超时。
比如Kubelet中的streamingConnectionIdleTimeout
设置默认是4h:
$ kubectl proxy --port=8001 &
$ NODE_NAME="XXXX"; curl -sSL "http://localhost:8001/api/v1/nodes/${NODE_NAME}/proxy/configz" | jq '.kubeletconfig|.kind="KubeletConfiguration"|.apiVersion="kubelet.config.k8s.io/v1beta1"' | grep streaming
"streamingConnectionIdleTimeout": "4h0m0s",
但是如果在 HAProxy(或您首选的 LB)中,您有这些设置:
defaults
timeout client 1m
timeout server 1m
...
如果您在应用程序上没有任何 activity,则尝试执行端口转发将超时:
$ date; kubectl port-forward service/XXXX 1234:80
Mon Jul 5 10:58:20 CEST 2021
Forwarding ...
# after a minute
E0705 10:59:21.217577 64160 portforward.go:233] lost connection to pod
为了解决这个问题,一个解决方案是增加超时(小心这个,因为根据您的集群,它可能会产生不良影响)或在直接连接到 API 服务器(如果您的环境允许的话)。
在使用 kubectl port-forward 函数时,我能够成功地将本地端口转发到远程端口。但是,似乎在闲置几分钟后连接断开。不知道为什么会这样。
这是用于转发的命令:
kubectl --namespace somenamespace port-forward somepodname 50051:50051
错误信息:
Forwarding from 127.0.0.1:50051 -> 50051
Forwarding from [::1]:50051 -> 50051
E1125 17:18:55.723715 9940 portforward.go:178] lost connection to pod
希望能够保持连接
似乎有 5 分钟的超时,可以用 kubelet
参数覆盖:
https://github.com/kubernetes/kubernetes/issues/19231
If you want to pass something higher than 5 minutes (or unlimited) into your kubelets, you can specify the
streaming-connection-idle-timeout
. E.g.--streaming-connection-idle-timeout=4h
to set it to 4 hours. Or:--streaming-connection-idle-timeout=0
to make it unlimited. (DEPRECATED: This parameter should be set via the config file specified by the Kubelet's --config flag. See https://kubernetes.io/docs/tasks/administer-cluster/kubelet-config-file/ for more information.)
将kube的streaming-connection-idle-timeout
设置为0应该是一个正确的解决方案,但是如果你不想改变什么,可以使用while-do构造
格式:while true; do <<YOUR COMMAND HERE>>; done
所以只需在 CLI 中输入:while true; do kubectl --namespace somenamespace port-forward somepodname 50051:50051; done
应该可以让 kubectl 在连接丢失时重新连接
为了windows做出这样的蝙蝠(求大神见谅)
:1
oc port-forward PODNAME 8003:8080
goto 1
我通过保持连接有效解决了这个问题,例如使用 curl 或 nc.
转发端口:
kubectl --namespace somenamespace port-forward somepodname 50051:50051
在另一个终端中,通过每 10 秒连接到端口来保持连接:
while true ; do nc -vz 127.0.0.1 50051 ; sleep 10 ; done
如果您 运行 您的 Kubernetes 集群位于负载均衡器(如 HAProxy)后面,可能会发生 kubelet 中配置的超时大于 HAProxy 中配置的超时。
比如Kubelet中的streamingConnectionIdleTimeout
设置默认是4h:
$ kubectl proxy --port=8001 &
$ NODE_NAME="XXXX"; curl -sSL "http://localhost:8001/api/v1/nodes/${NODE_NAME}/proxy/configz" | jq '.kubeletconfig|.kind="KubeletConfiguration"|.apiVersion="kubelet.config.k8s.io/v1beta1"' | grep streaming
"streamingConnectionIdleTimeout": "4h0m0s",
但是如果在 HAProxy(或您首选的 LB)中,您有这些设置:
defaults
timeout client 1m
timeout server 1m
...
如果您在应用程序上没有任何 activity,则尝试执行端口转发将超时:
$ date; kubectl port-forward service/XXXX 1234:80
Mon Jul 5 10:58:20 CEST 2021
Forwarding ...
# after a minute
E0705 10:59:21.217577 64160 portforward.go:233] lost connection to pod
为了解决这个问题,一个解决方案是增加超时(小心这个,因为根据您的集群,它可能会产生不良影响)或在直接连接到 API 服务器(如果您的环境允许的话)。