Kubectl exec: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 8: Line 8:


=Example=
=Example=
==Read-Only Commands==


Get the date on the target pod:
Get the date on the target pod:


   kubectl exec my-pod date
   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


=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 -). This is particularly useful if the command to be executed has its own command line arguments that start with '-'. 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.


=Flags=
=Flags=
Line 25: Line 45:
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.


-------


Examples:
  # Get output from running 'date' command from pod mypod, using the first container by default
  kubectl exec mypod date
  # Get output from running 'date' command in ruby-container from pod mypod
  kubectl exec mypod -c ruby-container date
  # Switch to raw terminal mode, sends stdin to 'bash' in ruby-container from pod mypod
  # and sends stdout/stderr from 'bash' back to the client
  kubectl exec mypod -c ruby-container -i -t -- bash -il


   # List contents of /usr from the first container of pod mypod and sort by modification time.
   # List contents of /usr from the first container of pod mypod and sort by modification time.

Revision as of 20:10, 3 March 2020

Internal

Overview

Execute a command in a container:

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

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

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 -). This is particularly useful if the command to be executed has its own command line arguments that start with '-'. 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.

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.



 # 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).