![kubernetes容器上的远程命令执行](https://linux22.com/image/722819/kubernetes%E5%AE%B9%E5%99%A8%E4%B8%8A%E7%9A%84%E8%BF%9C%E7%A8%8B%E5%91%BD%E4%BB%A4%E6%89%A7%E8%A1%8C.png)
我有一个 kubernetes 集群,我正在尝试检查在其上运行的容器的磁盘利用率。
$kubectl version
Client Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.8", GitCommit:"e8c167a115ec662726904265d17f75a6d79d78d8", GitTreeState:"clean", BuildDate:"2017-10-01T00:19:21Z", GoVersion:"go1.7.6", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.8", GitCommit:"e8c167a115ec662726904265d17f75a6d79d78d8", GitTreeState:"clean", BuildDate:"2017-10-01T00:01:59Z", GoVersion:"go1.7.6", Compiler:"gc", Platform:"linux/amd64"}
这对我有用
$kubectl -n example-system exec -it example-monitoring-0 df
Filesystem 1K-blocks Used Available Use% Mounted on
overlay 98227828 17592896 76340692 19% /
tmpfs 15701160 0 15701160 0% /dev
tmpfs 15701160 0 15701160 0% /sys/fs/cgroup
/dev/xvdbg 309506048 149730860 146106552 51% /prometheus
/dev/xvda1 98227828 17592896 76340692 19% /dev/termination-log
/dev/xvda1 98227828 17592896 76340692 19% /etc/prometheus
/dev/xvda1 98227828 17592896 76340692 19% /etc/resolv.conf
/dev/xvda1 98227828 17592896 76340692 19% /etc/hostname
/dev/xvda1 98227828 17592896 76340692 19% /etc/hosts
shm 65536 0 65536 0% /dev/shm
tmpfs 15701160 12 15701148 0% /var/run/secrets/kubernetes.io/serviceaccount
tmpfs 15701160 0 15701160 0% /proc/kcore
tmpfs 15701160 0 15701160 0% /proc/timer_list
tmpfs 15701160 0 15701160 0% /proc/timer_stats
tmpfs 15701160 0 15701160 0% /proc/sched_debug
但这对我来说不起作用
$kubectl -n example-system exec -it example-monitoring-0 df -kh
Error: unknown shorthand flag: 'k' in -kh
Examples:
# Get output from running 'date' from pod 123456-7890, using the first container by default
kubectl exec 123456-7890 date
# Get output from running 'date' in ruby-container from pod 123456-7890
kubectl exec 123456-7890 -c ruby-container date
# Switch to raw terminal mode, sends stdin to 'bash' in ruby-container from pod 123456-7890
# and sends stdout/stderr from 'bash' back to the client
kubectl exec 123456-7890 -c ruby-container -i -t -- bash -il
Options:
-c, --container='': Container name. If omitted, the first container in the pod will be chosen
-p, --pod='': Pod name
-i, --stdin=false: Pass stdin to the container
-t, --tty=false: Stdin is a TTY
Usage:
kubectl exec POD [-c CONTAINER] -- COMMAND [args...] [options]
Use "kubectl options" for a list of global command-line options (applies to all commands).
实际上我尝试了以下组合但没有效果。
kubectl -n example-system exec -it example-monitoring-0 `df -kh`
kubectl -n example-system exec -it example-monitoring-0 'df -kh'
kubectl -n example-system exec -it example-monitoring-0 "df -kh"
cmd="df -kh"
kubectl -n example-system exec -it example-monitoring-0 $cmd
kubectl -n example-system exec -it example-monitoring-0 echo $cmd
我希望有人一定遇到过这样的问题。
答案1
您可能需要使用双破折号分隔符,这通常意味着没有更多的命令选项,并且其后的所有内容都被视为带有空格的字符串。
$ kubectl -n example-system exec -it example-monitoring-0 -- df -kh
这里这是 Unix&Linux 网站上关于此问题的热门问题。