在 kubernetes pod 内执行命令(bash 脚本)

el3*_*323 1 bash kubernetes kubectl

我正在尝试从 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;'"
Run Code Online (Sandbox Code Playgroud)

这运行良好。所以,我猜这与我传递 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";
                                            ^
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

el3*_*323 5

我在 where 子句中使用了用$$ 美元引用的字符串,并使用/$.

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