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:
- You can delete the service.
- You can either change the selectors in the service manifest or the labels in deployment manifest.
Another way to achieve the same is to scale down the deployment to zero replicas.
Now you can do the same via crontab 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.
So your crontab file would contain the following lines:
Your crontab itself will run a shell script where it will be checking for the elapsed time for the your deployment.
crontab.txt running every minute
Contents of crontab.txt, shouldn't be alike but something like this:
* * * * * /bin/bash script.sh
The contents of script.sh, shouldn't be alike but something like this:
#!/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
The above scripts are just a logic implementation and not ready to use.
Similar logic can be implemented in kubernetes cronjob. Run a shell script there and delete the kubernetes cron after deployment is scaled and ttl is met.