在 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

那是因为引号没有正确转义,然后 FOO 被假定为列名。

尝试

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

我在 where 子句中使用了 $$ dollar-quoted string 并使用 /$.

对它们进行了转义
kubectl exec -it <postgres_pod> -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'\""