How to run command after initialization

You can use Helm to deploy a set of Kubernetes resources. And then, use a Helm hook, e.g. post-install or post-upgrade, to run a Job in a separate docker container. Set your Job to invoke db migration. A Job will run >=1 Pods to completion, so it fits here quite well.


I chose to use a readinessProbe
My application requires configuration after the process has completely started. The postStart command was running before the app was ready.

readinessProbe:
  exec:
    command: [healthcheck]
  initialDelaySeconds: 30
  periodSeconds: 2
  timeoutSeconds: 1
  successThreshold: 3
  failureThreshold: 10    

I resolved it using lifecycles:

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}}]

Tags:

Kubernetes