How to run one-time run command and get result
By running the command "kubectl exec -ti", we can get the output of the command result as follow; Using "kubectl-debug" can not get the desired result, Any Idea?
# kubectl exec -ti tkservice-5ffbb64854-6k2h6 -- ls -hl
total 30M
-rw-r--r-- 1 root root 263 Oct 31 09:59 Dockerfile
drwxr-xr-x 7 root root 4.0K Oct 31 09:59 config
-rw-r--r-- 1 root root 188 Oct 31 09:59 main.yml
# kubectl-debug tkservice-5ffbb64854-6k2h6 -- ls -hl
Agent Pod info: [Name:debug-agent-pod-146dc5f3-fc7d-11e9-a689-00163e03e155, Namespace:default, Image:debug-agent:latest, HostPort:10027, ContainerPort:10027]
Waiting for pod debug-agent-pod-146dc5f3-fc7d-11e9-a689-00163e03e155 to run...
pod tkservice-5ffbb64854-6k2h6 PodIP 10.81.133.178, agentPodIP 172.xxx.xxx.211
wait for forward port to debug agent ready...
Forwarding from 127.0.0.1:10027 -> 10027
Handling connection for 10027
pulling image netshoot...
latest: Pulling from paas-dev/netshoot
Digest: sha256:8b020dc72d8ef07663e44c449f1294fc47c81a10ef5303dc8c2d9635e8ca22b1
Status: Image is up to date for netshoot:latest
starting debug container...
container created, open tty...
Start deleting agent pod tkservice-5ffbb64854-6k2h6
end port-forward...
kubectl-debug pod_name -- /bin/sh -c "sleep 2 && ls -hl"
It works.
Because sleep 2 extends the lifecycle of the container, giving the chance to the attach cmd catch the container output when it runs a very short task.
@DavadDi Nice catch! I will investigate how to guarantee the output stream being flushed before the debug container get terminated.
Also, the log without verbosity control concerns me, which adds complexity for automation process to parse the output of executed command. I will file another issue for this.
A good approach with sleep, but if you execute kubectl debug via code(e.g. exec.Command in golang), you cannot get the one-time command's output according to my experience.
One approach I came up with is:
- modify the netshoot image with an additional volume.
- sleep 2 && cmd > file in that volume.
Any better idea? @aylei
A little ideat: use sleep infinity command to create debug container, then send user cmd(ls -hl) to stdin after attach, otherwise send the default cmd sh to stdin.