Kubernetes:是否可以将卷装载到作为CronJob运行的容器上? [英] Kubernetes: Is it possible to mount volumes to a container running as a CronJob?

查看:102
本文介绍了Kubernetes:是否可以将卷装载到作为CronJob运行的容器上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个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

相应的错误:

错误:验证"cron-applications.yaml"时出错:验证时出错 数据:找到v2alpha1.CronJobSpec的无效字段卷;如果你 选择忽略这些错误,使用关闭验证 --validate = false

error: error validating "cron-applications.yaml": error validating data: found invalid field volumes for v2alpha1.CronJobSpec; if you choose to ignore these errors, turn validation off with --validate=false

我找不到任何资源可以证明这是可能的.因此,如果不可能的话,如何解决将应用程序代码放入正在运行的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 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运行的容器上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆