gpt4 book ai didi

kubernetes - 如何在 Kubernetes cronjob 中执行脚本 shell

转载 作者:行者123 更新时间:2023-12-05 09:32:30 27 4
gpt4 key购买 nike

我想使用 CronJob 在 Kubernetes 中运行一个 shell 脚本,这是我的 CronJon.yaml 文件:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- /home/admin_/test.sh
restartPolicy: OnFailure

CronJob 已创建(kubectl apply -f CronJob.yaml)当我获得 cronjob 列表时,我可以看到 cron 作业 ( kubectl get cj ),当我运行“kubectl get pods”时,我可以看到正在创建 pod,但 pod 崩溃了。谁能帮助我学习如何在 Kubernetes 中创建 CronJob?

enter image description here

enter image description here

最佳答案

正如评论中正确指出的那样,您需要提供脚本文件才能通过您的 CronJob 执行它。您可以通过将文件安装在 volume 中来做到这一点.例如,您的 CronJob 可能如下所示:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- /myscript/test.sh
volumeMounts:
- name: script-dir
mountPath: /myscript
restartPolicy: OnFailure
volumes:
- name: script-dir
hostPath:
path: /path/to/my/script/dir
type: Directory

上面的例子展示了如何使用 hostPath卷的类型,以便安装脚本文件。

关于kubernetes - 如何在 Kubernetes cronjob 中执行脚本 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68014653/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com