<p>I'm trying to backup a MySQL database running inside a k8s cluster (k3s). This cluster is running locally on a Raspberry Pi. I've built a custom image based on Alpine Linux that contains a script to create the backup using mysqldump:</p>
<p><code>kubectl exec &lt;pod_name&gt; -n &lt;namespace_name&gt; -- /usr/bin/mysqldump -u &lt;db_user&gt; --password=&lt;db_password&gt; --verbose &lt;db_name&gt; &gt; &lt;file_name&gt;</code></p>
<p>When I run the mysqldump command from inside the database pod, it completes successfully after 10-15 seconds. But when this command gets executed from inside the Alpine pod it somehow takes a lot longer (2m40s). At that point it stops/aborts the kubectl exec command (due to a timeout?) and the script uploads a corrupt sql file because the mysqldump command wasn't finished with the backup.</p>
<p>Expected verbose output:</p>
<pre><code>-- Connecting to localhost...
-- Retrieving table structure for table _prisma_migrations...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table database...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table recycleBin...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table user...
-- Sending SELECT query...
-- Retrieving rows...
-- Disconnecting from localhost...
</code></pre>
<p>Received verbose output:</p>
<pre><code>-- Connecting to localhost...
-- Retrieving table structure for table _prisma_migrations...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table database...
-- Sending SELECT query...
-- Retrieving rows...
-- Retrieving table structure for table recycleBin...
-- Sending SELECT query...
-- Retrieving rows...
</code></pre>
<p>I have 2 questions:</p>
<ol>
<li>Why does the mysqldump command take so much longer in the Alpine pod compared to the database pod?</li>
<li>Why isn't the kubectl exec command waiting until the mysqldump command has finished taking the backup? Why does it suddenly decide it's time to disconnect and move on?</li>
</ol>

Tristan Diependael
<p>It is possible that you're getting disconnected because <code>kubectl</code> thinks the connection is dead due to no data from the other side.</p>
<p>Instead of:</p>
<p><code>kubectl exec &lt;pod_name&gt; -n &lt;namespace_name&gt; -- /usr/bin/mysqldump -u &lt;db_user&gt; --password=&lt;db_password&gt; --verbose &lt;db_name&gt; &gt; &lt;file_name&gt; </code></p>
<p>Try:</p>
<p><code>kubectl exec &lt;pod_name&gt; -n &lt;namespace_name&gt; -- /usr/bin/mysqldump -u &lt;db_user&gt; --password=&lt;db_password&gt; --verbose &lt;db_name&gt; | tee &lt;file_name&gt;</code></p>
<p>This will instead output to stdout as well as sending to the file. This will guarantee that kubectl will see data coming back and will not disconnect you if this is a no-data problem. The downside to this is that you are getting the entire SQL data being pumped back to you on stdout</p>
<p>The other alternative (and one I would personally recommend), is installing something like <code>screen</code> or <code>tmux</code> or another terminal multiplexer and doing the dump in that so you can disconnect from the pod and not worry about kubectl disconnecting you.</p>
<p>EDIT: Just to clarify, I meant installing the multiplexer inside the pod (e.g. inside the docker image you use to create the pod)</p>
