kubernetes Kubernes Job Curl URL使用错误/非法格式或缺少URL

bpsygsoo  于 2022-12-29  发布在  Kubernetes
关注(0)|答案(2)|浏览(133)

我准备了一个任务yaml文件并部署了它。任务将发送post请求到grafana API创建用户方法。但是它返回错误。
错误:“ curl :(3)URL使用错误/非法格式或缺少URL”
我应该如何更改命令行?
yaml文件:

apiVersion: batch/v1
kind: Job
metadata:
  name: grafanauser-ttl
spec:
  ttlSecondsAfterFinished: 100
  template:
    spec:
      containers:
      - name: grafanauser
        image: curlimages/curl:7.72.0
        command:
            - '/bin/sh'
            - '-ec'          
            - 'curl -X POST "http://admin:admin@grafana.utility.svc.cluster.local/api/admin/users" \
                -H "Content-Type:application/json" -d \
                "{"name":"test","email":"test@localhost.com","login":"test","password":"test","OrgId": 1}"'
      restartPolicy: OnFailure
omtl5h9j

omtl5h9j1#

您的curl命令格式不正确,这就是您看到这个问题的原因。还有其他一些线程与您从curl收到的错误消息有关。
然而,一个快速的解决办法是在一条线上提供 curl 。以下是可行的:

---
apiVersion: batch/v1
kind: Job
metadata:
  name: grafanauser-ttl
spec:
  ttlSecondsAfterFinished: 100
  template:
    spec:
      containers:
        - name: grafanauser
          image: curlimages/curl:7.72.0
          command:
            - /bin/sh
            - -ec
            - 'curl -X POST "http://admin:admin@grafana.utility.svc.cluster.local/api/admin/users" -H "Content-Type:application/json" -d "{"name":"test","email":"test@localhost.com","login":"test","password":"test","OrgId": 1}"'
      restartPolicy: OnFailure

我没有运行该服务,但您应该不会再收到curl错误:

$ kubectl logs grafanauser-ttl-xjt2q
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: grafana.utility.svc.cluster.local
avwztpqn

avwztpqn2#

shell命令行将转换为

curl -X POST "http://admin:admin@grafana.utility.svc.cluster.local/api/admin/users"                 -H "Content-Type:application/json" -d                 "{"name":"test","email":"test@localhost.com","login":"test","password":"test","OrgId": 1}"

这会让shell去掉所有的引号:

curl -X POST http://admin:admin@grafana.utility.svc.cluster.local/api/admin/users -H Content-Type:application/json -d {name:test,email:test@localhost.com,login:test,password:test,OrgId: 1}

curl将阻塞在最后一个1}(它的最后一个参数)上
尝试使用YAML块文本重写它:

command:
            - '/bin/sh'
            - '-ec'          
            - |
                curl -X POST "http://admin:admin@grafana.utility.svc.cluster.local/api/admin/users" \
                -H "Content-Type:application/json" -d \
                '"{"name":"test","email":"test@localhost.com","login":"test","password":"test","OrgId": 1}'

相关问题