How to run command after initialization

Viewed 56742

I would like to run specific command after initialization of deployment is successful.

This is my yaml file:

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

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

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

3 Answers

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    
Related