问题描述
我正在编写一个shell脚本,该脚本需要登录到pod并在kubernetes pod中执行一系列命令.
I'm writing a shell script which needs to login into the pod and execute a series of commands in a kubernetes pod.
下面是我的sample_script.sh
below is my sample_script.sh
kubectl exec octavia-api-worker-pod-test -c octavia-api bashunset http_proxy https_proxymv /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
kubectl exec octavia-api-worker-pod-test -c octavia-api bashunset http_proxy https_proxymv /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
运行此脚本后,没有任何输出.任何帮助将不胜感激
After running this script, I'm not getting any output.Any help will be greatly appreciated
推荐答案
您是否将所有这些命令作为单行命令运行?首先,这些命令之间没有;
或&&
.因此,如果将其作为多行脚本粘贴到终端,则很有可能会在本地执行.
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.
第二,要告诉bash执行某件事,您需要:bash -c "command"
.
Second, to tell bash to execute something, you need: bash -c "command"
.
尝试运行此:
$ 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"
这篇关于在kubernetes容器中执行多个命令(或从shell脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!