<p>I have a working Cluster with services that all respond behind a helm installed Ingress nGinx running on Azure AKS. <strong><em>This ended up being Azure specific.</em></strong></p>

<blockquote>
  <p>My question is: 
  Why does my connection to the services / pods in this cluster periodically get severed (apparently by some sort of idle timeout), and why does that connection severing appear to also coincide with my Az AKS Browse UI connection getting cut?</p>
</blockquote>

<p>This is an effort to get a final answer on what exactly triggers the time-out that causes the local 'Browse' proxy UI to disconnect from my Cluster (more background on why I am asking to follow).</p>

<p>When working with Azure AKS from the Az CLI you can launch the local Browse UI from the terminal using:</p>

<p><code>az aks browse --resource-group &lt;resource-group&gt; --name &lt;cluster-name&gt;</code></p>

<p>This works fine and pops open a browser window that looks something like this (yay):</p>

<p><a href="https://i.stack.imgur.com/pCr3t.png" rel="noreferrer"><img src="https://i.stack.imgur.com/pCr3t.png" alt="Azure AKS Disconnects Connections entering pods"></a></p>

<p>In your terminal you will see something along the lines of:</p>

<ol>
<li>Proxy running on <a href="http://127.0.0.1:8001/" rel="noreferrer">http://127.0.0.1:8001/</a> Press CTRL+C to close the tunnel... </li>
<li>Forwarding from 127.0.0.1:8001 -> 9090 Forwarding from</li>
<li>[::1]:8001 -> 9090 Handling connection for 8001 Handling connection for 8001 Handling connection for 8001</li>
</ol>

<p>If you leave the connection to your Cluster idle for a few minutes (ie. you don't interact with the UI) you should see the following print to indicate that the connection has timed out:</p>

<blockquote>
  <p>E0605 13:39:51.940659    5704 portforward.go:178] lost connection to pod</p>
</blockquote>

<p>One thing I still don't understand is whether OTHER activity inside of the Cluster can prolong this timeout but regardless once you see the above you are essentially at the same place I am... which means we can talk about the fact that it looks like all of my other connections OUT from pods in that server have also been closed by whatever timeout process is responsible for cutting ties with the AKS browse UI.</p>

<h2>So what's the issue?</h2>

<p>The reason this is a problem for me is that I have a Service running a Ghost Blog pod which connects to a remote MySQL database using an npm package called 'Knex'.  As it happens the newer versions of Knex have a bug (which has yet to be addressed) whereby if a connection between the Knex client and a remote db server is cut and needs to be restored — it doesn't re-connect and just infinitely loads.</p>

<h1>nGinx Error 503 Gateway Time-out</h1>

<p>In my situation that resulted in nGinx Ingress giving me an Error 503 Gateway time-out.  This was because Ghost wasn't responding after the Idle timeout cut the Knex connection — since Knex wasn't working properly and doesn't restore the broken connection to the server properly.</p>

<p><strong><em>Fine.</em></strong>
I rolled back Knex and everything works great.</p>

<blockquote>
  <p>But why the heck are my pod connections being severed from my Database to begin with?  </p>
</blockquote>

<p>Hence this question to hopefully save some future person days of attempting to troubleshoot phantom issues that relate back to Kubernetes (maybe Azure specific, maybe not) cutting connections after a service / pod has been idle for some time.</p>

<h2>Short Answer:</h2>
<blockquote>
<p>Azure AKS automatically deploys an Azure Load Balancer (with public IP address) when you add a new ingress (nGinx / Traefik... ANY Ingress) — that Load Balancer has its settings configured as a 'Basic' Azure LB which has a 4 minute idle connection timeout.</p>
</blockquote>
<p>That idle timeout is both standard AND required (although you MAY be able to modify it, see here: <a href="https://learn.microsoft.com/en-us/azure/load-balancer/load-balancer-tcp-idle-timeout" rel="noreferrer">https://learn.microsoft.com/en-us/azure/load-balancer/load-balancer-tcp-idle-timeout</a>).  That being said there is no way to ELIMINATE it entirely for any traffic that is heading externally OUT from the Load Balancer IP — the longest duration currently supported is 30 minutes.</p>
<p><strong>There is no native Azure way to get around an idle connection being cut.</strong></p>
<p>So as per the original question, the best way (I feel) to handle this is to leave the timeout at 4 minutes (since it has to exist anyway) and then setup your infrastructure to disconnect your connections in a graceful way (when idle) prior to hitting the Load Balancer timeout.</p>
<h2>Our Solutions</h2>
<p>For our Ghost Blog (which hit a MySQL database)  I was able to roll back as mentioned above which made the Ghost process able to handle a DB disconnect / reconnect scenario.</p>
<h1>What about Rails?</h1>
<p>Yep. Same problem.</p>
<p>For a separate Rails based app we also run on AKS which is connecting to a remote Postgres DB (not on Azure) we ended up implementing PGbouncer (<a href="https://github.com/pgbouncer/pgbouncer" rel="noreferrer">https://github.com/pgbouncer/pgbouncer</a>) as an additional container on our Cluster via the awesome directions found here: <a href="https://github.com/edoburu/docker-pgbouncer/tree/master/examples/kubernetes/singleuser" rel="noreferrer">https://github.com/edoburu/docker-pgbouncer/tree/master/examples/kubernetes/singleuser</a></p>
<p>Generally anyone attempting to access a remote database FROM AKS is probably going to need to implement an intermediary connection pooling solution.  The pooling service sits in the middle (PGbouncer for us) and keeps track of how long a connection has been idle so that your worker processes don't need to care about that.</p>
<p>If you start to approach the Load Balancer timeout the connection pooling service will throw out the old connection and make a new fresh one (resetting the timer). That way when your client sends data down the pipe it lands on your Database server as anticipated.</p>
<h2>In closing</h2>
<p>This was an INSANELY frustrating bug / case to track down.  We burned at least 2 dev-ops days figuring the first solution out but even KNOWING that it was probably the same issue we burned another 2 days this time around.</p>
<p>Even elongating the timer beyond the 4 minute default wouldn't really help since that would just make the problem more ephemeral to troubleshoot.  I guess I just hope that anyone who has trouble connecting from Azure AKS / Kubernetes to a remote db is better at googling than I am and can save themselves some pain.</p>
<p><em>Thanks to MSFT Support (Kris you are the best) for the hint on the LB timer and to the dude who put together PGbouncer in a container so I didn't have to reinvent the wheel.</em></p>
