Why busybox pod keep restarting after completed with exitCode 0

8/29/2019

Created a pod using following command

 kubectl run bb --image=busybox --generator=run-pod/v1 --command -- sh -c "echo hi"

Pod is getting created repeatedly

bb    1/1   Running             1     7s
bb    0/1   Completed           1     8s
bb    0/1   CrashLoopBackOff    1     9s
bb    0/1   Completed           2     22s
bb    0/1   CrashLoopBackOff    2     23s
bb    0/1   Completed           3     53s

exit code is 0

k describe pod bb

...

    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Thu, 29 Aug 2019 22:58:36 +0000
      Finished:     Thu, 29 Aug 2019 22:58:36 +0000
    Ready:          False
    Restart Count:  7

Thanks

-- Chaminda
kubernetes

2 Answers

8/29/2019

Please see the Last state reason which is Completed.

Terminated: Indicates that the container completed its execution and has stopped running. A container enters into this when it has successfully completed execution or when it has failed for some reason. Regardless, a reason and exit code is displayed, as well as the container’s start and finish time. Before a container enters into Terminated, preStop hook (if any) is executed.

...
  State:          Terminated
    Reason:       Completed
    Exit Code:    0
    Started:      Wed, 30 Jan 2019 11:45:26 +0530
    Finished:     Wed, 30 Jan 2019 11:45:26 +0530
...

Please see more details here. And you can try something like this will show the difference.

kubectl run bb --image=busybox --generator=run-pod/v1 --command -- sh -c "sleep 1000"
-- Bimal
Source: StackOverflow

8/29/2019

kubectl run defaults to setting the "restart policy" to "Always". It also sets up a Deployment in this case, to manage the pod.

--restart='Always': The restart policy for this Pod.  Legal values
 [Always, OnFailure, Never].  If set to 'Always' a deployment is created,
 if set to 'OnFailure' a job is created, if set to 'Never', a regular pod
 is created. For the latter two --replicas must be 1.  Default 'Always',
 for CronJobs `Never`.

If you change the command to:

kubectl run bb \
  --image=busybox \
  --generator=run-pod/v1 \
  --restart=Never \
  --command -- sh -c "echo hi"

A Job will be setup and the pod won't be restarted.

Outside of kubectl run

All pod specs will include a restartPolicy, which defaults to Always so must be specified if you want different behaviour.

spec:
  template:
    spec:
      containers:
      - name: something
      restartPolicy: Never

If you are looking to run a task to completion, try a Job instead.

-- Matt
Source: StackOverflow