Scheduling django command with kubernetes cronjobs

9/4/2019

I am trying to schedule a django command with kubernetes cronjobs my cron job yaml file looks like this:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
 name: dostuff
 namespace: development
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
spec:
  template:
    spec:
      containers:
        - name: dostuff
          image: "gcr.io/my_project/app:stg_onlycronimage"
          command: ["python /usr/src/app/manage.py dostuff"]
          env:
            - name: DATABASE_HOST
              value: 127.0.0.1
            - name: DATABASE_USER
              valueFrom:
                secretKeyRef:
                  name: literal-token
                  key: user
            - name: DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: literal-token
                  key: password
      restartPolicy: OnFailure

and my docker file looks like this:

FROM python:3.7

ENV PYTHONUNBUFFERED 1

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

ADD requirements.txt /usr/src/app/requirements.txt

RUN pip install -r requirements.txt

ADD ./ /usr/src/app

ENV PORT 8080

EXPOSE 8080

RUN rm -rf staticfiles

RUN python ./manage.py collectstatic --noinput

CMD gunicorn app.wsgi 0.0.0.0:8080

So basically I am using the same Dockerfile (Image) for my cron which I am using to run my application and the application is working fine.

But when I am deploying my cron job by command kubectl apply -f cronjob.yaml cron does not work and container keeps on crashing. In the logs I see

container_linux.go:247: starting container process caused "exec: \"python /usr/src/app/manage.py dostuff\": stat python /usr/src/app/manage.py dostuff: no such file or directory"

I've tried changing to different basic commands like "ls" but no luck I get the same error. Been looking into internet for a day but did not find any solution. Any kind of help is truly appreciated and thanks in advance.

-- user2912611
cron
django
docker
kubernetes
python

0 Answers