如何在Kubernetes中使用CronJobs运行Shell脚本?

我正在尝试使用CronJob每隔1分钟运行一次Shell脚本。

我在openshift模板中创建了以下Cron作业:

- kind: CronJob
  apiVersion: batch/v2alpha1
  metadata:
    name: "${APPLICATION_NAME}"
  spec:
    schedule: "*/1 * * * *"
    jobTemplate:
      spec:
        template:
          spec:
            containers:
            - name: mycron-container
              image: alpine:3
              imagePullPolicy: IfNotPresent

              command: [ "/bin/sh" ]
              args: [ "/var/httpd-init/croyscript.sh" ]
              volumeMounts:
              - name: script
                mountPath: "/var/httpd-init/"
            volumes:
            - name: script
              configMap:
                name: ${APPLICATION_NAME}-croyscript
            restartPolicy: OnFailure
            terminationGracePeriodSeconds: 0

    concurrencyPolicy: Replace

以下是此作业中作为卷插入的configmap:

- kind: ConfigMap
  apiVersion: v1
  metadata:
    name: ${APPLICATION_NAME}-croyscript
    labels:
      app: "${APPLICATION_NAME}"
  data:
    croyscript.sh: |
      #!/bin/sh
      if [ "${APPLICATION_PATH}" != "" ]; then
          mkdir -p /var/httpd-resources/${APPLICATION_PATH}
      fi
      mkdir temp
      cd temp 
      ###### SOME CODE ######

此Cron作业正在运行。如我所见,工作名称每1分钟被替换一次(按照我的工作安排)。但是它没有执行shell脚本croyscript.sh 我在这里做错什么吗? (也许我以错误的方式插入了configmap,所以Job无法访问shell脚本)

nihao_123456 回答:如何在Kubernetes中使用CronJobs运行Shell脚本?

尝试以下方法

更新configmap位置的权限

            volumes:
            - name: script
              configMap:
                name: ${APPLICATION_NAME}-croyscript
                defaultMode: 0777

如果该命令不起作用,则很有可能已装入的卷中的脚本已具有READONLY权限。 使用initContainer将脚本复制到其他位置并设置适当的权限,然后在命令参数中使用该位置

本文链接:https://www.f2er.com/3159985.html

大家都在问