HiveBrain v1.2.0
Get Started
← Back to all entries
snippetkubernetesMinor

How to run short-lived command using kubectl

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
shortlivedkubectlusinghowcommandrun

Problem

I was trying to measure pod startup time buy running the following:

% kubectl run --image busybox --attach test -- date
error: timed out waiting for the condition


At first it seems like this command takes a long time to start the pod. However if you run kubectl get pods while it is running you see something interesting.

At first the command succeeds:

% kubectl get pods
NAME   READY   STATUS      RESTARTS   AGE
test   0/1     Completed   0          2s


Then transitions to CrashLoopBackOff.

% kubectl get pods
NAME   READY   STATUS             RESTARTS   AGE
test   0/1     CrashLoopBackOff   1          7s


The logs show that the command ran successfully.

% kubectl logs test 
Thu Jan 27 15:02:36 UTC 2022


It is unclear to me why the pod is transitioning from Completed to CrashLoopBackOff. Is there a way to make kubectl do the right thing in this scenario?

The best work-around I have found so far is to use a slower command and subtract the delay from my testing.

% kubectl run --image busybox --attach test -- sleep 10

Solution

Ah, shortly after posting I found a clean solution. Adding --restart=Never makes the command work as expected.

% time kubectl run -n kcox-test --image busybox --attach test --restart=Never -- date
Thu Jan 27 15:10:41 UTC 2022
time Total: 9.449 (1%), User: 0.100, System: 0.034  - kubectl run -n kcox-test --image busybox --attach test --restart=Never -- date

Code Snippets

% time kubectl run -n kcox-test --image busybox --attach test --restart=Never -- date
Thu Jan 27 15:10:41 UTC 2022
time Total: 9.449 (1%), User: 0.100, System: 0.034  - kubectl run -n kcox-test --image busybox --attach test --restart=Never -- date

Context

StackExchange DevOps Q#15345, answer score: 2

Revisions (0)

No revisions yet.