<p>I would like to run specific command after initialization of deployment is successful.</p>

<p>This is my yaml file:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: auth
spec:
  replicas: 1
  template:
    metadata:
        labels:
          app: auth
    spec:
      containers:
        - name: auth
          image: {{my-service-image}}
          env:
          - name: NODE_ENV
            value: "docker-dev"
          resources:
            requests:
              cpu: 100m
              memory: 100Mi
          ports:
            - containerPort: 3000
</code></pre>

<p>However, I would like to run command for db migration after (not before) deployment is successfully initialized and pods are running. </p>

<p>I can do it manually for every pod (with kubectl exec), but this is not very scalable.</p>

meerkat
<p>I resolved it using <a href="https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/" rel="noreferrer">lifecycles</a>:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: auth
spec:
  replicas: 1
  template:
    metadata:
        labels:
          app: auth
    spec:
      containers:
        - name: auth
          image: {{my-service-image}}
          env:
          - name: NODE_ENV
            value: "docker-dev"
          resources:
            requests:
              cpu: 100m
              memory: 100Mi
          ports:
            - containerPort: 3000
          lifecycle:
            postStart:
              exec:
                command: ["/bin/sh", "-c", {{cmd}}]
</code></pre>
