使用 wget 命令的 cronjob yml 文件

cronjob yml file with wget command

嗨,我是 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 没有任何反应,我没有收到任何响应。 我 运行 的命令是:

在return中我只得到带日期的命令(上面的img)

当我只使用 wget 命令时,没有任何反应。在 pods 中,我可以在 STATUS CrashLoopBackOff 中看到。所以命令 运行.

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

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

kubernetes中的command是dockerequivalent to entrypoint in docker. For any container, there should be 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 (, , etc.)  set from the re‐
                        maining argument operands.