(O+P)ut

アウトプット



(O+P)ut

エンジニアのアウトプット

【Kubernetes】Client.Timeout or context cancellation while reading bodyというエラーの解消方法

スポンサーリンク

事象

kubectlコマンドでPodを展開しようとするも下記エラーでエラーとなる。

E0111 21:53:18.429065    5884 request.go:1001] Unexpected error when reading response body: net/http: request canceled (Client.Timeout or context cancellation while reading body)
error: unexpected error when reading response body. Please retry. Original error: net/http: request canceled (Client.Timeout or context cancellation while reading body)
環境情報
  • kubernetes v1.19

原因/対応策

Clusterとの通信状況が悪いためタイムアウトのエラーが発生している。
kubectlコマンドの--request-timeoutオプションを利用して明示的にタイムアウト値を大きな値に設定することで同エラーを回避する。

$ kubectl apply -f test.yaml --request-timeout="50"

以下、補足です。

補足

kubectlの実行時の詳細ログを確認するとtimeout=32s というリクエストで通信が行われていることが分かります。

$ kubectl apply -f test-r-m.yaml -v=6
I0111 22:08:16.114221   25044 loader.go:375] Config loaded from file:  C:\cygwin64\home\xx/.kube/config
I0111 22:08:30.256684   25044 round_trippers.go:443] GET https://apiserver:31203/openapi/v2?timeout=32s 200 OK in 14101 milliseconds
E0111 22:08:48.167223   25044 request.go:1001] Unexpected error when reading response body: context deadline exceeded (Client.Timeout or context cancellation while reading
body)
F0111 22:08:48.168919   25044 helpers.go:115] error: unexpected error when reading response body. Please retry. Original error: context deadline exceeded (Client.Timeout or context cancellation while reading body)
goroutine 1 [running]:...

通信状況が悪い場合、この値に抵触してYAMLが読み込まれないため、解決策に記載した通りで明示的にオプションで大きい値を指定します。

$ kubectl apply -f test-r-m.yaml -v=6 --request-timeout="50"
I0111 22:18:48.669611   21552 loader.go:375] Config loaded from file:  C:\cygwin64\home\xx/.kube/config
I0111 22:18:51.030776   21552 round_trippers.go:443] GET https://apiserver:31203/openapi/v2?timeout=50s 200 OK in 2319 milliseconds
...

そうすることtimeout=に続く数値に変更が入り、タイムアウトの制限を緩和することが可能になります。

ちなみにタイムアウトの制限ではなくネットワークが断続的に切れてしまうと以下のエラーが出るのでその場合はネットワークが安定する環境に移動することをお勧めします。

E0111 22:22:56.431657   22488 request.go:1001] Unexpected error when reading response body: read tcp xx:62579->yy:32429: wsarecv: An existing connection was forcibly closed by the remote host.
error: unexpected error when reading response body. Please retry. Original error: read tcp xx:62579->yy:32429: wsarecv: An existing connection was forcibly closed by the remote host.

以上、同様の事象でお困りな方の参考になれば幸いです。