Scheduling function runs

Kubernetes

If you are deploying OpenFaaS to Kubernetes, then we can easily run functions as cron jobs using the aptley named Cron Job resource.

We assume that you have used the recommended install of faas-netes which means that you have OpenFaaS deployed into two namespaces:

  • openfaas for the core componentes (ui, gateway, etc)
  • openfaas-fn for the function deployments

Simple Cron Job

For this example, we use the sample nodeinfo function, which can be deployed using this stack file

  1. # stack.yaml
  2. provider:
  3. name: faas
  4. gateway: http://gateway.openfaas.local
  5.  
  6. functions:
  7. nodeinfo:
  8. lang: dockerfile
  9. handler: node main.js
  10. image: functions/nodeinfo:latest
  11. skip_build: true

and the cli

  1. $ faas deploy

We can then define a Kubernetes cron job to call this function every minute using this manifest file:

  1. # node-cron.yaml
  2. apiVersion: batch/v1beta1
  3. kind: CronJob
  4. metadata:
  5. name: nodeinfo
  6. namespace: openfaas
  7. spec:
  8. schedule: "*/1 * * * *"
  9. concurrencyPolicy: Forbid
  10. successfulJobsHistoryLimit: 1
  11. failedJobsHistoryLimit: 3
  12. jobTemplate:
  13. spec:
  14. template:
  15. spec:
  16. containers:
  17. - name: faas-cli
  18. image: openfaas/faas-cli:0.8.3
  19. args:
  20. - /bin/sh
  21. - -c
  22. - echo "verbose" | faas-cli invoke nodeinfo -g http://gateway.openfaas:8080
  23. restartPolicy: OnFailure

You should also update the image to the latest version of the faas-cli available found via the Docker Hub or faas-cli releases page.

The important thing to notice is that we are using a Docker container with the faas-cli to invoke the function. This keeps the job very generic and easy to generize to other functions.

We schedule the job by applying our manifest

  1. $ kubectl apply -f node-cron.yaml
  2. $ kubectl -n=openfaas get cronjob nodeinfo --watch
  3. NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
  4. nodeinfo */1 * * * * False 0 <none> 42s
  5. nodeinfo */1 * * * * False 1 2s 44s
  6. nodeinfo */1 * * * * False 0 12s 54s
  7. nodeinfo */1 * * * * False 1 2s 1m
  8. nodeinfo */1 * * * * False 0 12s 1m

Unfortunately, there is no one-line command in kubectl for getting the logs from a cron job. Kubernetes creates new Job objects for each run of the CronJob, so we can look up that last run of our CronJob using

  1. $ kubectl -n openfaas get job
  2. NAME DESIRED SUCCESSFUL AGE
  3. nodeinfo-1529226900 1 1 6s

We can use this to then get the output logs

  1. $ kubectl -n openfaas logs -l "job-name=nodeinfo-1529226900"
  2. Hostname: nodeinfo-6fffdb4446-57mzn
  3.  
  4. Platform: linux
  5. Arch: x64
  6. CPU count: 1
  7. Uptime: 997420
  8. [ { model: 'Intel(R) Xeon(R) CPU @ 2.20GHz',
  9. speed: 2199,
  10. times:
  11. { user: 360061300,
  12. nice: 2053900,
  13. sys: 142472900,
  14. idle: 9425509300,
  15. irq: 0 } } ]
  16. { lo:
  17. [ { address: '127.0.0.1',
  18. netmask: '255.0.0.0',
  19. family: 'IPv4',
  20. mac: '00:00:00:00:00:00',
  21. internal: true },
  22. { address: '::1',
  23. netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
  24. family: 'IPv6',
  25. mac: '00:00:00:00:00:00',
  26. scopeid: 0,
  27. internal: true } ],
  28. eth0:
  29. [ { address: '10.4.2.40',
  30. netmask: '255.255.255.0',
  31. family: 'IPv4',
  32. mac: '0a:58:0a:04:02:28',
  33. internal: false },
  34. { address: 'fe80::f08e:d8ff:fecc:9635',
  35. netmask: 'ffff:ffff:ffff:ffff::',
  36. family: 'IPv6',
  37. mac: '0a:58:0a:04:02:28',
  38. scopeid: 3,
  39. internal: false } ] }

This example assumes no authentication is enabled on the gateway.

Multiple Namespaces

In this example, I created the CronJob in the same namespace as the gateway. If we deploy the CronJob in a different namespace, then we need to update the job arguments to accommodate. Fortunately, with Kubernetes DNS, this is simply changing the gateway parameter like this ./faas-cli invoke nodeinfo -g http://gateway.othernamespace:8080

Authentication

If you have enabled basic auth on the gateway, then the invoke command will also need to be updated to first login the cli client. Assuming that you have created the basic auth secret as in the Helm install guide

You could then update the CronJob to login, like this:

  1. # nodeauth-cron.yaml
  2. apiVersion: batch/v1beta1
  3. kind: CronJob
  4. metadata:
  5. name: nodeinfo-auth
  6. namespace: openfaas
  7. spec:
  8. schedule: "*/1 * * * *"
  9. concurrencyPolicy: Forbid
  10. successfulJobsHistoryLimit: 1
  11. failedJobsHistoryLimit: 3
  12. jobTemplate:
  13. spec:
  14. template:
  15. spec:
  16. containers:
  17. - name: faas-cli
  18. image: openfaas/faas-cli:0.8.3
  19. env:
  20. - name: USERNAME
  21. valueFrom:
  22. secretKeyRef:
  23. name: basic-auth
  24. key: basic-auth-user
  25. - name: PASSWORD
  26. valueFrom:
  27. secretKeyRef:
  28. name: basic-auth
  29. key: basic-auth-password
  30. args:
  31. - /bin/sh
  32. - -c
  33. - echo -n $PASSWORD | faas-cli login -g http://gateway.openfaas:8080 -u $USERNAME --password-stdin
  34. - echo "verbose" | faas-cli invoke nodeinfo -g http://gateway.openfaas:8080
  35. restartPolicy: OnFailure

Cron Connector

The cron event connector is an OpenFaaS event-connector which can be used to trigger functions on a timed-basis. It works with all OpenFaaS providers.

Kubernetes

  • Deploy the connector
  1. curl -s https://raw.githubusercontent.com/zeerorg/cron-connector/master/yaml/kubernetes/connector-dep.yml | kubectl create --namespace openfaas -f -
  • Now annotate a function with a topic to give it a schedule
  1. # (Abridged YAML)
  2.  
  3. functions:
  4. nodeinfo:
  5. image: functions/nodeinfo
  6. skip_build: true
  7. annotations:
  8. topic: cron-function
  9. schedule: "*/5 * * * *"

nodeinfo.yaml

  1. faas-cli deploy -f nodeinfo.yaml
  • Or deploy directly from the store
  1. faas-cli store deploy nodeinfo \
  2. --annotation topic="cron-function" \
  3. --annotation schedule="*/5 * * * *"
  • Now check the logs
  1. kubectl logs -n openfaas-fn deploy/nodeinfo -f

You'll see the function invoked every 5 minutes as per the schedule.

To stop the invocations, remove the two annotations or remove the cron-connector deployment.

Docker Swarm

Docker Swarm has no concepts of scheduled tasks or cron, but we have a suitable recommendation which you can use with your OpenFaaS cluster. If you deploy a Jenkins master service, then you can use that to manage your scheduled tasks. It will handle distributed locking, concurrency and queueing.

Example usage:

  • Deploy Swarm service for Jenkins using Official Docker Hub image
  • Define a Freestyle job for each scheduled task
  • Add a CRON entry for the schedule
  • Install the OpenFaaS CLI
  • Run faas-cli login —gateway
  • Invoke the function

Here is an example of how to do this with a Pipeline job.

Alternatively see the above cron-connector example.