在Kubernetes中的Pod中运行Cronjob [英] Running A cronjob in a pod in Kubernetes
问题描述
我有一个在kubernetes集群中运行的后端nodeJS应用程序.现在,我想每月运行两个计划的cron作业. cron作业位于JS文件中.我如何使用Kubernetes创建一个工作来每月在运行该服务的Pod中运行那些JS文件?
此链接提供了有关其工作原理的基本理解,但我对如何针对特定服务和特定pod运行它感到有些困惑.
https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/#编写一份cron-job-spec
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
推荐答案
不幸的是,您无法运行应用程序容器中的CronJob .
Unfortunately, you cannot run the CronJob inside a container of your application.
您唯一可以做的就是创建一个容器,其中将包含您的cronjob和运行它的必要环境,并安排CronJob运行该pod.
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.
以下是配置示例:
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
这篇关于在Kubernetes中的Pod中运行Cronjob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!