Execute a command in a container.

Usage:

kubectl exec POD — COMMAND [args…]
kubectl exec -f FILENAME — COMMAND [args…]
kubectl exec TYPE/NAME — COMMAND [args…]

Options:

-c, —container=’’: 指定的是YAML定义文件中的容器名。
-f, —filename=[]: to use to exec into the resource
-i, —stdin=false: Pass stdin to the container
-t, —tty=false: Stdin is a TTY

Examples:

Get output from running ‘date’ command from pod mypod, using the firstcontainer 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 -it -- bash -il

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

通过定义文件在资源中的容器执行命令
kubectl exec -f kubia-svc.yaml -- curl -s http://localhost:8080

交互式登录pod,在bash中执行命令
kubectl exec kubia-bcg5z -it -- bash

查看pod中的环境变量
kubectl exec POD -- env