Kubernetes: run a job on node reboot only once

2/5/2020

Is there a way to run a job only once upon machine reboot in Kubernetes?

Thought of running a cronjob as static pod but seems kubelet does not like it.

Edit: Based on the replies, I'd like to clarify. I'm only looking at doing this through native Kubernetes. I'm ok writing a cronjob in Kubernetes but I need this to run only once and upon node reboot.

-- Hem
cron
jobs
kubelet
kubernetes
kubernetes-pod

3 Answers

2/6/2020

Not sure your platform.

For example, in AWS, ec2 instance has user_data (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html)

It will run commands on Your Linux/Windows Instance at Launch

You should be fine to find the similar solutions for other cloud providers or on-premise servers.

-- BMW
Source: StackOverflow

2/6/2020

Consider using CronJob for this. It takes the same format as normal cron scheduler on linux. Besides the usual format (minute / hour / day of month / month / day of week) that is widely used to indicate a schedule, cron scheduler also allows the use of @reboot. This directive, followed by the absolute path to the script, will cause it to run when the machine boots.

-- shashank tyagi
Source: StackOverflow

2/6/2020

If I understand you correctly you should consider using DaemonSet:

A DaemonSet ensures that all (or some) Nodes run a copy of a Pod. As nodes are added to the cluster, Pods are added to them. As nodes are removed from the cluster, those Pods are garbage collected. Deleting a DaemonSet will clean up the Pods it created.

That way you could create a container with a job that would be run from a DaemonSet.

Alternatively you could consider DaemonJob:

This is an example CompositeController that's similar to Job, except that a pod will be scheduled to each node, similar to DaemonSet.

Also there are:

Kubebuilder is a framework for building Kubernetes APIs using custom resource definitions (CRDs).

and:

Metacontroller is an add-on for Kubernetes that makes it easy to write and deploy custom controllers in the form of simple scripts.

But the first option that I have provided would be easier to implement in my opinion.

Please let me know if that helped.

-- OhHiMark
Source: StackOverflow