如何让Kubernetespod运行本地脚本

我想在 Kubernetes pod 中运行一个本地脚本,然后将输出结果设置为一个 linux 变量

这是我尝试过的:

# if I directly run -c "netstat -pnt |grep ssh", I get output assigned to $result:

cat check_tcp_conn.sh 
#!/bin/bash
result=$(kubectl exec -ti <pod_name> -- /bin/bash -c "netstat -pnt |grep ssh")
echo "result is $result"

我想要的是这样的:

#script to be called:
cat netstat_tcp_conn.sh
#!/bin/bash
netstat -pnt |grep ssh

#script to call netstat_tcp_conn.sh:
cat check_tcp_conn.sh 
#!/bin/bash
result=$(kubectl exec -ti <pod_name> -- 
/bin/bash -c "./netstat_tcp_conn.sh)
echo "result is $result

结果显示result is /bin/bash: ./netstat_tcp_conn.sh: No such file or directory

如何让 Kubernetes pod 执行我本地机器上的 netstat_tcp_conn.sh?

回答

您可以使用以下命令在 pod 中执行脚本:

kubectl exec POD -- /bin/sh -c "`cat netstat_tcp_conn.sh`"

  • @heheh it doesn't depend on the contents of the pod at all. That `cat netstat_tcp_conn.sh` will be evaluated by your shell on client-side, before running `kubectl exec`.

以上是如何让Kubernetespod运行本地脚本的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>