1

I am using busybox container to understand kubernetes concepts. but if run a simple test-pod.yaml with busy box image, it is in completed state instead of running state can anyone explain the reason

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config
  restartPolicy: Never
Rajendar Talatam
  • 250
  • 1
  • 12

2 Answers2

4

Look, you should understand the basic concept here. Your docker container will be running till its main process is live. And it will be completed as soon as your main process will stop.

Going step-by-step in your case: You launch busybox container with main process "/bin/sh", "-c", "ls /etc/config/" and obviously this process has the end. Once command is completed and you listed directory - your process throw Exit:0 status, container stop to work and you see completed pod as a result.

If you want to run container longer, you should explicitly run some command inside the main process, that will keep your container running as long as you need.

Possible solutions

  • @Daniel's answer - container will execute ls /etc/config/ and will stay alive additional 3600 sec

  • use sleep infinity option. Please be aware that there was a long time ago issue, when this option hasn't worked properly exactly with busybox. That was fixed in 2019, more information here. Actually this is not INFINITY loop, however it should be enough for any testing purpose. You can find huge explanation in Bash: infinite sleep (infinite blocking) thread

Example:

apiVersion: v1
kind: Pod
metadata: 
  name: busybox-infinity
spec: 
  containers: 
    - 
      command: 
        - /bin/sh
        - "-c"
        - "ls /etc/config/"
        - "sleep infinity"
      image: busybox
      name: busybox-infinity
  • you can use different varioations of while loops, tailing and etc etc. That only rely on your imagination and needs.

Examples:

["sh", "-c", "tail -f /dev/null"]

command: ["/bin/sh"]
args: ["-c", "while true; do echo hello; sleep 10;done"]

command: [ "/bin/bash", "-c", "--" ]
args: [ "while true; do sleep 30; done;" ]
Vit
  • 7,740
  • 15
  • 40
3

That is because busybox runs the command and exits. You can solve it by updating your command in the containers section with the following command:

 [ "/bin/sh", "-c", "ls /etc/config/", "sleep 3600"]
Daniel Jacob
  • 1,455
  • 9
  • 17