powershell管道在kubectl exec中逸出

ufj5ltwl  于 2023-03-02  发布在  Shell
关注(0)|答案(2)|浏览(141)

如何在powershell上通过kubectl exec发送一个简单的管道?单个命令和参数工作正常。
在rancher内部的linux和kubectl shell中运行良好:

$> kubectl exec test12-7b7fd6b4f4-wzp59 --namespace=test12 -- dmesg | tail -1
[562119.054019] oom_reaper: reaped process 2410 (supervisorctl), now anon-rss:0kB, file-rss:0kB, shmem-rss:0kB

kubectl exec需要取消引用-- dmesg | tail -1,但是powershell捕获了它:

PS> kubectl exec test12-7b7fd6b4f4-wzp59 --namespace=test12 -- dmesg | tail -1
tail : The term 'tail' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:168
+ ... namespace=test12 -- dmesg | tail -1
+                                                                  ~~~~
    + CategoryInfo          : ObjectNotFound: (tail:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

如果我引用库贝特尔的话,他抱怨说:

PS> kubectl exec test12-7b7fd6b4f4-wzp59 --namespace=test12 -- "dmesg | tail -1"
OCI runtime exec failed: exec failed: unable to start container process: exec: "dmesg | tail -1": executable file not found in $PATH: unknown
command terminated with exit code 126

命令和参数的作用:

PS> kubectl exec test12-7b7fd6b4f4-wzp59 --namespace=test12 -- ls -l /var/log/lastlog 
-rw-rw-r-- 1 root utmp 2920292 Jan 27 09:56 /var/log/lastlog

我发现了这个链接,并尝试了所有这些,甚至停止解析--%,但没有工作:https://www.octopus.com/blog/powershell-pipe-escaping
一个简单的解决方法是在构建容器时在容器中创建别名:alias ll='dmesg|tail -1'
并将其附加到~./bashrc

efzxgjgh

efzxgjgh1#

感谢@jeroen Mostert的评论,我们可以在powershell中过滤所有的输出,这不是一个完美的解决方案,因为它会将所有的输出拖回powershell,而不仅仅是在容器中将其剪辑掉:

ps> kubectl exec test1-6974dc7b79-zf5r5 --namespace=test1 -- dmesg | select -last 1
[578268.156685] oom_reaper: reaped process 20236 (supervisorctl), now anon-rss:0kB, file-rss:0kB, shmem-rss:0kB

我也不能在linux和powershell中使用相同的命令。如果有人有更聪明的方法,那就太好了:-)

roejwanj

roejwanj2#

如果将pipe封装到sh命令中,它就可以工作:

ps> kubectl exec id-pro-trk-trk-user-portal-web-production-747b88964c-rqt75 \
 --namespace=id-pro-trk-trk-user-portal-web-production \
 -- sh -c 'wget -O- http://localhost:8082/user-portal/bp-internal/info | grep version'

输出:

Connecting to localhost:8082 (127.0.0.1:8082)
writing to stdout
-                    100% |********************************|   818  0:00:00 ETA
written to stdout
  "version" : "1.10.247",

相关问题