<p>I have pod that includes one init container and one app container,
between them there is a volume with shared folder.</p>
<p>My issue is that once a day or even more, the init container run itself and therefore it delete the node modules from the volume, then the main app crash because of missing modules.
The app container is not making any restarts, only the init container.</p>
<p>is anyone familiar with this issue in k8s? why those restarts happens only in the init container?</p>
<p>Thanks :)</p>
<p>edit:
the deployment yaml file -</p>
<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: &quot;25&quot;
  creationTimestamp: &quot;2020-05-19T06:48:18Z&quot;
  generation: 25
  labels:
    apps: ******
    commit: ******
  name: *******
  namespace: fleet
  resourceVersion: &quot;24059934&quot;
  selfLink: *******
  uid: *******
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: *******
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: *******
        commit: *******
        revision: *******
    spec:
      containers:
        image: XXXXXXXXXXXX
        imagePullPolicy: IfNotPresent
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /ping
            port: http
            scheme: HTTP
          initialDelaySeconds: 120
          periodSeconds: 30
          successThreshold: 1
          timeoutSeconds: 30
        name: *******
        ports:
        - containerPort: 1880
          name: http
          protocol: TCP
        readinessProbe:
          failureThreshold: 20
          httpGet:
            path: /ping
            port: http
            scheme: HTTP
          initialDelaySeconds: 20
          periodSeconds: 5
          successThreshold: 1
          timeoutSeconds: 30
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /opt/breeze
          name: workdir
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: flowregistrykey
      initContainers:
        image: XXXXXXXXXXXX
        imagePullPolicy: IfNotPresent
        name: get-flow-json
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /work-dir
          name: workdir
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - emptyDir: {}
        name: workdir
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: &quot;2020-06-01T12:30:10Z&quot;
    lastUpdateTime: &quot;2020-06-01T12:30:10Z&quot;
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: &quot;True&quot;
    type: Available
  - lastTransitionTime: &quot;2020-05-19T06:48:18Z&quot;
    lastUpdateTime: &quot;2020-06-01T12:45:05Z&quot;
    message: ReplicaSet &quot;collection-associator.sandbox.services.collection-8784dcb9d&quot;
      has successfully progressed.
    reason: NewReplicaSetAvailable
    status: &quot;True&quot;
    type: Progressing
  observedGeneration: 25
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1
</code></pre>
<p>and this image explain the problem-
the pod was created 7 days ago, but the files inside were created today, and there is no node_modules folder - because only the init container run again and not the app container so there was no mpm install</p>

Sariel
<blockquote>
  <p>the init container run itself and therefore it delete the node modules from the volume</p>
</blockquote>

<p>initContainer is only run when a pod restart. Don't treat it as service or application. It should be a script, a job only for setup before your application.</p>

<blockquote>
  <p>then the main app crash because of missing modules. </p>
</blockquote>

<p><code>node_modules</code> is not dynamic loading. It's loaded when you <code>npm start</code></p>

<h2>You might want to try <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-a-liveness-command" rel="nofollow noreferrer">livenessProbe</a>.</h2>

<blockquote>
  <p>Many applications running for long periods of time eventually transition to broken states, and cannot recover except by being restarted. Kubernetes provides liveness probes to detect and remedy such situations.</p>
</blockquote>

<pre class="lang-yaml prettyprint-override"><code>initContainers:
  - name: set-delete-time
    command: ["/bin/sh","-c"]
    args: 
      - |
        # Let's set 3600
        echo 3600 &gt; deleteTime

...
containers:
  - name: node-app
    livenessProbe:
      exec:
        command:
        # If deleteTime &gt; 0 exit 0
        # Else /tmp/deleteTime - $periodSeconds &gt; /tmp/deleteTime
        - cat
        - /tmp/deleteTime
        - ...
      initialDelaySeconds: 5
      periodSeconds: 5
</code></pre>
