AWS Elastic Beanstalk的ebextensions配置在EC2扩容新实例时不生效如何解决
问题原因
你当前配置仅在手动触发应用部署时执行container_commands,新扩容的EC2实例虽然会拉取相同的应用包,但存在三个核心问题导致配置不生效:
- 未配置supervisor、beanstalkd的开机自启规则,实例启动后服务不会自动运行
setup.sh中使用相对路径读取配置文件,新实例初始化阶段执行脚本时路径不匹配- 未给
setup.sh配置执行权限,部分场景下脚本会因权限不足执行失败
修改方案
1. 调整supervisor.config配置
新增systemd服务注册规则、脚本权限配置,修改后内容如下:
files: /etc/systemd/system/supervisord.service: mode: "000644" owner: root group: root content: | [Unit] Description=Supervisor daemon After=network.target [Service] Type=forking ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf ExecStop=/usr/bin/supervisorctl shutdown ExecReload=/usr/bin/supervisorctl reload KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target container_commands: 00_chmod_setup: command: "chmod +x .ebextensions/supervisor/setup.sh" 01_install_supervisor: command: ".ebextensions/supervisor/setup.sh"
2. 调整setup.sh脚本
修正路径问题,新增服务开机自启配置,修改后内容如下:
#!/bin/bash # 先切换到EB部署阶段的应用临时目录,解决相对路径问题 cd /var/app/staging || exit 1 echo "Supervisor - starting setup" . /opt/elasticbeanstalk/deployment/env if [ ! -f /usr/bin/supervisord ]; then echo "installing supervisor" easy_install supervisor else echo "supervisor already installed" fi if [ ! -d /etc/supervisor ]; then mkdir /etc/supervisor echo "create supervisor directory" fi if [ ! -d /etc/supervisor/conf.d ]; then mkdir /etc/supervisor/conf.d echo "create supervisor configs directory" fi cat .ebextensions/supervisor/supervisord.conf > /etc/supervisor/supervisord.conf cat .ebextensions/supervisor/supervisord.conf > /etc/supervisord.conf cat .ebextensions/supervisor/supervisor_laravel.conf > /etc/supervisor/conf.d/supervisor_laravel.conf # 重载systemd配置,识别新增的supervisord服务 systemctl daemon-reload if systemctl is-active --quiet supervisord; then echo "supervisor is running, reloading config" supervisorctl reread supervisorctl update else echo "starting supervisor" systemctl start supervisord fi # 配置supervisor开机自启 systemctl enable supervisord echo "Supervisor Running!" yum -y install http://cbs.centos.org/kojifiles/packages/beanstalkd/1.9/3.el7/x86_64/beanstalkd-1.9-3.el7.x86_64.rpm if systemctl is-active --quiet beanstalkd; then echo "beanstalkd is running" else echo "starting beanstalkd" systemctl start beanstalkd.service fi # 配置beanstalkd开机自启 systemctl enable beanstalkd echo "Beanstalkd Running..."
生效方法
将修改后的配置提交到代码仓库,重新打包部署到Elastic Beanstalk即可。后续新扩容的实例会自动执行全部配置,无需手动操作。
内容的提问来源于stack exchange,提问作者fakhruddin tahery
相关产品推荐
相关产品推荐

