oBa
oBa

Reputation: 411

Unable to run cronjobs on kubernetes

I'm having an issue whereby cronjob in kubernetes doesnt seem to work. Below is the test Dockerfile used

FROM debian:jessie

RUN apt-get update
RUN apt-get -y install --no-install-recommends cron

RUN echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' | crontab

RUN echo '0-59/2 * * * 0-4 export ENV=dev [email protected]; echo "$(date) ${ENV} ${RECIPIENT}" >> /var/log/cron.log' | crontab

CMD ["cron", "-f", "-L", "15"]

Using native docker to run the above dockerfile, I could see the output in the logfile but not in kubernetes. Checked the events but didn't notice anything unusual.

Below is the replicationcontroller yaml file used

apiVersion: v1
kind: ReplicationController
metadata:
  labels:
    name: cron-test
  name: cron-test
spec:
  replicas: 1
  selector:
    name: cron-test
  template:
    metadata:
      labels:
        name: cron-test
    spec:
      containers:
        - name: cron-test
          image: example/cron-test:latest
          resources:
            limits:
              cpu: 100m
              memory: 512Mi
          imagePullPolicy: Always

Thanks

Upvotes: 2

Views: 1372

Answers (2)

oBa
oBa

Reputation: 411

Somehow I got it working by switching the Dockerfile around

Dockerfile

FROM debian:jessie

RUN apt-get update
RUN apt-get -y install --no-install-recommends cron

COPY . /src
WORKDIR /src

RUN cp run.sh /run.sh \
    && chmod a+x /run.sh \
    && touch /var/log/cron.log

CMD ["/run.sh"]

run.sh

#!/bin/sh

cat << EOF > /tmp/setup-env.sh
export ENV=dev
export [email protected]
EOF

crontab /src/crons.conf
exec cron -f -L 15

crons.conf

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0-59/2 * * * 0-4 . /tmp/setup-env.sh ; echo "$(date) ${ENV} ${RECIPIENT}" >> /var/log/cron.log 2>&1

My guess is that because crontab /src/crons.conf was running at build time and build file-system is different from running file-system i.e building with native docker (rootfs) and running it on kubernetes (overlayfs).

Upvotes: 2

Prashanth B
Prashanth B

Reputation: 5133

You're probably on the wrong day of the week (0-4), I just tried this with all * and observed the expected output.

Upvotes: 0

Related Questions