Running A cronjob in a pod in Kubernetes

元气小坏坏 提交于 2021-02-10 07:48:56

问题


I have a backend nodeJS application running in a kubernetes cluster. Now I want to run two cron jobs to be scheduled every month. The cron jobs are in a JS file. How do I create a job that runs those JS files in the pod running that service every month using Kubernetes ?
This link gives a basic understanding of how it works but I am a little confused on how to run it for a specific service and on a specific pod
https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/#writing-a-cron-job-spec


回答1:


Unfortunately, you cannot run the CronJob inside a container of your application.

The only thing you can do is create a container which will contain your cronjob and necessary environment for running it and schedule to run that pod by a CronJob.

Here is an example of the configuration:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *" # syntax is a same as in system cron jobs
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: image_with_cronjob_code # here is an image with your cronjob
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure


来源:https://stackoverflow.com/questions/49431902/running-a-cronjob-in-a-pod-in-kubernetes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!