首页 > 解决方案 > 使用 Cronjob Kubernetes 集群执行脚本

问题描述

我在 AWS 中有一个 3 节点 K8 v1.21 集群,并正在寻找 SOLID 配置以使用 cronjob 运行脚本。我在这里看到了很多文档,Google 使用 cronjob 和 hostPath 到 Persistent Volumes/Claims 到使用 ConfigMaps,列表就是其中之一。

我不断收到“后退重启失败的容器/CrashLoopBackOff”错误。

任何帮助深表感谢。

cronjob.yaml

我尝试运行的脚本仅用于测试

#! /bin/<br/>
kubectl create deployment nginx --image=nginx

仍然得到同样的错误。

kubectl 描述 pod/xxxx

使用 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

标签: kuberneteskubernetes-cronjob

解决方案


假设您在远程多节点集群中运行它(因为您在问题中提到了 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当然不是开箱即用的容器中存在的命令之一。我希望这足以回答你的问题。


推荐阅读