Issues with initContainer and using if statement in command:[]

PNRao:
apiVersion: v1
kind: Pod
metadata:
name: hungry-bear
spec:
volumes:

  • name: workdir
    emptyDir: {}
    containers:
  • name: checker
    image: busybox
    command: [ " /bin/sh " , " -c " , " if [-f /workdir/faithful.txt] ; then sleep 1000; else exit 1; fi " ]
    volumeMounts:
    • name: workdir
      mountPath: / workdir
      initContainers:
    • command:
      • “/bin/sh”
      • -c
      • “touch /workdir/faithful.txt”
        name: init-c
        image: busybox
        volumeMounts:
      • name: workdir
        mountPath: /workdir

KK:
Hi <@U011C2JE91D>

KK:
Looks like there is a gap in mountPath: /workdir in your yaml (in the container section)

PNRao:
i have corrected that one

PNRao:
still no go

KK:

image.png

KK:
What is the error you are getting?

PNRao:
master k apply -f 1.yaml pod/hungry-bear created master k get po
NAME READY STATUS RESTARTS AGE
hungry-bear 0/1 Init:0/1 0 4s
master k get po NAME READY STATUS RESTARTS AGE hungry-bear 0/1 RunContainerError 0 8s master

PNRao:
Events:
Type Reason Age From Message


Normal Scheduled <unknown> default-scheduler Successfully assigned default/hungry-bear to node01
Normal Pulling 55s kubelet, node01 Pulling image “busybox”
Normal Pulled 53s kubelet, node01 Successfully pulled image “busybox”
Normal Created 53s kubelet, node01 Created container init-c
Normal Started 53s kubelet, node01 Started container init-c
Warning BackOff 18s kubelet, node01 Back-off restarting failed container
Normal Pulling 7s (x4 over 52s) kubelet, node01 Pulling image “busybox”
Normal Pulled 5s (x4 over 50s) kubelet, node01 Successfully pulled image “busybox”
Normal Created 5s (x4 over 50s) kubelet, node01 Created container checker
Warning Failed 5s (x4 over 50s) kubelet, node01 Error: failed to start container “checker”: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: " /bin/sh “: stat /bin/sh : no such file or directory”: unknown

KK:
Looks like syntax issues in command:[]

KK:
remove spaces for /bin/sh and that will fix your “no such file or directory” issue.

" /bin/sh " ----> “/bin/sh”

Under containers section

PNRao:
let me correct

PNRao:
Events:
Type Reason Age From Message


Normal Scheduled <unknown> default-scheduler Successfully assigned default/test to node01
Normal Pulling 17s kubelet, node01 Pulling image “busybox”
Normal Pulled 16s kubelet, node01 Successfully pulled image “busybox”
Normal Created 16s kubelet, node01 Created container init-c
Normal Started 15s kubelet, node01 Started container init-c
Normal Pulling 13s (x2 over 15s) kubelet, node01 Pulling image “busybox”
Normal Pulled 11s (x2 over 13s) kubelet, node01 Successfully pulled image “busybox”
Normal Created 11s (x2 over 13s) kubelet, node01 Created container checker
Normal Started 11s (x2 over 13s) kubelet, node01 Started container checker
Warning BackOff 9s (x2 over 10s) kubelet, node01 Back-off restarting failed container
master $ k get po
NAME READY STATUS RESTARTS AGE
test 0/1 CrashLoopBackOff 2 38s

PNRao:
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
volumes:

  • name: workdir
    emptyDir: {}
    containers:
  • name: checker
    image: busybox
    command: [ “/bin/sh” , “-c” , "if [-f /workdir/faithful.txt]; then sleep 100000; else exit 1; fi " ]
    volumeMounts:
    • name: workdir
      mountPath: /workdir
      initContainers:
    • command:
      • “/bin/sh”
      • -c
      • “touch /workdir/faithful.txt”
        name: init-c
        image: busybox
        volumeMounts:
      • name: workdir
        mountPath: /workdir

KK:
Check the logs

KK:
kubectl logs -f <podname>

PNRao:
master $ k logs test
/bin/sh: [-f: not found

PNRao:
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
volumes:

  • name: workdir
    emptyDir: {}
    containers:
  • name: checker
    image: busybox
    command: [ “/bin/sh” , “-c” , "if [ -f /workdir/faithful.txt ]; then sleep 100000; else exit 1; fi " ]
    volumeMounts:
    • name: workdir
      mountPath: /workdir
      initContainers:
    • command:
      • “/bin/sh”
      • -c
      • “touch /workdir/faithful.txt”
        name: init-c
        image: busybox
        volumeMounts:
      • name: workdir
        mountPath: /workdir

PNRao:
this fixed the issue…

PNRao:
thank you