mysqlkuberneteshookkubernetes-helmkubernetes-jobs

helm hook for both Pod and Job for kubernetes not running all yamls


I am using Kubernetes with Helm 3.

It is ran on CentOS Linux 7 (Core).

K8S (check by running: kubectl version):

git version (kubernetes): v1.21.6, go version: go1.16.9.

helm version: v3.3.4

helm version (git) go1.14.9.

I need to create a Job that is running after a Pod is created.

The pod yaml:

apiVersion: v1
kind: Pod
metadata:
  name: {{ include "test.fullname" . }}-mysql
  labels:
    app: {{ include "test.fullname" . }}-mysql
  annotations:
    "helm.sh/hook": post-install
    "helm.sh/hook-weight": "-20"
    "helm.sh/delete-policy": before-hook-creation
spec:
  containers:
    - name: {{ include "test.fullname" . }}-mysql
      image: {{ .Values.mysql.image }}
      imagePullPolicy: IfNotPresent
      env:
        - name: MYSQL_ROOT_PASSWORD
          value: "12345"
        - name: MYSQL_DATABASE
          value: test

The Job:

apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "test.fullname" . }}-migration-job
  labels:
    app: {{ include "test.fullname" . }}-migration-job
  annotations:
    "helm.sh/hook": post-install
    "helm.sh/hook-weight": "-10"
    "helm.sh/hook-delete-policy": hook-succeeded, hook-failed
spec:
  parallelism: 1
  completions: 1
  backoffLimit: 1
  template: #PodTemplateSpec (Core/V1)
    spec: #PodSpec (core/v1)
    initContainers: # regular
    - name: wait-mysql
      image: bitnami/kubectl
      imagePullPolicy: IfNotPresent
      args:
        - wait
        - pod/{{ include "test.fullname" . }}-mysql
        - --namespace={{ .Release.Namespace }}
        - --for=condition=ready
        - --timeout=120s
    containers:
      - name: {{ include "test.fullname" . }}
        image: {{ .Values.myMigration.image }}
        imagePullPolicy: IfNotPresent
        command: {{- toYaml .Values.image.entrypoint | nindent 12 }}
        args: {{- toYaml .Values.image.cmd | nindent 12}}

MySQL is MySQL 5.6 image.

When I write the above, also run helm install test ./test --namespace test --create-namespace

Even though I changed the hook for pre-install (for Pod and Job), the job is never running.

In both situations, I get messages (and need to press - to exit - I don't want this behavior either:

Pod test-mysql pending Pod test-mysql pending Pod

test-mysql pending Pod test-mysql running Pod

test-mysql running Pod test-mysql running Pod

test-mysql running ...

In this example, when I put a 'bug' in the Job, for example: containersx instead of container, I don't get any notification that I have a wrong syntax.

Maybe because MySQL is running (and not completed), can I force to go to the next yaml declared by hook? (Even I declare the proper order for Pod and Job. Pod should run before Job).

What is wrong, and how can I ensure the pod is created before the job? And when the pod starts running, my job will run after that?

Thanks.


Solution

  • As per your configuration, it looks like you need to set post-install hook precisely for Job as it should execute after all resources are loaded into Kubernetes. On executing pre-install hook both on Pod and Job, it is run before the rest of the chart is loaded, which seems to prevent Job from starting.