我尝试让 postStart 钩子在容器中工作,但总是失败。我收到的错误如下:
kubelet[1057]: E0212 11:07:20.205922 1057 handlers.go:78] "Exec lifecycle hook for Container in Pod failed" err=<
kubelet[1057]: command 'curl -H 'Content-Type: application/json' -d '{ \"restarted\": True}' -X POST http://localhost:5000/restarted' exited with 2: curl: (2) no URL specified
kubelet[1057]: curl: try 'curl --help' or 'curl --manual' for more information
kubelet[1057]: > execCommand=[curl -H 'Content-Type: application/json' -d '{ \"restarted\": True}' -X POST http://localhost:5000/restarted] containerName="srsran-cu-du" pod="srsran/srsran-project-cudu-chart-78f658b865-pjvt2" message=<
kubelet[1057]: curl: (2) no URL specified
kubelet[1057]: curl: try 'curl --help' or 'curl --manual' for more information
kubelet[1057]: >
我的清单中的钩子如下所示:
lifecycle:
postStart:
exec:
command: [ "curl", "-H", "'Content-Type: application/json'", "-d", "'{ \"restarted\": True}'", "-X", "POST http://localhost:5000/restarted" ]
其呈现为curl -H 'Content-Type: application/json' -d '{ \"restarted\": True}' -X POST http://localhost:5000/restarted
。
如果我直接在容器中运行 curl 命令,它会正常工作。但是当通过 posStart 钩子运行它时,它不起作用。我做错了什么?
我尝试过用 替换'
但\\\"
也没有用。
当您使用数组形式
command:
或将命令作为容器的 传递时args:
,您需要为每个 YAML 列表项传递一个 shell 单词。导致错误的最直接原因是最后一个列表项中有两个单词,因此curl
将其解释为请求 HTTP 方法的单个参数POST http://...
(包括空格),然后 URL 中没有后续参数。您还可能会从标题中收到错误
Content-Type:
:因为在双引号 YAML 字符串内有一组单引号,所以curl
会将这些引号视为参数的一部分-H
,并且它可能会发送无效的 HTTP 标题或拒绝标题语法本身。将其拆分为每个单词一个参数,使用 YAML 块序列语法,并且仅在需要时使用 YAML 引用,我可能会写:
或者你可以使用相同的引用规则将其重新打包成内联列表(“流序列”)
再次注意,标题只有一组引号
Content-Type:
,并且POST
和http://...
是单独的列表项。如果您愿意,也可以引用其他单词,但只有可能与其他 YAML 语法混淆的几个词才需要引用。