<p>I can log into console from one of the pods (on kubernetes) and run this command:</p>

<pre><code>RAILS_ENV=production bin/delayed_job start
</code></pre>

<p>The jobs are run correctly doing that. However when the pods are deleted or restarted, the jobs stop running. </p>

<p>I also tried adding the command above in an initializer file (eg config/initializers/delayed_jobs_runner.rb), but I get a recursive loop when starting the app. </p>

<p>Another thing I tried to do is create a new file called <code>my-jobs.yaml</code> with this</p>

<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: job
spec:
  template:
    spec:
      containers:
      - name: job
        image: gcr.io/test-app-123/somename:latest
        command: ["/bin/bash", "-l", "-c"]
        args: ["RAILS_ENV=production bundle exec rake jobs:work"]
      restartPolicy: Never
  backoffLimit: 4
</code></pre>

<p>I then do <code>kubectl apply -f my-jobs.yaml</code>, but the jobs aren't running.</p>

<p>Any idea how to run delayed_jobs correctly in kubernetes?</p>

<p>EDIT: Here's my Dockerfile:</p>

<pre><code>FROM gcr.io/google_appengine/ruby

# Install 2.5.1 if not already preinstalled by the base image
RUN cd /rbenv/plugins/ruby-build &amp;&amp; \
    git pull &amp;&amp; \
    rbenv install -s 2.5.1 &amp;&amp; \
    rbenv global 2.5.1 &amp;&amp; \
    gem install -q --no-rdoc --no-ri bundler 
    # --version 1.11.2

ENV RBENV_VERSION 2.5.1

# Copy the application files.
COPY . /app/

# Install required gems.
RUN bundle install --deployment &amp;&amp; rbenv rehash

# Set environment variables.
ENV RACK_ENV=production \
    RAILS_ENV=production \
    RAILS_SERVE_STATIC_FILES=true

# Run asset pipeline.
RUN bundle exec rake assets:precompile


CMD ["setup.sh"]


# Reset entrypoint to override base image.
ENTRYPOINT ["/bin/bash"]




################### setup.sh ############################
cd /app &amp;&amp; RAILS_ENV=production bundle exec script/delayed_job -n 2 start
bundle exec foreman start --formation "$FORMATION"
#########################################################
</code></pre>

sjsc
<p>Running multiple processes in one docker container is problematic as you cannot easily observe lifetime of particular process - every container need one process which is "main" and when it exit, container also exit.</p>

<p>As looking on Github (<a href="https://github.com/collectiveidea/delayed_job#user-content-running-jobs" rel="nofollow noreferrer">https://github.com/collectiveidea/delayed_job#user-content-running-jobs</a>) I would strongly suggest to change a little your starting command to run it in foreground because now when you are starting Kubernetes job with daemons 
 - job is ending immediately as docker container lifetime is directly related to "main" foreground process lifetime so when you run only background process your main process exit immediately and your container too.</p>

<p>Change your command to:</p>

<pre><code>RAILS_ENV=production script/delayed_job run
</code></pre>

<p>What start worker in foreground so your Kubernetes Job won't exit. Please note also that Kubernetes Jobs are not intended to such infinitive tasks (job should has start and end) so I would suggest to use <a href="https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/" rel="nofollow noreferrer">ReplicaSet</a> for that</p>
