问题描述
我正在尝试创建一个Kubernetes CronJob,以便每分钟运行一个应用程序.
I'm attempting to create a Kubernetes CronJob to run an application every minute.
一个先决条件是我需要将我的应用程序代码放入运行在CronJob内的容器中.我认为最好的方法是使用一个持久卷,即pvclaim,然后定义该卷并将其安装到容器上.我已经使用Pod中运行的容器成功完成了此操作,但是在CronJob中似乎不可能吗?这是我尝试的配置:
A prerequisite is that I need to get my application code onto the container that runs within the CronJob. I figure that the best way to do so is to use a persistent volume, a pvclaim, and then defining the volume and mounting it to the container. I've done this successfully with containers running within a Pod, but it appears to be impossible within a CronJob? Here's my attempted configuration:
apiVersion: batch/v2alpha1
kind: CronJob
metadata:
name: update_db
spec:
volumes:
- name: application-code
persistentVolumeClaim:
claimName: application-code-pv-claim
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: update-fingerprints
image: python:3.6.2-slim
command: ["/bin/bash"]
args: ["-c", "python /client/test.py"]
restartPolicy: OnFailure
相应的错误:
我找不到任何资源可以证明这是可能的.因此,如果不可能的话,如何解决将应用程序代码放入正在运行的CronJob中的问题?
I can't find any resources that show that this is possible. So, if not possible, how does one solve the problem of getting application code into a running CronJob?
推荐答案
A CronJob 使用PodTemplate以及其他基于Pods的其他东西,并且可以使用Volumes.您将卷规范直接放置在CronJobSpec中,而不是 PodSpec ,像这样使用它:
A CronJob uses a PodTemplate as everything else based on Pods and can use Volumes. You placed your Volume specification directly in the CronJobSpec instead of the PodSpec, use it like this:
apiVersion: batch/v2alpha1
kind: CronJob
metadata:
name: update_db
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: update-fingerprints
image: python:3.6.2-slim
command: ["/bin/bash"]
args: ["-c", "python /client/test.py"]
volumeMounts:
- name: application-code
mountPath: /where/ever
restartPolicy: OnFailure
volumes:
- name: application-code
persistentVolumeClaim:
claimName: application-code-pv-claim
这篇关于Kubernetes:是否可以将卷装载到作为CronJob运行的容器上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!