如何在aws elastic-beanstalk上自动重启节点应用程序

时间:2021-02-23 07:27:54

I have googled this question for a while but can't find the answer. My question is while deploying the nodejs application on aws elastic-beanstalk servers, I want the nodejs application would be restart automatically if the application crash.

我用谷歌搜索了这个问题一段时间但找不到答案。我的问题是在aws elastic-beanstalk服务器上部署nodejs应用程序时,我希望如果应用程序崩溃,nodejs应用程序将自动重启。

Actually there're few nodejs package already support this by command line, such as forever, but there's no easy way from console management or awscli to install this package and execute it to achieve restart automatically.

实际上很少有nodejs包已经通过命令行支持这个,例如永远,但是从控制台管理或awscli安装这个包并执行它以实现自动重启没有简单的方法。

I am wondering how do you resolve the restart issue on aws eb?

我想知道你如何解决aws eb上的重启问题?

5 个解决方案

#1


2  

Yes, better option to use Supervisor, however in order to have ability to restart app server with help of aws console or beanstalk cli tools you need to put own handler to Elastic beanstalk hooks in the directory: /opt/elasticbeanstalk/hooks/restartappserver/enact Hook is shell, python or ruby script that placed in mentioned directory. Put logic of the supervisord restart here and you will be able to restart it with help of management console, aws cli tools (http://docs.aws.amazon.com/cli/latest/reference/elasticbeanstalk/restart-app-server.html), elastic beanstalk api: (http://docs.aws.amazon.com/elasticbeanstalk/latest/APIReference/API_RestartAppServer.html)

是的,使用Supervisor的更好选择,但是为了能够在aws控制台或beanstalk cli工具的帮助下重启app服务器,你需要将自己的处理程序放到目录中的Elastic beanstalk挂钩:/ opt / elasticbeanstalk / hooks / restartappserver / enook Hook是放在提到的目录中的shell,python或ruby脚本。将supervisord的逻辑重新启动到此处,您将能够在管理控制台,aws cli工具的帮助下重新启动它(http://docs.aws.amazon.com/cli/latest/reference/elasticbeanstalk/restart-app-server .html),elastic beanstalk api:(http://docs.aws.amazon.com/elasticbeanstalk/latest/APIReference/API_RestartAppServer.html)

How to add hook, install supervisiord etc you can read here: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html

如何添加钩子,安装supervisiord等,你可以在这里阅读:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html

#2


29  

I've confirmed (as of Mar 11, 2015) that EB does indeed restart node for you.

我已经确认(截至2015年3月11日)EB确实为您重新启动了节点。

To confirm, I added a hidden query param to my app:

为了确认,我在我的应用中添加了一个隐藏的查询参数:

if (req.query.testcrash == 'true') {
    setTimeout(function(){
        undefined.crashMe();
    }, 100);
}

Then verified in the log that the exception happened, and that my app was restarted.

然后在日志中验证发生了异常,并且我的应用程序已重新启动。

For reference:

以供参考:

  • My EB/EC2 config is "64bit Amazon Linux 2014.09 v1.0.9 running Node.js"
  • 我的EB / EC2配置是“运行Node.js的64位Amazon Linux 2014.09 v1.0.9”
  • Using nginx and node 0.10.31
  • 使用nginx和节点0.10.31

#3


9  

Add forever to your package.json so it gets installed automatically. Then in EB console, under configuration, custom node command:

永远添加到您的package.json,以便自动安装。然后在EB控制台中,在配置下,自定义节点命令:

node_modules/.bin/forever app.js

#4


0  

If you want to restart the server from cron then you could use these commands.

如果要从cron重新启动服务器,则可以使用这些命令。

aws elasticbeanstalk restart-app-server --environment-name my-env

Reference

参考

#5


0  

After playing around with this a bit, and inspecting the process immediately after running

稍微玩了一下,并在运行后立即检查过程

aws elasticbeanstalk restart-app-server --environment-name my-env

from @Human Love 's comment. I found these two commands for manually starting/stopping the process when ssh'd into the EC2. Not sure if these are recommended, but for quick debugging I find them useful

来自@Human Love的评论。当ssh进入EC2时,我发现这两个命令用于手动启动/停止进程。不确定是否推荐这些,但为了快速调试,我觉得它们很有用

# to start the process
python /opt/elasticbeanstalk/containerfiles/ebnode.py --action start-all
# to stop the process
sudo python /opt/elasticbeanstalk/containerfiles/ebnode.py --action stop-all

[NOTE]: this is a nodejs specific solution. Though other application types are probably pretty similar. To inspect the exact command. Open two terminal windows and

[注意]:这是nodejs特定的解决方案。虽然其他应用程序类型可能非常相似。检查确切的命令。打开两个终端窗口

  1. in the first, run aws elasticbeanstalk restart-app-server --environment-name my-env
  2. 在第一个,运行aws elasticbeanstalk restart-app-server --environment-name my-env
  3. in the second, run ps aux | grep python (I grepped for node because it was a node app)
  4. 在第二个,运行ps aux | grep python(因为它是一个节点app,我为节点而grepped)

to find the specific /opt/elasticbeanstalk script

找到特定的/ opt / elasticbeanstalk脚本

#1


2  

Yes, better option to use Supervisor, however in order to have ability to restart app server with help of aws console or beanstalk cli tools you need to put own handler to Elastic beanstalk hooks in the directory: /opt/elasticbeanstalk/hooks/restartappserver/enact Hook is shell, python or ruby script that placed in mentioned directory. Put logic of the supervisord restart here and you will be able to restart it with help of management console, aws cli tools (http://docs.aws.amazon.com/cli/latest/reference/elasticbeanstalk/restart-app-server.html), elastic beanstalk api: (http://docs.aws.amazon.com/elasticbeanstalk/latest/APIReference/API_RestartAppServer.html)

是的,使用Supervisor的更好选择,但是为了能够在aws控制台或beanstalk cli工具的帮助下重启app服务器,你需要将自己的处理程序放到目录中的Elastic beanstalk挂钩:/ opt / elasticbeanstalk / hooks / restartappserver / enook Hook是放在提到的目录中的shell,python或ruby脚本。将supervisord的逻辑重新启动到此处,您将能够在管理控制台,aws cli工具的帮助下重新启动它(http://docs.aws.amazon.com/cli/latest/reference/elasticbeanstalk/restart-app-server .html),elastic beanstalk api:(http://docs.aws.amazon.com/elasticbeanstalk/latest/APIReference/API_RestartAppServer.html)

How to add hook, install supervisiord etc you can read here: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html

如何添加钩子,安装supervisiord等,你可以在这里阅读:http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html

#2


29  

I've confirmed (as of Mar 11, 2015) that EB does indeed restart node for you.

我已经确认(截至2015年3月11日)EB确实为您重新启动了节点。

To confirm, I added a hidden query param to my app:

为了确认,我在我的应用中添加了一个隐藏的查询参数:

if (req.query.testcrash == 'true') {
    setTimeout(function(){
        undefined.crashMe();
    }, 100);
}

Then verified in the log that the exception happened, and that my app was restarted.

然后在日志中验证发生了异常,并且我的应用程序已重新启动。

For reference:

以供参考:

  • My EB/EC2 config is "64bit Amazon Linux 2014.09 v1.0.9 running Node.js"
  • 我的EB / EC2配置是“运行Node.js的64位Amazon Linux 2014.09 v1.0.9”
  • Using nginx and node 0.10.31
  • 使用nginx和节点0.10.31

#3


9  

Add forever to your package.json so it gets installed automatically. Then in EB console, under configuration, custom node command:

永远添加到您的package.json,以便自动安装。然后在EB控制台中,在配置下,自定义节点命令:

node_modules/.bin/forever app.js

#4


0  

If you want to restart the server from cron then you could use these commands.

如果要从cron重新启动服务器,则可以使用这些命令。

aws elasticbeanstalk restart-app-server --environment-name my-env

Reference

参考

#5


0  

After playing around with this a bit, and inspecting the process immediately after running

稍微玩了一下,并在运行后立即检查过程

aws elasticbeanstalk restart-app-server --environment-name my-env

from @Human Love 's comment. I found these two commands for manually starting/stopping the process when ssh'd into the EC2. Not sure if these are recommended, but for quick debugging I find them useful

来自@Human Love的评论。当ssh进入EC2时,我发现这两个命令用于手动启动/停止进程。不确定是否推荐这些,但为了快速调试,我觉得它们很有用

# to start the process
python /opt/elasticbeanstalk/containerfiles/ebnode.py --action start-all
# to stop the process
sudo python /opt/elasticbeanstalk/containerfiles/ebnode.py --action stop-all

[NOTE]: this is a nodejs specific solution. Though other application types are probably pretty similar. To inspect the exact command. Open two terminal windows and

[注意]:这是nodejs特定的解决方案。虽然其他应用程序类型可能非常相似。检查确切的命令。打开两个终端窗口

  1. in the first, run aws elasticbeanstalk restart-app-server --environment-name my-env
  2. 在第一个,运行aws elasticbeanstalk restart-app-server --environment-name my-env
  3. in the second, run ps aux | grep python (I grepped for node because it was a node app)
  4. 在第二个,运行ps aux | grep python(因为它是一个节点app,我为节点而grepped)

to find the specific /opt/elasticbeanstalk script

找到特定的/ opt / elasticbeanstalk脚本