kubernetes在busybox容器中通过curl运行HTTP POST 报error converting YAML to JSON: yaml

狼行天下 发表于: 2019-10-22   最后更新时间: 2019-10-22 22:28:41   7,675 游览

我在k8s中创建一个简单的cronjob,该cronjob只是在busybox容器中使用curl来执行HTTP Post。但是报格式是错误的,但我不知道为什么。

我尝试以及以各种方式更改curl命令的格式,但均未成功。

apiVersion: batch/v1beta1
kind: CronJob
#namespace: test
metadata:
  name: test-cron
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: test-cron
            image: busybox
            args:
            - /bin/sh
            - -c
            - curl "https://test-es01.test.svc.clu01.test.local:9200/logs_write/_rollover" -H 'Content-Type: application/json' -d '{"conditions: {"max_size": "5gb"}}'
          restartPolicy: OnFailure

运行它:

kubectl -n test apply -f test-cron.yaml

错误:

error: error parsing test-cron.yaml: error converting YAML to JSON: yaml: line 20: mapping values are not allowed in this context

有人知道这个格式错误吗?

谢谢

发表于 2019-10-22

是因为curl命令包含分号,所以yaml认为你正在尝试定义的是对象

 - curl "https://test-es01.test.svc.clu01.test.local:9200/logs_write/_rollover" -H 'Content-Type: application/json' -d '{"conditions: {"max_size": "5gb"}}'

应该是

- "curl \"https://test-es01.test.svc.clu01.test.local:9200/logs_write/_rollover\" -H 'Content-Type: application/json' -d '{\"conditions: {\"max_size\": \"5gb\"}}'\n"

验证此类错误的小工具网站 http://www.yamllint.com/

狼行天下 -> 半兽人 4年前

谢谢。 现在好了!

你好,你是怎么解决的呢。我也遇到这个问题。

你的答案

查看kubernetes相关的其他问题或提一个您自己的问题