日常总有些场景需要在多个 Pod 中并行查看一些问题和执行些脚本,因此搞了一个 kubectl 插件 https://github.com/major1201/kubectl-mexec/ 来并行执行命令或本地脚本。希望大家多多 Star 支持~,效果如下:
# 并行执行命令
kubectl mexec -l app.kubernetes.io/name=foo -p 3 -- bash -c "ip a | grep eth0"
footest-2 456: eth0@if142: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1430 qdisc noqueue state UP group default
footest-2 inet 172.16.1.101/32 scope global eth0
footest-0 460: eth0@if211: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1430 qdisc noqueue state UP group default
footest-0 inet 172.16.1.103/32 scope global eth0
footest-1 458: eth0@if434: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1430 qdisc noqueue state UP group default
footest-1 inet 172.16.1.102/32 scope global eth0
# 并行执行本地脚本
kubectl mexec -l app.kubernetes.io/name=foo -p 3 -F ./test.sh
# 批量上传文件
kubectl mexec upload -l app.kubernetes.io/name=myapp --local-file ./local-file --remote-path /tmp/local-file
# 批量下载文件
kubectl mexec download -l app.kubernetes.io/name=myapp --remote-path /tmp/local-file
推荐 krew 安装
kubectl krew update
kubectl krew install mexec