简体   繁体   English

Kubernetes cronJob 使用带有标头的 CURL 调用 REST API 因 curl(6) 失败:无法解析主机

[英]Kubernetes cronJob to call REST API using CURL with headers is failing with curl(6) : could not resolve host

How can I trigger the restAPI[POST with header having authentication and parameter] in cronJob using kubernetes.如何使用 kubernetes 在 cronJob 中触发 restAPI[带有身份验证和参数的标头的 POST]。 When I create the cron and try to run it I am getting the following error当我创建 cron 并尝试运行它时,出现以下错误

curl: (7) Failed to connect to localhost port 8080: Connection refused

When i replace the host with the actual rather than localhost it still gives me the error as当我用实际而不是 localhost 替换主机时,它仍然给我错误

 curl: (6) Could not resolve host : xxxHostName

I am able to hit the curl using the command prompt as well as in the POSTMAN我可以使用命令提示符以及在 POSTMAN 中点击 curl

Following is the cronJob which i am trying to run以下是我试图运行的 cronJob

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: e2em-rule-violation-job  
spec:
  schedule: "*/3 * * * *"
  concurrencyPolicy: Allow
  failedJobsHistoryLimit: 1
  successfulJobsHistoryLimit: 3
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: e2em-rule-violation-job 
            image: curlimages/curl:7.72.0
            args:
            - /bin/sh
            - -ec
            - "curl -H \"Authorization: Basic c3lzdGVtOm1hbmFnZQ==\" -H \"InternalUser: true\" -X POST  \"http://localhost:8080/myIntegration/rest/executeScheduledTask\""
          restartPolicy: OnFailure

Following is the dry run which is successful but Jobs fails when its created以下是试运行成功,但 Jobs 在创建时失败

W1006 13:35:34.443357   18304 helpers.go:535] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"batch/v1beta1","kind":"CronJob","metadata":{"annotations":{},"name":"e2em-rule-violation-job","namespace":"default"},"spec":{"concurrencyPolicy":"Allow","failedJobsHistoryLimit":1,"jobTemplate":{"spec":{"template":{"spec":{"containers":[{"args":["/bin/sh","-ec","curl -H \"Authorization: Basic c3lzdGVtOm1hbmFnZQ==\" -H \"InternalUser: true\" -X POST  \"http://localhost:8080/myIntegration/rest/executeScheduledTask\""],"image":"curlimages/curl:7.72.0","name":"e2em-rule-violation-job"}],"restartPolicy":"OnFailure"}}}},"schedule":"*/3 * * * *","successfulJobsHistoryLimit":3}}
  name: e2em-rule-violation-job
  namespace: default
spec:
  concurrencyPolicy: Allow
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - args:
            - /bin/sh
            - -ec
            - 'curl -H "Authorization: Basic c3lzdGVtOm1hbmFnZQ==" -H "InternalUser:
              true" -X POST  "http://localhost:8080/myIntegration/rest/executeScheduledTask"'
            image: curlimages/curl:7.72.0
            name: e2em-rule-violation-job
          restartPolicy: OnFailure
  schedule: '*/3 * * * *'
  successfulJobsHistoryLimit: 3
"curl -H \"Authorization: Basic c3lzdGVtOm1hbmFnZQ==\" -H \"InternalUser: true\" -X POST  \"http://localhost:8080/myIntegration/rest/executeScheduledTask\""

The problem in your case is in the above curl.您的情况的问题在于上述卷曲。 Look, you are trying to curl non-existent address.看,您正在尝试卷曲不存在的地址。 Whats happening once you run cronjob?运行 cronjob 后会发生什么?

  1. k8s creates cronjob e2em-rule-violation-job k8s 创建cronjob e2em-rule-violation-job
  2. cronjob e2em-rule-violation-job CREATES POD cronjob e2em-rule-violation-job POD
  3. curl command is trying to run exactly inside from this POD curl 命令正试图从这个 POD 内部准确运行

You POD doesnt know what the localhost:8080 is.您的 POD 不知道localhost:8080是什么。 You pod has absolutelly another localcost comparing to where your app is running...This address is not exposed, you pod cant have access to it.与您的应用程序运行的位置相比,您的 pod 绝对有另一个本地成本……此地址未公开,您的 pod 无法访问它。 Plus image: curlimages/curl:7.72.0 dockerfile doesnt have port 8080 exposed..image: curlimages/curl:7.72.0 dockerfile没有暴露端口 8080..

What you can do for test is to你可以做的测试是

  1. create normal deployment with the curl pod, lets say NEWCURLPOD使用 curl pod 创建正常部署,比如NEWCURLPOD
  2. connect to NEWCURLPOD and execute your initial command you wanted to run.连接到NEWCURLPOD并执行您想要运行的初始命令。 Example: kubectl exec -ti {NEWCURLPOD} -n {PROPER_NAMESPACE} -- curl blablabla示例: kubectl exec -ti {NEWCURLPOD} -n {PROPER_NAMESPACE} -- curl blablabla

If that will not work - check dns.如果这不起作用 - 检查 dns。 If your app is running inside the same cluster - you should properly expose its service to reach the target, but for sure this is not localhost:8080如果您的应用程序在同一个集群中运行 - 您应该正确公开其服务以到达目标,但这肯定不是localhost:8080

Dry run..试运行..

Following is the dry run which is successful but Jobs fails when its created以下是试运行成功,但 Jobs 在创建时失败

While running dry-run - it only check and preview the object, it should not execute curl there - as a result you see it completes successfully.在运行试运行时 - 它只检查和预览对象,它不应该在那里执行 curl - 结果你会看到它成功完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM