我有一个用python2.7编写的服务,由supervisord在Ubuntu16.04 EC2 spot实例上管理。
在系统启动时,我有许多 system d任务,这些任务需要在监督启动服务之前完成。
当实例即将关闭时,我需要监督and 来捕获事件,并告诉服务优雅地停止。服务将需要停止处理,并在正常退出之前将任何工作负载返回到队列。
发布于 2016-07-28 15:50:35
首先,我们需要安装一个systemd任务,我们希望在监控器启动之前运行它。让我们创建一个脚本/usr/bin/pre-supervisor.sh,它将处理为我们工作的执行过程,并创建/lib/systemd/system/pre-supervisor.service for systemd.。
[Unit]
Description=Task to run prior to supervisor Starting up
After=cloud-init.service
Before=supervisor.service
Requires=cloud-init.service
[Service]
Type=oneshot
WorkingDirectory=/usr/bin
ExecStart=/usr/bin/pre-supervisor.sh
RemainAfterExit=no
TimeoutSec=90
User=ubuntu
# Output needs to appear in instance console output
StandardOutput=journal+console
[Install]
WantedBy=multi-user.target如您所见,这将在ec2云-init.service完成之后和supervisor.service之前运行。
接下来,让我们修改/lib/systemd/system/supervisor.service,使其在pres-Superor.service完成之后运行,而不是在network.target之后运行。
[Unit]
Description=Supervisor process control system for UNIX
Documentation=http://supervisord.org
After=pre-supervisor.service
[Service]
ExecStart=/usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl -c /etc/supervisor/supervisord.conf $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=50s
[Install]
WantedBy=multi-user.target这将确保我们的前主管任务在主管启动之前运行。
由于这些是spot实例,AWS已经在元数据url中公开了终止通知,因此我只需要注入以下内容:
if requests.get("http://169.254.169.254/latest/meta-data/spot/termination-time").status_code == 200在我的python服务中,让它每隔5秒钟左右检查一次,并在终止通知出现时优雅地关闭。
https://stackoverflow.com/questions/38620864
复制相似问题