如何让 Kubernetes pod 运行本地脚本

use*_*955 1 bash kubernetes

我想在 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"
Run Code Online (Sandbox Code Playgroud)

我想要的是这样的:

#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
Run Code Online (Sandbox Code Playgroud)

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

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

Ali*_*Tou 5

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

kubectl exec POD -- /bin/sh -c "`cat netstat_tcp_conn.sh`"
Run Code Online (Sandbox Code Playgroud)

  • @呵呵,它根本不依赖于 Pod 的内容。在运行“kubectl exec”之前,客户端的 shell 将评估“cat netstat_tcp_conn.sh”。 (2认同)