<p>There's a similar question <a href="https://stackoverflow.com/questions/37559704/kubectl-yaml-config-file-equivalent-of-kubectl-run-i-tty">here</a>, but I think I want different thing.
For those who familiar with docker-compose, there's a brilliant command which runs command in container just once, this insanely helps for launching migrations before each deploy:</p>

<pre><code>docker-compose -f docker-compose.prod.yml run web npm run migrate
</code></pre>

<p>Also because this is a one line command it's comfortable for automation purpose: like using Makefile or ansible/chef/saltstack. </p>

<p>The only thing I've found is <code>kubectl run</code> which is more similar to <code>docker run</code>. But <code>docker-compose run</code> allows us to use config file, where docker-run does not:</p>

<pre><code> kubectl run rp2migrate --command -- npm run migrate
</code></pre>

<p>This would probably work, but I need to list 20 environment variables, and really don't want to do this in command line.. instead I'd like to pass a flag which would specify yaml config like this:</p>

<pre><code> kubectl run rp2migrate  -f k8s/rp2/rp2-deployment.yaml --command -- npm run migrate
</code></pre>

<p>Edit:</p>

<p>Kubernetes also got <code>init containers</code> as a beta feature (as of now) - <a href="http://kubernetes.io/docs/user-guide/production-pods/#handling-initialization" rel="nofollow">http://kubernetes.io/docs/user-guide/production-pods/#handling-initialization</a></p>

<hr>

<p>You should probably leverage Kubernetes PostStart hook. Something like below:</p>

<pre><code>lifecycle:
  postStart:
    exec:
      command:
      - "npm"
      - "run"
      - "migrate"
</code></pre>

<p><a href="http://kubernetes.io/docs/user-guide/container-environment/" rel="nofollow">http://kubernetes.io/docs/user-guide/container-environment/</a></p>

<p>The environment variables specified for your pod will be available too:</p>

<blockquote>
  <p>Additionally, user-defined environment variables from the pod definition, are also available to the container, as are any environment variables specified statically in the Docker image</p>
</blockquote>
