kubectl exec returns 异常错误消息?

kubectl exec returns unexcepted error messages?

我目前正在尝试在我的 kubernetes pod 上执行一个简单的 bash 命令,但似乎出现了一些没有意义的错误。

如果我在 docker 容器中执行 运行 命令 plain

I have no name!@kafka-0:/tmp$ if [ $(comm -13 <(sort selectedTopics) <(sort topics.sh) | wc -l) -gt 0 ];  then  echo "hello"; fi

我得到 Hello 作为输出。

但是如果从外部执行与

相同
kubectl exec --namespace default kafka-0 -c kafka -- bash -c "if [ $(comm -13 </tmp/selectedTopics </tmp/topics.sh| wc -l) -gt 0 ];  then  echo topic does not exist && exit 1; fi"

然后我收到一条错误消息,指出 /tmp/topics.sh: No such file or directory

尽管我能做到这一点

kubectl exec --namespace $namespace kafka-0 -c kafka -- bash -c "cat /tmp/topics.sh"

为什么 kubectl exec 给我带来问题?

当你写:

 kubectl ... "$(cmd)"

cmd 在本地主机上执行以创建用作 kubectl 参数的字符串。换句话说,您是在本地主机上执行 comm -13 </tmp/selectedTopics </tmp/topics.sh| wc -l,而不是在 pod 中执行。

如果你想避免局部扩展,你应该使用单引号:

kubectl exec --namespace default kafka-0 -c kafka -- bash -c 'if comm -13 </tmp/topics.sh grep -q . ;  then  echo topic does not exist >&2 && exit 1; fi'