Running command in loop for n times inside kubernetes pods container

12/23/2018

Basically, I need clarification if this is the right way to do: I am able to run sed command inside a container on a k8s pod. Now, the same sed I want to loop over for 10times but am not sure if this is working though I get no error from kubernetes pods or logs. Please confirm if my looping is good.

'sed -i "s/\(training:\).*/\1 12/" ghav/default_sql.spec.txt &&
         lant estimate -e dlav/lat/experiment_specs/default_sql.spec.txt -r /out'

I want to do this working command 10times inside the same container. is the below right?

'for run in $(seq 1 10); do sed -i "s/\(training:\).*/\1 12/" ghav/default_sql.spec.txt &&
         lant estimate -e dlav/lat/experiment_specs/default_sql.spec.txt -r /out; done' 

the pod gets created and is running fine but am not sure how to confirm my loop is good and am doing that 10times...

inside pod describe I see below

Args:
  sh
  -c
  'for run in $(seq 1 10); do sed -i "s/\(training:\).*/\1 12/" ghav/default_sql.spec.txt &&
         lant estimate -e dlav/lat/experiment_specs/default_sql.spec.txt -r /out; done'
-- AhmFM
bash
docker
kubernetes
kubernetes-pod
sed

1 Answer

12/24/2018

The "Define a Command and Arguments for a Container" does mention:

To see the output of the command that ran in the container, view the logs from the Pod:

kubectl logs command-demo

So make sure that your command, for testing, does echo something, and check the pod logs.

sh -c 'for run in $(seq 1 10); do echo "$run"; done'

As in:

command: ["/bin/sh"]
args: ["-c", "for run in $(seq 1 10); do echo \"$run\"; done"]

(using seq here, as mentioned in kubernetes issue 56631)

For any complex sequence of commands, mixing quotes, it is best to wrap that sequence in a script file, and call that executable file 10 tiles. The logs will confirm that the loop is executed 10 times.

-- VonC
Source: StackOverflow