【问题标题】:what is the issue with this cron job, its not running. how to debug it这个 cron 作业有什么问题,它没有运行。如何调试它
【发布时间】:2019-12-03 23:03:12
【问题描述】:

我正在使用 helm 进行 k8s 部署,我需要一个仅访问 url 的 cron 作业。如果我将它作为 shell 脚本任务独立运行,我已经编写了脚本并且脚本可以工作。为什么cron作业不能在里面运行脚本。

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: sampletemplaterelease-myapi
  labels:
    app.kubernetes.io/name: myapi
    helm.sh/chart: myapi-0.1.0
    app.kubernetes.io/instance: sampletemplaterelease
    app.kubernetes.io/version: "1.0"
    app.kubernetes.io/managed-by: Tiller
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/bash
            - -c
            - |
              accessTokenBody=$(curl -X POST -d "client_id=sample&grant_type=sample&username=sample&password=sample&override=true" https://sample.com/sample/sample)
              accessToken=$(jq -r  '.access_token' <<< "${accessTokenBody}" )
              echo $accessToken
              sfSyncTriggerResult=$(curl -X POST -H "Content-Length: 0" -H "Authorization: Bearer $accessToken" https://sample.com/sample/sample)
              echo $sfSyncTriggerResult
              echo "${sfSyncTriggerResult}" | jq '.'
              errorCount=$(echo $sfSyncTriggerResult | jq '. | length')
              echo "Total Number Of Errors"
              echo $errorCount
              if [ "$errorCount" -gt 0 ]
                  then
                      echo "not working, exiting"
                      exit 1
                      break
              else
                      echo "Sync triggered successfully"
                  fi
          restartPolicy: OnFailure

kubectl 记录 podname:

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1183    0  1053  100   130   1193    147 --:--:-- --:--:-- --:--:--  1339
/bin/bash: line 1: jq: command not found



  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

/bin/bash: line 7: jq: command not found
/bin/bash: line 8: jq: command not found
Total Number Of Errors

Sync triggered successfully
/bin/bash: line 11: [: : integer expression expected

【问题讨论】:

  • 请添加对您的 pod 的描述。
  • 也提供容器的日志
  • 您需要提供更多详细信息,例如 pod 发生了什么,是否已安排好?豆荚是否因错误而终止? kubectl describe po [pod_name] 连同容器日志应该会告诉你出了什么问题。还要检查 cronjob 对象的状态,那里的状态是什么?。
  • 我已经更新了我的问题的输入。它给 jq: command not found
  • @VatanSoni 因为您的图像中没有 jq 二进制文件。你的船长很明显。

标签: kubernetes cron google-kubernetes-engine kubernetes-helm kubernetes-cronjob


【解决方案1】:

您可以使用任何具有jq 的图像或在容器内安装jq 来实现。因此,我尝试的一种方法是使用alpine 作为容器映像而不是busybox,然后在其中安装jq。请参阅以下内容:

spec:
  template:
    spec:
      containers:
      - name: hello
        image: alpine
        args:
        - sh
        - -c
        - |
          apk add --no-cache jq
          <do_what_you_need>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2012-08-18
    • 2014-01-07
    • 1970-01-01
    • 2014-03-17
    相关资源
    最近更新 更多