<p>I'm looking for an alternative solution to k8s resources ttl. From the docs, this feature is not supproted. </p>

<p>more info:</p>

<ul>
<li>I need ttl for dployments &amp; services,</li>
<li>the deployment may be still running but I will still want to delete it.</li>
</ul>

<hr>

<p>possible solutions:</p>

<ol>
<li>setup an additional monitoring-deployment in the cluster and truck down other resources and stop/delete them at specified time.

<ul>
<li>disadvantage: I would like to avoid starting additional resources.</li>
</ul></li>
<li>k8s cronjobs - create cronjobs for a single use. 

<ul>
<li>disadvantage: how will I stop the cronjobs after they are done?</li>
</ul></li>
</ol>

<p>Is there any other solution? I'm finding it hard to belive that I'm the first one who needs this feature.</p>

Stav Alfi
<p>What would the use-case for this feature? Now there's no way you can stop a deployment. What you can do is stop the app serving the traffic which can be done in two ways:</p>

<ol>
<li>You can <em>delete</em> the service.</li>
<li>You can either change the <em>selectors in the service</em> manifest or the <em>labels in deployment</em> manifest. </li>
</ol>

<p>Another way to achieve the same is to scale down the deployment to <strong>zero replicas</strong>.</p>

<p>Now you can do the same via <strong>crontab</strong> file in Linux. You should be running it at specified time and because it's a one time job for you and you want to delete the crontab after deployment is scaled, you can put the same condition in crontab too. </p>

<p>So your crontab file would contain the following lines:</p>

<p>Your crontab itself will run a <em>shell script</em> where it will be checking for the <strong>elapsed time</strong> for the your deployment.
<strong>crontab.txt</strong> running every minute
Contents of <em>crontab.txt</em>, shouldn't be alike but something like this:</p>

<pre><code>* * * * * /bin/bash script.sh
</code></pre>

<p>The contents of <strong>script.sh</strong>, shouldn't be alike but something like this:</p>

<pre><code>#!/bin/bash
ttl=kubectl get deployment deployment-name -n deployment-namespace | awk '{print $5}' | grep -v "AGE"

##It will give output in the format as xxxh or xxxdxxxh, where xxx can be
##any number. You will have to come up with converting them to hours, and
##then compare it with your value.

if [ "$ttl" gt your-value ] #checks for TTL condition
then
kubectl scale --replicas=0 deployment deployment-name -n deployment-namespace #scales deployment to zero
crontab -r username #deletes crontab job
</code></pre>

<p>The above scripts are just a logic implementation and not ready to use.</p>

<p>Similar logic can be implemented in <strong>kubernetes cronjob</strong>. Run a shell script there and delete the kubernetes cron after <em>deployment is scaled</em> and <em>ttl is met</em>.</p>
