Remote Command Execution on Kubernetes Container

containersdockerkubernetes

I am having kubernetes cluster where I am trying to check disk utilisation of containers running on it.

$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"}

This works for me

$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

However this doesn't work for me

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

Actually I tried below combinations however didn't work.

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

I hope someone must have faced this sort of issue.

Best Answer

You may want to use double dash separator, which usually means that there is no more command options and everything after it is considered as a string with spaces.

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

Here is the popular question about it on Unix&Linux site.

Related Topic