<p>I am using K8S with helm.</p>
<p>I need to run pods and dependencies with a predefined flow order.</p>
<p>How can I create helm dependencies that run the pod only once (i.e - populate database for the first time), and exits after first success?</p>
<p>Also, if I have several pods, and I want to run the pod only on certain conditions occurs and after creating a pod.</p>
<p>Need to build 2 pods, as is described as following:</p>
<p>I have a database.</p>
<p><strong>1st step</strong> is to create the database.</p>
<p><strong>2nd step</strong> is to populate the db.</p>
<p>Once I populate the db, this job need to finish.</p>
<p><strong>3rd step</strong> is another pod (not the db pod) that uses that database, and always in listen mode (never stops).</p>
<p>Can I define in which order the dependencies are running (and not always parallel).</p>
<p>What I see for <code>helm create</code> command that there are templates for deployment.yaml and service.yaml, and maybe pod.yaml is better choice?</p>
<p>What are the best charts types for this scenario?</p>
<p>Also, need the to know what is the chart hierarchy.</p>
<p>i.e: when having a chart of type: listener, and one pod for database creation, and one pod for the database population (that is deleted when finished), I may have a chart tree hierarchy that explain the flow.</p>
<p><a href="https://i.stack.imgur.com/FpBys.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FpBys.png" alt="enter image description here" /></a></p>
<p>The main chart use the populated data (after all the sub-charts and templates are run properly - BTW, can I have several templates for same chart?).</p>
<p>What is the correct tree flow</p>
<p>Thanks.</p>

<p>You can achieve this using helm hooks and K8s Jobs, below is defining the same setup for Rails applications.</p>
<p>The first step, define a k8s job to create and populate the db</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: {{ template &quot;my-chart.name&quot; . }}-db-prepare
  annotations:
    &quot;helm.sh/hook&quot;: pre-install,pre-upgrade
    &quot;helm.sh/hook-weight&quot;: &quot;-1&quot;
    &quot;helm.sh/hook-delete-policy&quot;: hook-succeeded
  labels:
    app: {{ template &quot;my-chart.name&quot; . }}
    chart: {{ template &quot;my-chart.chart&quot; . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  backoffLimit: 4
  template:
    metadata:
      labels:
        app: {{ template &quot;my-chart.name&quot; . }}
        release: {{ .Release.Name }}
    spec:
      containers:
      - name: {{ template &quot;my-chart.name&quot; . }}-db-prepare
        image: &quot;{{ .Values.image.repository }}:{{ .Values.image.tag }}&quot;
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        command: [&quot;/docker-entrypoint.sh&quot;]
        args: [&quot;rake&quot;, &quot;db:extensions&quot;, &quot;db:migrate&quot;, &quot;db:seed&quot;]
        envFrom:
        - configMapRef:
            name: {{ template &quot;my-chart.name&quot; . }}-configmap
        - secretRef:
            name: {{ if .Values.existingSecret }}{{ .Values.existingSecret }}{{- else }}{{ template &quot;my-chart.name&quot; . }}-secrets{{- end }}
      initContainers:
      - name: init-wait-for-dependencies
        image: wshihadeh/wait_for:v1.2
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        command: [&quot;/docker-entrypoint.sh&quot;]
        args: [&quot;wait_for_tcp&quot;, &quot;postgress:DATABASE_HOST:DATABASE_PORT&quot;]
        envFrom:
        - configMapRef:
            name: {{ template &quot;my-chart.name&quot; . }}-configmap
        - secretRef:
            name: {{ if .Values.existingSecret }}{{ .Values.existingSecret }}{{- else }}{{ template &quot;my-chart.name&quot; . }}-secrets{{- end }}
      imagePullSecrets:
      - name: {{ .Values.imagePullSecretName }}
      restartPolicy: Never

</code></pre>
<p>Note the following :
1- The Job definitions have helm hooks to run on each deployment and to be the first task</p>
<pre><code>    &quot;helm.sh/hook&quot;: pre-install,pre-upgrade
    &quot;helm.sh/hook-weight&quot;: &quot;-1&quot;
    &quot;helm.sh/hook-delete-policy&quot;: hook-succeeded
</code></pre>
<p>2- the container command, will take care of preparing the db</p>
<pre><code>command: [&quot;/docker-entrypoint.sh&quot;]
args: [&quot;rake&quot;, &quot;db:extensions&quot;, &quot;db:migrate&quot;, &quot;db:seed&quot;]
</code></pre>
<p>3- The job will not start until the db-connection is up (this is achieved via initContainers)</p>
<pre><code>args: [&quot;wait_for_tcp&quot;, &quot;postgress:DATABASE_HOST:DATABASE_PORT&quot;]
</code></pre>
<p>the second step is to define the application deployment object. This can be a regular deployment object (make sure that you don't use helm hooks ) example :</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template &quot;my-chart.name&quot; . }}-web
  annotations:
    checksum/config: {{ include (print $.Template.BasePath &quot;/configmap.yaml&quot;) . | sha256sum  }}
    checksum/secret: {{ include (print $.Template.BasePath &quot;/secrets.yaml&quot;) . | sha256sum  }}
  labels:
    app: {{ template &quot;my-chart.name&quot; . }}
    chart: {{ template &quot;my-chart.chart&quot; . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.webReplicaCount }}
  selector:
    matchLabels:
      app: {{ template &quot;my-chart.name&quot; . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath &quot;/configmap.yaml&quot;) . | sha256sum  }}
        checksum/secret: {{ include (print $.Template.BasePath &quot;/secrets.yaml&quot;) . | sha256sum  }}
      labels:
        app: {{ template &quot;my-chart.name&quot; . }}
        release: {{ .Release.Name }}
        service: web
    spec:
      imagePullSecrets:
      - name: {{ .Values.imagePullSecretName }}
      containers:
        - name: {{ template &quot;my-chart.name&quot; . }}-web
          image: &quot;{{ .Values.image.repository }}:{{ .Values.image.tag }}&quot;
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: [&quot;/docker-entrypoint.sh&quot;]
          args: [&quot;web&quot;]
          envFrom:
          - configMapRef:
              name: {{ template &quot;my-chart.name&quot; . }}-configmap
          - secretRef:
              name: {{ if .Values.existingSecret }}{{ .Values.existingSecret }}{{- else }}{{ template &quot;my-chart.name&quot; . }}-secrets{{- end }}
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          resources:
{{ toYaml .Values.resources | indent 12 }}
      restartPolicy: {{ .Values.restartPolicy  }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
</code></pre>
