Home > Enterprise >  run initContainer only once
run initContainer only once

Time:10-13

I have a yamlmanifest with parallelism: 2, including one initContainers. The command in the initContainers, therefore, runs two times and cause problems to the main command. How can I make it run only once?

Here are the important parts of the yaml

kind: Job
apiVersion: batch/v1
metadata:
  name: bankruptcy
spec:
  parallelism: 2
  template:
    metadata:
      labels:
        app: bankruptcy
    spec:
      restartPolicy: Never
      containers:
        - name: bankruptcy
          image: "myimage"
          workingDir: /mount/
          command: ["bash","./sweep.sh"]
          resources:
            limits:
              nvidia.com/gpu: 1
      initContainers:
        - name: dev-init-sweep
          image: 'myimage'
          workingDir: /mount/
          command: ['/bin/bash']
          args:
            - '--login'
            - '-c'
            - 'wandb sweep ./sweep.yaml 2>&1 | tee ./wandb/sweep-output.txt; echo `expr "$(cat ./wandb/sweep-output.txt)" : ".*\(wandb agent.*\)"` > ./sweep.sh;'

CodePudding user response:

An initContainer runs once per Pod. You can't make the initContainer run only once for a given number of pods. But you could implement a guard as part of your initContainer that detects that another one has already started and just returns without performing an own operation or waits until a condition is met.

You have to implement it yourself, though, there is no support from Kubernetes for this.

  • Related