How do I mail the output of a kubernetes cronjob?

4/8/2019

I've setup a cronjob in Openshift. I can see its logs through the Web Console but I want to receive a mail containing the output of the job whenever it completes. How do I go about implementing this?

-- vatsal
kubernetes
kubernetes-cronjob
openshift

2 Answers

4/9/2019

You can add sth. like "mailx" to your cronjob image and forward the output.

In the following example "mailgateway.default.svc" is a service route to a mailgateway outside the cluster:

<output_producing_command> | mailx -E -v -s "Subject" -S smtp=smtp://mailgateway.default.svc:25 -S from="foo.bar@baz.com (Foo Bar)" abc.def@ghi.com 2>&1

-E If an outgoing message does not contain any text in its first or only message part, do not send it but discard it silently, effectively setting the skipemptybody variable at program startup. This is useful for sending messages from scripts started by cron(8).

-- Milde
Source: StackOverflow

4/8/2019

There are no built-in instruments for such kind of reporting. You will need to implement own cronjob wrapper which emails you a result.

Kubernetes wasn't designed as a full-featured scheduled job runner, cron jobs are just one of the API extensions added later, and it's still in beta.

-- Max Lobur
Source: StackOverflow