如何在 AWS EKS 中一次在多个容器上执行 shell 脚本

How to execute shell scripts on multiple containers at once in AWS EKS

我想在 AWS EKS 上部署一个 Laravel 应用程序。

我的应用程序使用 Laravel 作业和队列。我们可以使用 Laravel 附带的 artisan 实用程序来管理 Queue Worker。

php artisan queue:work
php artisan queue:restart

我将使用 Supervisord 来监控队列进程。

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/forge/app.com/artisan queue:work sqs --sleep=3 --tries=3
autostart=true
autorestart=true
user=forge
numprocs=8
redirect_stderr=true
stdout_logfile=/home/forge/app.com/worker.log

为了在部署容器时启动队列,我使用了 Dockerfile 中的 ENTRYPOINT 脚本。

#!/usr/bin/env bash

##
# Ensure /.composer exists and is writable
#
if [ ! -d /.composer ]; then
    mkdir /.composer
fi

chmod -R ugo+rw /.composer

##
# Run a command or start supervisord
#
if [ $# -gt 0 ];then
    # If we passed a command, run it
    exec "$@"
else
    # Otherwise start supervisord
    /usr/bin/supervisord
fi

我无法理解的是,如果我的应用程序 运行ning 有多个副本,我将如何远程停止和启动 运行ning 容器上的队列进程。

在 EC2 上,我可以使用 AWS SSM 同时在多个实例上执行 运行 shell 命令。

AWS EKS 是否也有类似的东西?

或者一般来说,您如何在 AWS EKS 的多个容器上管理 运行ning 队列进程?

一般来说,如果你想一次在多个容器中执行一个命令,你可以这样做,例如:

for pod in $(kubectl get pods -o jsonpath='{.items[*].metadata.name}' -l app=myapp); do
  kubectl exec "$pod" mycommand
done

这会在带有 app=myapp 标签的所有 Pods 的第一个容器中执行 mycommand。您的集群是在 EKS 上运行还是在其他任何地方运行都没有关系。