<p>Basically I am trying to do is play around with pod lifecycle and check if we can do some cleanup/backup such as copy logs before the pod terminates.</p>

<p>What I need :
Copy logs/heapdumps from container to a hostPath/S3 before terminating</p>

<p>What I tried:</p>

<p>I used a preStop hook with a bash command to echo a message (just to see if it works !!). Used terminationGracePeriodSeconds with a delay to preStop and toggle them to see if the process works. Ex. keep terminationGracePeriodSeconds:30 sec (default) and set preStop command to sleep by 50 sec and the message should not be generated since the container will be terminated by then. This works as expected.</p>

<p>My questions:</p>

<ul>
<li>what kind of processes are allowed(recommended) for a preStop hook? As copying logs/heapdumps of 15 gigs or more will take a lot of time. This time will then be used to define terminationGracePeriodSeconds</li>
<li>what happens when preStop takes more time than the set gracePeriod ?
(in case logs are huge say 10 gigs)</li>
<li>what happens if I do not have any hooks but still set terminationGracePeriodSeconds ? will the container remain up until that grace time ? </li>
</ul>

<p>I found this article which closely relates to this but could not follow through <a href="https://github.com/kubernetes/kubernetes/issues/24695" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/issues/24695</a></p>

<p>All inputs appreciated !!</p>

Prathamesh dhanawade
<blockquote>
  <p>what kind of processes are allowed(recommended) for a preStop hook? As copying logs/heapdumps of 15 gigs or more will take a lot of time. This time will then be used to define terminationGracePeriodSeconds</p>
</blockquote>

<p>Anything goes here, it's more of an opinion and how you would like your pods to linger around. Another option is to let your pods terminate and store your data in some place (i.e, AWS S3, EBS) where data will persist past the pod lifecycle then use something like <a href="https://kubernetes.io/docs/tasks/job/" rel="nofollow noreferrer">Job</a> to clean up the data, etc.</p>

<blockquote>
  <p>what happens when preStop takes more time than the set gracePeriod? (in case logs are huge say 10 gigs)</p>
</blockquote>

<p>Your preStop will not complete which may mean incomplete data or data corruption.</p>

<blockquote>
  <p>what happens if I do not have any hooks but still set terminationGracePeriodSeconds ? will the container remain up until that grace time ?</p>
</blockquote>

<p>This explains would be the sequence:</p>

<ul>
<li>A SIGTERM signal is sent to the main process in each container, and a “grace period” countdown starts.</li>
<li>If a container doesn’t terminate within the grace period, a SIGKILL signal will be sent and the container.</li>
</ul>
