无法在kubernetes上运行cronjobs [英] Unable to run cronjobs on kubernetes

查看:66
本文介绍了无法在kubernetes上运行cronjobs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,即kubernetes中的cronjob似乎不起作用. 下面是使用的测试Dockerfile

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 RECIPIENT=email@example.com; echo "$(date) ${ENV} ${RECIPIENT}" >> /var/log/cron.log' | crontab

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

使用本地docker运行上述dockerfile,我可以在日志文件中看到输出,但在kubernetes中看不到.检查了事件,但没有发现任何异常.

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.

下面是所使用的复制控制器yaml文件

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

谢谢

推荐答案

我可以通过切换Dockerfile使其正常工作

Somehow I got it working by switching the Dockerfile around

Dockerfile

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 RECIPIENT=email@example.com
EOF

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

crons.conf

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

我的猜测是,因为crontab /src/crons.conf在构建时运行,并且构建文件系统不同于运行文件系统,即使用本机docker(rootfs)进行构建并在kubernetes(overlayfs)上运行.

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).

这篇关于无法在kubernetes上运行cronjobs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆