Executing Commands Against a Pod

I recently responded to another user in a Slack channel regarding this topic and thought I’d post it here as well. The discussion revolved around when you need to use the -- command demarcation with the kubectl exec command.

As for the necessity or not of the — command, it depends on what you’re doing. If you’re not passing any arguments to the command, it’s not needed.

Let’s create a new pod based on busybox to test the theory: kubectl run bb --image=busybox --restart=Never --command -- sleep 3600

If we want to list the directories with ls, we can do it your way:

$ kubectl exec bb ls
bin
dev
etc
home
proc
root
sys
tmp
usr
var

Let’s say we want to know more about the contents so we try to do a long listing. This will generate an error.

$ kubectl exec bb ls -al
Error: unknown shorthand flag: 'a' in -al

However, if we include the --, the command and argument(s) are handled properly and we get the desired results.

$ kubectl exec bb -- ls -al
total 44
drwxr-xr-x    1 root     root          4096 Dec  8 21:17 .
drwxr-xr-x    1 root     root          4096 Dec  8 21:17 ..
-rwxr-xr-x    1 root     root             0 Dec  8 21:17 .dockerenv
drwxr-xr-x    2 root     root         12288 Dec  2 20:12 bin
drwxr-xr-x    5 root     root           360 Dec  8 21:17 dev
drwxr-xr-x    1 root     root          4096 Dec  8 21:17 etc
drwxr-xr-x    2 nobody   nogroup       4096 Dec  2 20:12 home
dr-xr-xr-x  238 root     root             0 Dec  8 21:17 proc
drwx------    2 root     root          4096 Dec  2 20:12 root
dr-xr-xr-x   13 root     root             0 Dec  8 21:16 sys
drwxrwxrwt    2 root     root          4096 Dec  2 20:12 tmp
drwxr-xr-x    3 root     root          4096 Dec  2 20:12 usr
drwxr-xr-x    1 root     root          4096 Dec  8 21:17 var

Hopefully this will clear up any confusion.

Post to Twitter

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.