<p>We sometimes use Python scripts to spin up and monitor Kubernetes Pods running on Google Kubernetes Engine using the <a href="https://github.com/kubernetes-client/python" rel="nofollow noreferrer">Official Python client library for kubernetes</a>. We also enable auto-scaling on several of our node pools. </p>

<p>According to <a href="https://cloudplatform.googleblog.com/2017/11/Cutting-Cluster-Management-Fees-on-Google-Kubernetes-Engine.html" rel="nofollow noreferrer">this</a>, "Master VM is automatically scaled, upgraded, backed up and secured". The post also seems to indicate that some automatic scaling of the control plane / Master VM occurs when the node count increases from 0-5 to 6+ and potentially at other times when more nodes are added.</p>

<p>It seems like the control plane can go down at times like this, when many nodes have been brought up. In and around when this happens, our Python scripts that monitor pods via the control plane often crash, seemingly unable to find the KubeApi/Control Plane endpoint triggering some of the following exceptions:</p>

<blockquote>
  <p>ApiException, urllib3.exceptions.NewConnectionError, urllib3.exceptions.MaxRetryError.</p>
</blockquote>

<p>What's the best way to handle this situation? Are there any properties of the autoscaling events that might be helpful?</p>

<p>To clarify what we're doing with the Python client is that we are in a loop reading the status of the pod of interest via <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#read_namespaced_pod" rel="nofollow noreferrer">read_namespaced_pod</a> every few minutes, and catching exceptions similar to the <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md#read_namespaced_pod" rel="nofollow noreferrer">provided example</a> (in addition we've tried also catching exceptions for the underlying <em>urllib</em> calls). We have also added retrying with exponential back-off, but things are unable to recover and fail after a specified max number of retries, even if that number is high (e.g. keep retrying for >5 minutes). </p>

<p>One thing we haven't tried is recreating the <code>kubernetes.client.CoreV1Api</code> object on each retry. Would that make much of a difference?</p>

Kevin Lam
<p>When a nodepool size changes, depending on the size, this can initiate a change in the size of the master. Here are the <a href="https://kubernetes.io/docs/admin/cluster-large/#size-of-master-and-master-components" rel="nofollow noreferrer"><strong>nodepool sizes mapped with the master sizes</strong></a>. In the case where the nodepool size requires a larger master, automatic scaling of the master is initiated on GCP. During this process, the master will be unavailable for approximately 1-5 minutes. Please note that these events are not available in <a href="https://cloud.google.com/logging/docs/view/overview#sd-accts-for-logging" rel="nofollow noreferrer"><strong>Stackdriver Logging</strong></a>. </p>

<p>At this point all API calls to the master will fail, including the ones from the Python API client and kubectl. However after 1-5 minutes the master should be available and calls from both the client and kubectl should work. I was able to test this by scaling my cluster from 3 node to 20 nodes and for 1-5 minutes the master wasn't available . 
I obtained the following errors from the Python API client: </p>

<pre><code>Max retries exceeded with url: /api/v1/pods?watch=False (Caused by NewConnectionError('&lt;urllib3.connection.VerifiedHTTPSConnection object at&gt;: Failed to establish a new connection: [Errno 111] Connection refused',)) 
</code></pre>

<p>With kubectl I had : </p>

<pre><code>“Unable to connect to the server: dial tcp” 
</code></pre>

<p>After 1-5 minutes the master was available and the calls were successful. There was no need to recreate <strong><em>kubernetes.client.CoreV1Api</em></strong> object as this is just an API endpoint. </p>

<p>According to your description, your master wasn't accessible after 5 minutes which signals a potential issue with your master or setup of the Python script. To troubleshoot this further on side while your Python script runs, you can check for availability of master by running any kubectl command. </p>
