如何在 kubernetes-client-python 中减少 kubernetes 集群的重试计数



我需要使用 kubernetes-client-python 减少不可用/已删除的 kubernetes 集群的重试计数,目前默认为 3。

WARNING Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x00000000096E3860>: Failed to establish a new connection: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond',)': /api/v1/pods
WARNING Retrying (Retry(total=1,....... /api/v1/pods
WARNING Retrying (Retry(total=0,....... /api/v1/pods

重试 3 次后,它会引发异常。

有没有办法减少计数。

示例代码

from kubernetes import client, config
config.load_kube_config(config_file='location-for-kube-config')
v1 = client.CoreV1Api()
ret = v1.list_pod_for_all_namespaces()
for i in ret.items:
print("%st%st%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

可悲的是,这似乎是不可能的,因为:

Python 客户端使用 urlib3 PoolManager 发出请求,如您所见

https://github.com/kubernetes-client/python/blob/master/kubernetes/client/rest.py#L162

r = self.pool_manager.request(method, url,
body=request_body,
preload_content=_preload_content,
timeout=timeout,
headers=headers)

在引擎盖下,它使用带有默认参数的 urlopen,如您所见

https://urllib3.readthedocs.io/en/1.2.1/pools.html#urllib3.connectionpool.HTTPConnectionPool.urlopen

urlopen(..., retries=3, ...)

所以现在有办法在这里传递其他值 - 你必须分叉官方库来实现这一点。

此后,他们在此 github 问题中向客户端对象添加了重试参数。

eks_config = client.Configuration()
eks_config.retries = 1

最新更新