Docker

在 Kubernetes 容器上執行遠端命令

  • June 6, 2018

我有 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

我希望有人一定遇到過這種問題。

您可能想要使用雙破折號分隔符,這通常意味著沒有更多的命令選項,並且它之後的所有內容都被視為帶有空格的字元串。

$ kubectl -n example-system exec -it example-monitoring-0 -- df -kh

是 Unix&Linux 網站上關於它的熱門問題。

引用自:https://serverfault.com/questions/915399