Is it possible to stop a job in Kubernetes without deleting it

10/1/2018

Because Kubernetes handles situations where there's a typo in the job spec, and therefore a container image can't be found, by leaving the job in a running state forever, I've got a process that monitors job events to detect cases like this and deletes the job when one occurs.

I'd prefer to just stop the job so there's a record of it. Is there a way to stop a job?

-- Brent212
jobs
kubernetes

3 Answers

2/15/2020

You can suspend cronjobs by using the suspend attribute. From the Kubernetes documentation:

https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/#suspend

-- Dan
Source: StackOverflow

10/1/2018

Not really, no such mechanism exists in Kubernetes yet afaik.

You can workaround is to ssh into the machine and run a: (if you're are using Docker)

# Save the logs
$ docker log <container-id-that-is-running-your-job> 2>&1 > save.log
$ docker stop <main-container-id-for-your-job>

It's better to stream log with something like Fluentd, or logspout, or Filebeat and forward the logs to an ELK or EFK stack.

In any case, I've opened this

-- Rico
Source: StackOverflow

10/2/2018

1) According to the K8S documentation here.

Finished Jobs are usually no longer needed in the system. Keeping them around in the system will put pressure on the API server. If the Jobs are managed directly by a higher level controller, such as CronJobs, the Jobs can be cleaned up by CronJobs based on the specified capacity-based cleanup policy.

Here are the details for the failedJobsHistoryLimit property in the CronJobSpec.

This is another way of retaining the details of the failed job for a specific duration. The failedJobsHistoryLimit property can be set based on the approximate number of jobs run per day and the number of days the logs have to be retained. Agree that the Jobs will be still there and put pressure on the API server.

This is interesting. Once the job completes with failure as in the case of a wrong typo for image, the pod is getting deleted and the resources are not blocked or consumed anymore. Not sure exactly what kubectl job stop will achieve in this case. But, when the Job with a proper image is run with success, I can still see the pod in kubectl get pods.

2) Another approach without using the CronJob is to specify the ttlSecondsAfterFinished as mentioned here.

Another way to clean up finished Jobs (either Complete or Failed) automatically is to use a TTL mechanism provided by a TTL controller for finished resources, by specifying the .spec.ttlSecondsAfterFinished field of the Job.

-- Praveen Sripati
Source: StackOverflow