Kubectl exec: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(13 intermediate revisions by the same user not shown)
Line 5: Line 5:
Execute a command in a container:
Execute a command in a container:


kubectl exec <''pod''|''type/name''> [-c <''container''>] [''flags''] -- <''command''> [''args''...] [''options'']
<syntaxhighlight lang='text'>
kubectl exec <pod-name|type/name> [-c <container>] [flags] -- <command> [args...] [options]
</syntaxhighlight>


=Example=
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:
<syntaxhighlight lang='text'>
kubectl exec my-pod -it bash -c my-command
</syntaxhighlight>


==Read-Only Commands==
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.


Get the date on the target pod:
The correct command is:
<syntaxhighlight lang='text'>
kubectl exec my-pod -it -- bash -c my-command
</syntaxhighlight>
which tells kubectl to execute "bash -c my-command" as command on the pod.


  kubectl exec my-pod date
=Example=


==Interactive Commands==
==<span id='Read-Only_Commands'></span>Execute Commands Remotely into a Pod==


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:
Get the date on the target pod:
<syntaxhighlight lang='bash'>
kubectl exec my-pod -- date
</syntaxhighlight>


  kubectl exec my-pod -it -- bash -il
List the content of the container's root filesystem.


=The Role of '--' on Command Line=
<syntaxhighlight lang='bash'>
kubectl exec my-pod -- ls /
</syntaxhighlight>


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 -). This is particularly useful if the command to be executed has its own command line arguments that start with '-'. For example,
Delete a file on the container's root filesystem:
<syntaxhighlight lang='bash'>
kubectl exec my-pod -- rm /tmp/some.file
</syntaxhighlight>


kubectl exec my-pod -it bash -c my-command
If the target file does not exist, the kubectl returns a non-zero exit code:
<syntaxhighlight lang='bash'>
kubectl exec my-pod -- rm /tmp/some.file; echo $?
rm: can't remove '/tmp/some.file': No such file or directory
command terminated with exit code 1
1
</syntaxhighlight>


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.
Note that [[Bash_Command_Line_Expansion#Globbing|globbing]] cannot be used with commands like <code>rm</code> or <code>ls</code> executed directly. A shell will be needed to interpret it:
<syntaxhighlight lang='bash'>
kubectl exec my-pod -- sh -c 'rm /tmp/some-dir/*'
</syntaxhighlight>


The correct command is:
==Interactive Commands==


  kubectl exec my-pod -it -- bash -c my-command
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:


which tells kubectl to execute "bash -c my-command" as command on the pod.
<syntaxhighlight lang='bash'>
kubectl exec my-pod -it -- bash -il
</syntaxhighlight>


=Flags=
=Flags=
Line 40: Line 77:
==-i,--stdin=false ==
==-i,--stdin=false ==
Pass stdin to the container.
Pass stdin to the container.
==-t,--tty=false==
==-t,--tty=false==
Stdin is a TTY
Stdin is a TTY
==--pod-running-timeout= ==
==--pod-running-timeout= ==
The length of time (like 5s, 2m, or 3h, higher than zero) to wait until at least one pod is running.
The length of time (like 5s, 2m, or 3h, higher than zero) to wait until at least one pod is running.
-------
  # List contents of /usr from the first container of pod mypod and sort by modification time.
  # If the command you want to execute in the pod has any flags in common (e.g. -i),
  # you must use two dashes (--) to separate your command's flags/arguments.
  # Also note, do not surround your command and its flags/arguments with quotes
  # unless that is how you would execute it normally (i.e., do ls -t /usr, not "ls -t /usr").
  kubectl exec mypod -i -t -- ls -t /usr
  # Get output from running 'date' command from the first pod of the deployment mydeployment, using the first container
by default
  kubectl exec deploy/mydeployment date
  # Get output from running 'date' command from the first pod of the service myservice, using the first container by
default
  kubectl exec svc/myservice date
Usage:
 
Use "kubectl options" for a list of global command-line options (applies to all commands).

Latest revision as of 02:25, 18 May 2021

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

Execute Commands Remotely into a Pod

Get the date on the target pod:

kubectl exec my-pod -- date

List the content of the container's root filesystem.

kubectl exec my-pod -- ls /

Delete a file on the container's root filesystem:

kubectl exec my-pod -- rm /tmp/some.file

If the target file does not exist, the kubectl returns a non-zero exit code:

kubectl exec my-pod -- rm /tmp/some.file; echo $?
rm: can't remove '/tmp/some.file': No such file or directory
command terminated with exit code 1
1

Note that globbing cannot be used with commands like rm or ls executed directly. A shell will be needed to interpret it:

kubectl exec my-pod -- sh -c 'rm /tmp/some-dir/*'

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.