How to use concurrencyPolicy for GKE cron job correctly?

1/28/2019

I set concurrencyPolicy to Allow, here is my cronjob.yaml:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: gke-cron-job
spec:
  schedule: '*/1 * * * *'
  startingDeadlineSeconds: 10
  concurrencyPolicy: Allow
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            run: gke-cron-job
        spec:
          restartPolicy: Never
          containers:
            - name: gke-cron-job-solution-2
              image: docker.io/novaline/gke-cron-job-solution-2:1.3
              env:
                - name: NODE_ENV
                  value: 'production'
                - name: EMAIL_TO
                  value: 'novaline.dulin@gmail.com'
                - name: K8S_POD_NAME
                  valueFrom:
                    fieldRef:
                      fieldPath: metadata.name
              ports:
                - containerPort: 8080
                  protocol: TCP

After reading docs: https://cloud.google.com/kubernetes-engine/docs/how-to/cronjobs

I still don't understand how to use concurrencyPolicy.

How can I run my cron job concurrency?

Here is the logs of cron job:

nodejs-gcp [master]kubectl logs -l run=gke-cron-job

> gke-cron-job-solution-2@1.0.2 start /app
> node ./src/index.js

config:  { ENV: 'production',
  EMAIL_TO: 'novaline.dulin@gmail.com',
  K8S_POD_NAME: 'gke-cron-job-1548660540-gmwvc',
  VERSION: '1.0.2' }
[2019-01-28T07:29:10.593Z] Start daily report
send email:  { to: 'novaline.dulin@gmail.com', text: { test: 'test data' } }

> gke-cron-job-solution-2@1.0.2 start /app
> node ./src/index.js

config:  { ENV: 'production',
  EMAIL_TO: 'novaline.dulin@gmail.com',
  K8S_POD_NAME: 'gke-cron-job-1548660600-wbl5g',
  VERSION: '1.0.2' }
[2019-01-28T07:30:11.405Z] Start daily report
send email:  { to: 'novaline.dulin@gmail.com', text: { test: 'test data' } }

> gke-cron-job-solution-2@1.0.2 start /app
> node ./src/index.js

config:  { ENV: 'production',
  EMAIL_TO: 'novaline.dulin@gmail.com',
  K8S_POD_NAME: 'gke-cron-job-1548660660-8mn4r',
  VERSION: '1.0.2' }
[2019-01-28T07:31:11.099Z] Start daily report
send email:  { to: 'novaline.dulin@gmail.com', text: { test: 'test data' } }

As you can see, the timestamp indicates that the cron job is not concurrency.

-- slideshowp2
kubernetes

1 Answer

1/30/2019

It's because you're reading the wrong documentation. CronJobs aren't a GKE-specific feature. For the full documentation on CronJob API, refer to the Kubernetes documentation: https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/#concurrency-policy (quoted below).

Concurrency policy decides whether a new container can be started while the previous CronJob is still running. If you have a CronJob that runs every 5 minutes, and sometimes the Job takes 8 minutes, then you may run into a case where multiple jobs are running at a time. This policy decides what to do in that case.

Concurrency Policy

The .spec.concurrencyPolicy field is also optional. It specifies how to treat concurrent executions of a job that is created by this cron job. the spec may specify only one of the following concurrency policies:

  • Allow (default): The cron job allows concurrently running jobs
  • Forbid: The cron job does not allow concurrent runs; if it is time for a new job run and the previous job run hasn’t finished yet, the cron job skips the new job run
  • Replace: If it is time for a new job run and the previous job run hasn’t finished yet, the cron job replaces the currently running job run with a new job run

Note that concurrency policy only applies to the jobs created by the same cron job. If there are multiple cron jobs, their respective jobs are always allowed to run concurrently.

-- AhmetB - Google
Source: StackOverflow