簡體   English   中英

在 kubernetes pod 內執行命令(bash 腳本)

[英]Execute command inside kubernetes pod (bash script)

我正在嘗試從 shell 腳本在 postgres 容器內執行命令。 這是我到目前為止:

kubectl exec -it <postgres_pod> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select count from table where name='FOO';'"

我收到以下錯誤:

ERROR: column "foo" does not exist LINE 1: select count from table where name=FOO; ^

查詢在容器內運行良好,因此我傳遞命令的方式一定有問題。 我確實嘗試了另一個查詢:

kubectl exec -it <postgres_pod> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select * from table;'"

這運行良好。 所以,我猜這與我傳遞 where 子句where name='FOO' 我怎樣才能讓它發揮作用。 請幫幫我。

更新:

嘗試使用以下方法進行轉義:

1:雙引號

kubectl exec -it <postgres_pod> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select count from table where name=\\"FOO\\";'"

ERROR:  column "FOO" does not exist
LINE 1: select count from table where name="FOO";
                                            ^

2:單引號

kubectl exec -it <postgres_pod> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select count from table where name=\\'FOO\\';'"

bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file
command terminated with exit code 1

我在 where 子句中使用了$$ 美元引用的字符串,並使用/$它們進行了轉義。

kubectl exec -it <postgres_pod> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select count from table where name=\$\$FOO\$\$;'"

那是因為引號沒有正確轉義,然后 FOO 被假定為列名。

嘗試

kubectl exec -it <pod_id> -n <deployment> -- bash -c "psql -U postgres -d database -c 'select count from table where name=\"FOO\";'"

看起來像查詢條件元素,name='value' 應該在單引號內。

試試這個:它有效!

kubectl exec -it <pod_id> -n <deployment> -- bash -c "psql -U postgres -d database -c \"select count from table where name='FOO'\""

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM