<p>I am using rabbitmq at a remote (cloudamqp.com) and I create a cron job on Kubernetes. On my local machine, my job is working fine and the Kubernetes cronJob schedules perfectly well but the Job redirects the rabbitmq connection URL to <code>127.0.0.1:5672</code>  and I get an error.</p>

<pre><code>pika.exceptions.ConnectionClosed: Connection to 127.0.0.1:5672 failed: [Errno 111] Connection refused
</code></pre>

<p>I check logs of cron job and my connection URL is perfectly fine but when pika is trying to connect to the host it automatically redirects to <code>127.0.0.1:5672</code> as we know the cron pod is not running any rabbitmq server so it refuses the connection.</p>

<p>CronJob.yml</p>

<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: scrape-news
spec:
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: scrape-news
        spec:
          containers:
          - name: scrape-news
            image: SCRAPER_IMAGE
            imagePullPolicy: Always
          restartPolicy: Never
  schedule: '* * * * *'
  successfulJobsHistoryLimit: 3
</code></pre>

<p>RabbitMQ Connection </p>

<pre><code>    print(env.RABBIT_URL)
    self.params = pika.URLParameters(env.RABBIT_URL)
    self.connection = pika.BlockingConnection(parameters=self.params)
    self.channel = self.connection.channel() # start a channel
</code></pre>

<p>Connection URL is exact same and works on my local setup.</p>

Yuvraj
<p>Based on your <code>CronJob</code> spec you are not passing the environment variable <code>RABBIT_URL</code>.</p>

<p>Your code looks as if it is expecting this variable to be set, which it is not, and which is likely why it is defaulting to <code>localhost</code>.</p>

<pre><code>self.params = pika.URLParameters(env.RABBIT_URL)
</code></pre>

<p>You probably want something like this:</p>

<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: scrape-news
spec:
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: scrape-news
        spec:
          containers:
          - name: scrape-news
            image: SCRAPER_IMAGE
            imagePullPolicy: Always
            env:
              - name: RABBIT_URL
                value: cloudamqp.com
          restartPolicy: Never
  schedule: '* * * * *'
  successfulJobsHistoryLimit: 3
</code></pre>
