Executing multiple commands( or from a shell script) in a kubernetes pod

你说的曾经没有我的故事 提交于 2019-11-29 13:44:20

Are you running all these commands as a single line command? First of all, there's no ; or && between those commands. So if you paste it as a multi-line script to your terminal, likely it will get executed locally.

Second, to tell bash to execute something, you need: bash -c "command".

Try running this:

$ kubectl exec POD_NAME -- bash -c "date && echo 1"

Wed Apr 19 19:29:25 UTC 2017
1

You can make it multiline like this:

$ kubectl exec POD_NAME -- bash -c "date && \
      echo 1 && \
      echo 2"

-t can solve your task

For example, I run here few cmd:

kubectl get pods |grep nginx|cut -f1 -d\  |\
while read pod; \
 do echo "$pod writing:";\
  kubectl exec -t $pod -- bash -c \
   "dd if=/dev/zero of=/feeds/test.bin bs=260K count=4 2>&1|\
    grep copi |cut -d, -f4; \
    a=$SECONDS; echo -ne 'reading:'; cat /feeds/test.bin >/dev/null ; \
    let a=SECONDS-a ; \
    echo $a sec"
done

p.s. your example will be:

kubectl exec -t octavia-api-worker-pod-test -c octavia-api -- bash -c "unset http_proxy https_proxy ; mv /usr/local/etc/octavia/octavia.conf /usr/local/etc/octavia/octavia.conf-orig ; /usr/local/bin/octavia-db-manage --config-file /usr/local/etc/octavia/octavia.conf ; upgrade ; head"

The following should work

kubectl -it exec podname -- bash -c "ls && ls"

bin   dev   etc   home  proc  root  run   sys   tmp   usr   var bin  
dev   etc   home  proc  root  run   sys   tmp   usr   var

If above command doesn't work then try too replace bash with one of the following /bin/bash, sh, bin/sh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!