How can I start a job automatically after a successful deployment in kubernetes?

1/23/2020

I have a deployment .yaml file that basically create a pod with mariadb, as follows

    apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}-pod
spec:
  replicas: 1
  selector:
    matchLabels:
      pod: {{ .Release.Name }}-pod
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        pod: {{ .Release.Name }}-pod
    spec:
      containers:
      - env:
        - name: MYSQL_ROOT_PASSWORD
          value: {{ .Values.db.password }}
        image: {{ .Values.image.repository }}
        name: {{ .Release.Name }}
        ports:
        - containerPort: 3306
        resources: 
          requests:
            memory: 2048Mi
            cpu: 0.5
          limits:
            memory: 4096Mi
            cpu: 1
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: dbsvr-claim
        - mountPath: /etc/mysql/conf.d/my.cnf
          name: conf
          subPath: my.cnf
        - mountPath: /docker-entrypoint-initdb.d/init.sql
          name: conf
          subPath: init.sql

      restartPolicy: Always

      volumes:
      - name: dbsvr-claim
        persistentVolumeClaim:
          claimName: {{ .Release.Name }}-claim
      - name: conf
        configMap:
          name: {{ .Release.Name }}-configmap
status: {}

Upon success on

helm install abc ./abc/ -f values.yaml

I have a job that generates a mysqldump backup file and it completes successfully (just showing the relevant code)

apiVersion: batch/v1
kind: Job
metadata: 
    name: {{ .Release.Name }}-job
spec:
  template:
    metadata:
      name: {{ .Release.Name }}-job
    spec:
        containers:
        - name: {{ .Release.Name }}-dbload
          image: {{ .Values.image.repositoryRoot }}/{{.Values.image.imageName}}
          command: ["/bin/sh", "-c"]
          args: 
            - mysqldump -p$(PWD) -h{{.Values.db.source}} -u$(USER) --databases xyz > $(FILE);
              echo "done!";
          imagePullPolicy: Always
        # Do not restart containers after they exit
        restartPolicy: Never    

So, here's my question. Is there a way to automatically start the job after the helm install abc ./ -f values.yaml finishes with success?

-- Ricardo Adão
automation
deployment
kubernetes

1 Answer

1/23/2020

you can use kubectl wait -h command to execute job when the condition=Ready for the deployment.

Here the article wait-for-condition demonstrate quite similar situation

-- Suresh Vishnoi
Source: StackOverflow