【问题标题】:cronjob yml file with wget command使用 wget 命令的 cronjob yml 文件
【发布时间】:2021-08-24 13:58:19
【问题描述】:

您好,我是 Kubernetes 新手。我正在尝试在 cronjob.yml 文件中运行 wget 命令以每天从 url 获取数据。现在我正在测试它并通过时间表为 1 分钟。我还添加了一些 echo 命令,只是为了从该工作中获得一些响应。下面是我的 yml 文件。我正在将目录更改为要保存数据的文件夹,并将 url 与我从中获取数据的站点一起传递。我在终端中使用wget url 尝试了 url,它可以工作并下载隐藏在 url 中的 json 文件。

apiVersion: batch/v1
kind: CronJob
metadata:
  name: reference
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: reference
            image: busybox
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
            - cd /mnt/c/Users/path_to_folder
            - wget {url}
          restartPolicy: OnFailure

当我创建作业并查看 pod 日志时,url 没有任何反应,我没有得到任何响应。 我运行的命令是:

  • kubectl create -f cronjob.yml
  • kubectl get pods

  • kubectl logs <pod_name>

作为回报,我只得到带有日期的命令(上面的 img)

当我只用 wget 命令离开时,什么也没有发生。在 pod 中,我可以在 STATUS CrashLoopBackOff 中看到。所以命令运行有问题。

command:
                - cd /mnt/c/Users/path_to_folder
                - wget {url}

cronjob.yml 中的 wget 命令应该是什么样子的?

【问题讨论】:

    标签: kubernetes cron yaml wget busybox


    【解决方案1】:

    kubernetes 中的command 是docker 中的equivalententrypoint。对于任何容器,都应该有only one 进程作为入口点。图像中的默认入口点或通过command 提供。

    在这里,您将 /bin/sh 用作单个进程,并将其他所有内容用作参数。您执行 /bin/sh -c 的方式,这意味着提供 date; echo Hello from the Kubernetes cluster 作为输入命令。不是 cdwget 命令。将清单更改为以下内容,以将所有内容作为一个块提供给/bin/sh。请注意,所有命令都适合作为 1 个参数。

    apiVersion: batch/v1
    kind: CronJob
    metadata:
      name: reference
    spec:
      schedule: "*/1 * * * *"
      jobTemplate:
        spec:
          template:
            spec:
              containers:
              - name: reference
                image: busybox
                imagePullPolicy: IfNotPresent
                command:
                - /bin/sh
                - -c
                - date; echo Hello from the Kubernetes cluster; cd /mnt/c/Users/path_to_folder;wget {url}
              restartPolicy: OnFailure
    

    为了说明问题,请检查以下示例。请注意,仅执行第一个参数。

    /bin/sh -c date
    Tue 24 Aug 2021 12:28:30 PM CDT
    /bin/sh -c echo hi
    
    /bin/sh -c 'echo hi'
    hi
    /bin/sh -c 'echo hi && date'
    hi
    Tue 24 Aug 2021 12:28:45 PM CDT
    /bin/sh -c 'echo hi' date #<-----your case is similar to this, no date printed.
    hi
    
           -c               Read commands from the command_string operand instead of from the standard input.  Special parameter 0
                            will be set from the command_name operand and the positional parameters ($1, $2, etc.)  set from the re‐
                            maining argument operands.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-22
      • 2016-11-26
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      • 2021-02-15
      • 2020-02-13
      • 2016-12-16
      相关资源
      最近更新 更多