<p>We are deploying a Rails application on Kubernetes.</p>
<p>The <code>assets:precompile</code> task run as part of the Docker image build process.</p>
<p>We want to run rake tasks like rake db:migrate task and other tasks on each deployment.</p>
<p>Current solution, we are using kubectl exec.</p>
<pre><code>deplyment.yaml

apiVersion: extensions/v1
kind: Deployment
metadata:
  name: rails-app
spec:
  template:
    spec:
      containers:
        - image: test/rails-app:v0.0.1
          name: myapp
          imagePullPolicy: IfNotPresent
</code></pre>
<p>Get list of pods</p>
<pre><code>$ kubectl get pods
</code></pre>
<p>Then exec on the rails pod:</p>
<pre><code>$ kubectl exec rails-app-4007005961-1st7s                              \
          -- bash -c                                               \
          'bin/rake db:migrate
</code></pre>
<p>'</p>
<p>We need to use kubernentes cronjob (.yaml) to start our rake taks but we do not know which docker image we must use ? how to connect to rails pod and start the rake task ?.
example of implementation ?</p>

karlos
<blockquote>
<p>We need to use kubernentes cronjob (.yaml) to start our rake taks</p>
</blockquote>
<p>I don't think you need a k8s <strong>cronjob</strong> to run your db migrations. You might wanna use the <em>job</em> resource in k8s, and run it as part of your CI/CD script. Run it right before you apply your deployment.yaml file. In case the migrations job fails, you should abort the deployment.</p>
