kubernetes - 使用 Cronjob Kubernetes 集群执行脚本
问题描述
我在 AWS 中有一个 3 节点 K8 v1.21 集群,并正在寻找 SOLID 配置以使用 cronjob 运行脚本。我在这里看到了很多文档,Google 使用 cronjob 和 hostPath 到 Persistent Volumes/Claims 到使用 ConfigMaps,列表就是其中之一。
我不断收到“后退重启失败的容器/CrashLoopBackOff”错误。
任何帮助深表感谢。
我尝试运行的脚本仅用于测试
#! /bin/<br/>
kubectl create deployment nginx --image=nginx
仍然得到同样的错误。
使用 eksctl 创建的 AWS 集群中的这个 hostPath 有效。
apiVersion: v1
kind: Pod
metadata:
name: redis-hostpath
spec:
containers:
- image: redis
name: redis-container
volumeMounts:
- mountPath: /test-mnt
name: test-vol
volumes:
- name: test-vol
hostPath:
path: /test-vol
更新
尝试在新集群上的 GCP 中运行您的配置。我唯一改变的是 /home/script.sh 到 /home/admin/script.sh
您是否在集群上对此进行了测试?
Warning FailedPostStartHook 5m27s kubelet Exec lifecycle hook ([/home/mchung/script.sh]) for Container "busybox" in Pod "dumb-job-1635012900-qphqr_default(305c4ed4-08d1-4585-83e0-37a2bc008487)" failed - error: rpc error: code = Unknown desc = failed to exec in container: failed to create exec "0f9f72ccc6279542f18ebe77f497e8c2a8fd52f8dfad118c723a1ba025b05771": cannot exec in a deleted state: unknown, message: ""
Normal Killing 5m27s kubelet FailedPostStartHook
解决方案
假设您在远程多节点集群中运行它(因为您在问题中提到了 AWS),hostPath
这不是卷挂载的选项。您最好的选择是使用ConfigMap并将其用作卷挂载。
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-script
data:
script.sh: |
# write down your script here
接着:
apiVersion: batch/v1
kind: CronJob
metadata:
name: redis-job
spec:
schedule: '*/5 * * * *'
jobTemplate:
spec:
template:
spec:
containers:
- name: redis-container
image: redis
args:
- /bin/sh
- -c
- /home/user/script.sh
volumeMounts:
- name: redis-data
mountPath: /home/user/script.sh
subPath: script.sh
volumes:
- name: redis-data
configMap:
name: redis-script
希望这可以帮助。如果您遇到任何困难,请告诉我。
更新:
我认为你做错了什么。kubectl
不是您应该从另一个容器/ pod 运行的东西。因为它需要在该容器中存在必要的二进制文件和适当的上下文集。我在下面放了一个工作清单,让您了解将脚本作为 cron 作业的一部分运行的整个概念:
apiVersion: v1
kind: ConfigMap
metadata:
name: script-config
data:
script.sh: |-
name=StackOverflow
echo "I love $name <3"
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: dumb-job
spec:
schedule: '*/1 * * * *' # every minute
jobTemplate:
spec:
template:
spec:
containers:
- name: busybox
image: busybox:stable
lifecycle:
postStart:
exec:
command:
- /home/script.sh
volumeMounts:
- name: some-volume
mountPath: /home/script.sh
volumes:
- name: some-volume
configMap:
name: script-config
restartPolicy: OnFailure
它的作用是每分钟在 STDOUT 中打印一些文本。请注意,我只放置了容器能够执行的命令,kubectl
当然不是开箱即用的容器中存在的命令之一。我希望这足以回答你的问题。
推荐阅读
- python - 为什么我的堆叠游戏的数学不起作用?
- r - 通过使用for循环替换平均df列的NA不起作用并且不会在R中引发任何错误
- pandas - 跨 2 列用于 groupby 的 Isin
- typescript - 如果不存在,CDK 创建资源 - 打字稿
- c# - 如何使用 C# 导出 Excel 文件
- r - 如何将列表中的嵌套变量分配到 R 中的新列表或向量中?
- javascript - MongoError:E11000 重复键错误集合:ProductsRepo.users 索引:username_1 dup key:{ username:null }
- c - 使用像 wchar_t 这样的类型有什么意义?
- angular - 如何在没有组件的情况下从Angular 9+中的延迟加载模块动态注入服务?
- android - 具有视图绑定的片段中的问题获取值 Material TexInputEditText