Kubectl exec: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 12: Line 12:
==The Role of '--' on Command Line==
==The Role of '--' on Command Line==


The '--' is a sequence of characters that signals to kubectl to stop scanning command line for its own flags and options (sequences that start with -).  
The '--' is a sequence of characters that signals to kubectl to stop scanning command line for its own flags and options (sequences that start with -). Everything after the double dash is a command that should be executed inside the pod.


This is particularly useful if the command to be executed has its own command line arguments that start with '-'. If the command you want to execute in the pod has any flags in common (e.g. -i), two dashes (--) must be used to separate the command's flags/arguments from kubectl's commands/arguments.
Using the double dash is optional if the command to be executed inside the pod has no arguments that start with dash.
 
However, double dash is required if the command to be executed has its own command line arguments that start with dash. If the command you want to execute in the pod has any flags in common (e.g. -i), two dashes (--) must be used to separate the command's flags/arguments from kubectl's commands/arguments.


For example:
For example:

Revision as of 00:58, 20 September 2020

Internal

Overview

Execute a command in a container:

kubectl exec <pod-name|type/name> [-c <container>] [flags] -- <command> [args...] [options]

Do not surround the command and its flags/arguments with quotes unless that is how it would execute normally (i.e., do ls -t /usr, not "ls -t /usr").

The Role of '--' on Command Line

The '--' is a sequence of characters that signals to kubectl to stop scanning command line for its own flags and options (sequences that start with -). Everything after the double dash is a command that should be executed inside the pod.

Using the double dash is optional if the command to be executed inside the pod has no arguments that start with dash.

However, double dash is required if the command to be executed has its own command line arguments that start with dash. If the command you want to execute in the pod has any flags in common (e.g. -i), two dashes (--) must be used to separate the command's flags/arguments from kubectl's commands/arguments.

For example:

kubectl exec my-pod -it bash -c my-command

will fail because kubectl will interpret "-c" as a container name flag and try to look up "my-command" as a container in the pod - which will fail.

The correct command is:

kubectl exec my-pod -it -- bash -c my-command

which tells kubectl to execute "bash -c my-command" as command on the pod.

Example

Read-Only Commands

Get the date on the target pod:

 kubectl exec my-pod date

Interactive Commands

Start a bash into the container, switch to raw terminal mode, send stdin to the bash process in pod my-pod and sends stdout/stderr from 'bash' back to the client:

 kubectl exec my-pod -it -- bash -il

Flags

-c,--container=

The name of the container to execute the command into. If omitted, the first container in the pod will be chosen.

-i,--stdin=false

Pass stdin to the container.

-t,--tty=false

Stdin is a TTY

--pod-running-timeout=

The length of time (like 5s, 2m, or 3h, higher than zero) to wait until at least one pod is running.