k8s_api connection time out in Jenkins pipeline

4/11/2019

I wanted to list the namespaces present in a cluster using Kubernetes API i.e. using the list_namespace method.

When I run the below-mentioned code from my Linux machine it works fine.

I created a Jenkins pipeline job and calling the python file as below its throwing Connection Timeout error.

If I do a kubectl get ns in same Jenkins file I am getting the output.

I thought of proxies are not allowing it. But could not find how to fix it.

My Python code:

config.load_kube_config(os.environ['KUBE_CONFIG'])
v1_api = client.CoreV1Api()
api_instance = kubernetes.client.CoreV1Api()

try: 
    api_response = api_instance.list_namespace(limit="1")
    pprint(api_response)
except Exception as e:
    print("Exception when calling CoreV1Api->list_namespace: %s\n" % e)

sys.exit("EXITING")

Command I ran:

export KUBE_CONFIG=${env.WORKSPACE}/.kube/kube_config
python36 listns.py 

Error:

WARNING Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError(': Failed to establish a new connection: [Errno 110] Connection timed out',)': /api/v1/namespaces?limit=1

Exception when calling CoreV1Api->list_namespace: HTTPSConnectionPool(host='XXXXXXX', port=xxxx): Max retries exceeded with url: /api/v1/namespaces?limit=1 (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 110] Connection timed out',))

-- Pritish
jenkins-pipeline
kubernetes
python
python-3.x
urllib3

1 Answer

4/16/2019

Got a fix by passing the proxy using Configuration class:

configuration = kubernetes.client.Configuration()
configuration.proxy='http://www-proxy-<myproxyserver>'

api_instance = kubernetes.client.ApiClient(configuration)
-- Pritish
Source: StackOverflow