<p>How can I trigger the restAPI[POST with header having authentication and parameter] in cronJob using kubernetes. When I create the cron and try to run it I am getting the following error</p>
<pre><code>curl: (7) Failed to connect to localhost port 8080: Connection refused
</code></pre>
<p>When i replace the host with the actual rather than localhost it still gives me the error as</p>
<pre><code> curl: (6) Could not resolve host : xxxHostName
</code></pre>
<p>I am able to hit the curl using the command prompt as well as in the POSTMAN</p>
<p>Following is the cronJob which i am trying to run</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: e2em-rule-violation-job  
spec:
  schedule: &quot;*/3 * * * *&quot;
  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
            - &quot;curl -H \&quot;Authorization: Basic c3lzdGVtOm1hbmFnZQ==\&quot; -H \&quot;InternalUser: true\&quot; -X POST  \&quot;http://localhost:8080/myIntegration/rest/executeScheduledTask\&quot;&quot;
          restartPolicy: OnFailure
</code></pre>
<p>Following is the dry run which is successful but Jobs fails when its created</p>
<pre><code>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: |
      {&quot;apiVersion&quot;:&quot;batch/v1beta1&quot;,&quot;kind&quot;:&quot;CronJob&quot;,&quot;metadata&quot;:{&quot;annotations&quot;:{},&quot;name&quot;:&quot;e2em-rule-violation-job&quot;,&quot;namespace&quot;:&quot;default&quot;},&quot;spec&quot;:{&quot;concurrencyPolicy&quot;:&quot;Allow&quot;,&quot;failedJobsHistoryLimit&quot;:1,&quot;jobTemplate&quot;:{&quot;spec&quot;:{&quot;template&quot;:{&quot;spec&quot;:{&quot;containers&quot;:[{&quot;args&quot;:[&quot;/bin/sh&quot;,&quot;-ec&quot;,&quot;curl -H \&quot;Authorization: Basic c3lzdGVtOm1hbmFnZQ==\&quot; -H \&quot;InternalUser: true\&quot; -X POST  \&quot;http://localhost:8080/myIntegration/rest/executeScheduledTask\&quot;&quot;],&quot;image&quot;:&quot;curlimages/curl:7.72.0&quot;,&quot;name&quot;:&quot;e2em-rule-violation-job&quot;}],&quot;restartPolicy&quot;:&quot;OnFailure&quot;}}}},&quot;schedule&quot;:&quot;*/3 * * * *&quot;,&quot;successfulJobsHistoryLimit&quot;:3}}
  name: e2em-rule-violation-job
  namespace: default
spec:
  concurrencyPolicy: Allow
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - args:
            - /bin/sh
            - -ec
            - 'curl -H &quot;Authorization: Basic c3lzdGVtOm1hbmFnZQ==&quot; -H &quot;InternalUser:
              true&quot; -X POST  &quot;http://localhost:8080/myIntegration/rest/executeScheduledTask&quot;'
            image: curlimages/curl:7.72.0
            name: e2em-rule-violation-job
          restartPolicy: OnFailure
  schedule: '*/3 * * * *'
  successfulJobsHistoryLimit: 3
</code></pre>

<pre><code>&quot;curl -H \&quot;Authorization: Basic c3lzdGVtOm1hbmFnZQ==\&quot; -H \&quot;InternalUser: true\&quot; -X POST  \&quot;http://localhost:8080/myIntegration/rest/executeScheduledTask\&quot;&quot;
</code></pre>
<p>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?</p>
<ol>
<li>k8s creates <code>cronjob</code> <code>e2em-rule-violation-job</code></li>
<li><code>cronjob</code> <code>e2em-rule-violation-job</code> CREATES <code>POD</code></li>
<li>curl command is trying to run exactly inside from this POD</li>
</ol>
<p>You POD doesnt know what the <code>localhost:8080</code> is. 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. Plus <code>image: curlimages/curl:7.72.0</code> <a href="https://github.com/curl/curl-docker/blob/master/alpine/latest/Dockerfile" rel="nofollow noreferrer">dockerfile</a> doesnt have port 8080 exposed..</p>
<p>What you can do for test is to</p>
<ol>
<li>create normal deployment with the curl pod, lets say <code>NEWCURLPOD</code></li>
<li>connect to <code>NEWCURLPOD</code> and execute your initial command you wanted to run. Example: <code>kubectl exec -ti {NEWCURLPOD} -n {PROPER_NAMESPACE} -- curl blablabla</code></li>
</ol>
<p>If that will not work - check 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 <code>localhost:8080</code></p>
<p>Dry run..</p>
<blockquote>
<p>Following is the dry run which is successful but Jobs fails when its
created</p>
</blockquote>
<p>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.</p>
