Search code examples
kuberneteskubectlkubernetes-podkatacoda

Why is Kubernetes pod failing to start?


I was trying to test one scenario where pod will mount a volume and it will try to write one file to it. Below mentioned yaml works fine when I exclude command and args. However with command and args it fails with "crashloopbackoff". The describe command is not providing much information for the failure. What's wrong here?

Note: I was running this yaml on katacoda.

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: voltest
  name: voltest
spec:
  replicas: 1
  selector:
    matchLabels:
      run: voltest
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: voltest
    spec:
      containers:
      - image: nginx
        name: voltest
        volumeMounts:
        - mountPath: /var/local/aaa
          name: mydir
        command: ["/bin/sh"]
        args: ["-c", "echo 'test complete' > /var/local/aaa/testOut.txt"]
      volumes:
      - name: mydir
        hostPath:
            path: /var/local/aaa
            type: DirectoryOrCreate

Describe command output:

    Events:
  Type     Reason     Age                From                   Message
  ----     ------     ----               ----                   -------
  Normal   Scheduled  49s                default-scheduler      Successfully assigned default/voltest-78678dd56c-h5frs to controlplane
  Normal   Pulling    19s (x3 over 48s)  kubelet, controlplane  Pulling image "nginx"
  Normal   Pulled     17s (x3 over 39s)  kubelet, controlplane  Successfully pulled image "nginx"
  Normal   Created    17s (x3 over 39s)  kubelet, controlplane  Created container voltest
  Normal   Started    17s (x3 over 39s)  kubelet, controlplane  Started container voltest
  Warning  BackOff    5s (x4 over 35s)   kubelet, controlplane  Back-off restarting failed container

Solution

  • You've configured your pod to run a single shell command:

    command: ["/bin/sh"]
    args: ["-c", "echo 'test complete' > /var/testOut.txt"]
    

    This means that the pod starts up, runs echo 'test complete' > /var/testOut.txt, and then immediately exits. From the perspective of kubernetes, this is a crash.

    You've replaced the default behavior of the nginx image ("run nginx") with a shell command.

    If you want the pod to continue running, you'll need to arrange for it to run some sort of long-running command. A simple solution would be something like:

    command: ["/bin/sh"]
    args: ["-c", "echo 'test complete' > /var/testOut.txt; sleep 3600"]
    

    This will cause the pod to sleep for an hour before exiting, giving you time to inspect the results of your shell command.


    Note that your shell command isn't testing anything useful; you've mounted your mydir volume on /var/local/aaa, but your shell command is writing to /var/testOut.txt, so it's not making any use of the volume.