C# Kubernetes Client Exec 命令进入 Pod 容器问题

问题描述 投票:0回答:1

我有一个用 .NET7 编写的自定义 Web 应用程序,我正在使用 Kubernetes 客户端 (C#) 与 Kubernetes 集群 (Azure) 交互。到目前为止一切顺利,一切都按预期进行,我已经达到了需要与 Pod 容器进行通信的程度。

让我们从一个简单的例子开始,只需创建一个新文件:

无论我到目前为止尝试过什么,以下命令都会返回 BadRequest:

string command_test = "touch servers_.json";
var stream_test = client.CoreV1.ConnectPostNamespacedPodExec(podname, space, command_test, container, true, true, false, false);

然后切换到 WebSocketNamespacedPodExecAsync 而不是 ConnectPostNamespacedPodExec (使用 vargs),然后我注意到以下内容(在本例中我尝试创建一个包含内容的文件):

//string[] command_test = { "touch servers_.json" }; //DOES NOT WORK
//string[] command_test = { "touch", "servers_.json" }; //DOES WORK
client.WebSocketNamespacedPodExecAsync(podname, space, command_test, container, true, true, false, false).Result;

在上面的示例中,当每个单词都用逗号和引号分隔时,触摸命令似乎起作用。

//string[] command_test = { "echo 'test' > servers_.json" }; //DOES NOT WORK
//string[] command_test = { "echo", "'test' > servers_.json" }; //DOES NOT WORK
//string[] command_test = { "echo", "'test'", ">", "servers_.json" }; //DOES NOT WORK
//var websocket_test = client.WebSocketNamespacedPodExecAsync(podname, space, command_test, container, true, true, false, false).Result;

但是使用 echo 的相同场景无论我尝试什么都不起作用,即使通过像触摸命令那样拆分单词也是如此。

有什么想法吗?

file kubernetes post containers exec
1个回答
0
投票

最后我找到了答案: 这是因为重定向运算符 (>) 由 shell 解释,而不是由 echo 命令解释。因此,您需要调用 shell 来执行带有重定向的命令。例如,您可以尝试以下操作:

string[] command_test = { "/bin/sh", "-c", "echo 'test' > servers_.json" };
© www.soinside.com 2019 - 2024. All rights reserved.