參考:https://kubernetes.io/zh/docs/tasks/debug-application-cluster/get-shell-running-container/
獲取容器的 Shell
獲取正在運行容器的 Shell:
kubectl exec -it shell-demo -- /bin/bash
說明:雙破折號 "--" 用於將要傳遞給命令的參數與 kubectl 的參數分開。
在容器中運行單個命令
在普通的命令窗口(而不是 shell)中,打印環境運行容器中的變量:
kubectl exec shell-demo -- env
當 Pod 包含多個容器時打開 shell
如果 Pod 有多個容器,--container 或者 -c 可以在 kubectl exec 命令中指定容器。 例如,您有個名為 my-pod 的容器,該 Pod 有兩個容器分別為 main-app 和 healper-app。 下面的命令將會打開一個 shell 訪問 main-app 容器。
kubectl exec -it my-pod -c main-app -- /bin/bash