kubectl:在多个多容器的所有容器中执行命令pods
kubectl: execute command in all containers of multiple multi-container pods
我有以下 pods,
root@sea:scripts# kubectl get pods -l app=mubu7 -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mubu71 2/2 Running 0 51m 10.244.1.215 spring <none> <none>
mubu72 2/2 Running 0 51m 10.244.2.17 island <none> <none>
并且每个都有 2 个容器:
root@sea:scripts# kubectl get pods mubu71 -o jsonpath='{.spec.containers[*].name}' | \
> tr " " "\n" | uniq
mubu711
mubu712
root@sea:scripts# kubectl get pods mubu72 -o jsonpath='{.spec.containers[*].name}' | \
> tr " " "\n" | uniq
mubu721
mubu722
我想向pods及其所有 (4) 个容器传递一个命令,但我能想出的最好办法是
kubectl get pods \
-l app=mubu7 \
-o jsonpath='{.items[*].metadata.name}' | \
tr " " "\n" | uniq | \
xargs -I{} kubectl exec {} -- bash -c \
"service ssh start"
输出
Defaulted container "mubu711" out of: mubu711, mubu712
* Starting OpenBSD Secure Shell server sshd
...done.
Defaulted container "mubu721" out of: mubu721, mubu722
* Starting OpenBSD Secure Shell server sshd
...done.
如何修改以上代码 运行 所有 pod 容器中的命令,而不仅仅是第一个 或者是否有更好的替代方法?
任何帮助将不胜感激。
你可以试试tmux-exec?
有解释here.
对于 Linux 用户:从 Github Release.
下载最新的 .tar.gz
解压 .tar.gz 并将 bin/kubectl-tmux_exec 移动到您的 bin 目录之一,例如 /usr/local/bin。
运行 kubectl tmux-exec --help .
以下代码将遍历所有带有标签 app=mubu7
的 pods。稍后它会获取它们的 pod 名称和所有容器的名称。容器名称列表被转换成一个数组,并为每个 pod 迭代。因此,无论 pod 中有多少个容器(例如:1、2、3..等),这段代码都可以工作。
#JSON-path expression to extract the pod name and the list of containers
#{range .items[*]}{.metadata.name} {.spec.containers[*].name}{"\n"} {end}
#note that the pod name is stored in 1st variable(pod)
# container(s) name is stored in 2nd variable(containers)
kubectl get pods -l app=mubu7 -o jsonpath='{range .items[*]}{.metadata.name} {.spec.containers[*].name}{"\n"} {end}' |while read -r pod containers; do
# 2nd variable consist of space seprated container lists, converting it to array
array=($containers);
#inner loop to iterate over containers for each pod(outer loop)
for container in "${array[@]}";do
kubectl exec -i ${pod} -c "$container" </dev/null -- service ssh start
done
done
我有以下 pods,
root@sea:scripts# kubectl get pods -l app=mubu7 -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mubu71 2/2 Running 0 51m 10.244.1.215 spring <none> <none>
mubu72 2/2 Running 0 51m 10.244.2.17 island <none> <none>
并且每个都有 2 个容器:
root@sea:scripts# kubectl get pods mubu71 -o jsonpath='{.spec.containers[*].name}' | \
> tr " " "\n" | uniq
mubu711
mubu712
root@sea:scripts# kubectl get pods mubu72 -o jsonpath='{.spec.containers[*].name}' | \
> tr " " "\n" | uniq
mubu721
mubu722
我想向pods及其所有 (4) 个容器传递一个命令,但我能想出的最好办法是
kubectl get pods \
-l app=mubu7 \
-o jsonpath='{.items[*].metadata.name}' | \
tr " " "\n" | uniq | \
xargs -I{} kubectl exec {} -- bash -c \
"service ssh start"
输出
Defaulted container "mubu711" out of: mubu711, mubu712
* Starting OpenBSD Secure Shell server sshd
...done.
Defaulted container "mubu721" out of: mubu721, mubu722
* Starting OpenBSD Secure Shell server sshd
...done.
如何修改以上代码 运行 所有 pod 容器中的命令,而不仅仅是第一个 或者是否有更好的替代方法?
任何帮助将不胜感激。
你可以试试tmux-exec? 有解释here.
对于 Linux 用户:从 Github Release.
下载最新的 .tar.gz解压 .tar.gz 并将 bin/kubectl-tmux_exec 移动到您的 bin 目录之一,例如 /usr/local/bin。
运行 kubectl tmux-exec --help .
以下代码将遍历所有带有标签 app=mubu7
的 pods。稍后它会获取它们的 pod 名称和所有容器的名称。容器名称列表被转换成一个数组,并为每个 pod 迭代。因此,无论 pod 中有多少个容器(例如:1、2、3..等),这段代码都可以工作。
#JSON-path expression to extract the pod name and the list of containers
#{range .items[*]}{.metadata.name} {.spec.containers[*].name}{"\n"} {end}
#note that the pod name is stored in 1st variable(pod)
# container(s) name is stored in 2nd variable(containers)
kubectl get pods -l app=mubu7 -o jsonpath='{range .items[*]}{.metadata.name} {.spec.containers[*].name}{"\n"} {end}' |while read -r pod containers; do
# 2nd variable consist of space seprated container lists, converting it to array
array=($containers);
#inner loop to iterate over containers for each pod(outer loop)
for container in "${array[@]}";do
kubectl exec -i ${pod} -c "$container" </dev/null -- service ssh start
done
done