简体   繁体   English

如何在Node.js中实现零停机时间重新部署

[英]How to achieve zero downtime redeployment in Node.js

What is the easiest way to achieve zero downtime for my Node.js application? 实现Node.js应用程序零停机的最简单方法是什么?

I have an app that requires the following steps for redeployment: 我有一个需要重新部署以下步骤的应用程序:

  • npm install
  • node_modules/.bin/bower install
  • node_modules/.bin/gulp

The result of these operations is the ready-to-run application in the generated by the gulpfile.js directory named build . 这些操作的结果是在名为buildgulpfile.js目录生成的随时可以运行的应用程序。 In this directory I have a currently-running instance of the same application (currently launched via forever like this -- forever start server.js ). 在这个目录中我也有同样的应用程序的当前运行的实例(目前通过推出永远这样的- forever start server.js )。

As far as I know, it is not possible to achieve zero downtime via forever module, so I decided to choose another way to do it. 据我所知,不可能通过forever模块实现零停机时间,因此我决定选择另一种方式来实现。

I saw pm2 but I found it very complex tbh (prove me wrong if you don't feel the same). 我看到了pm2,但是我发现它非常复杂(如果您感觉不一样,请向我证明错误)。

I also saw naught but I can't even start my application via naught start server.js -- it doesn't even print anything in stdout / stderr. 我也看到一无所获,但我什至无法通过naught start server.js启动我的应用程序-它甚至无法在stdout / stderr中打印任何内容。

I also saw up-time but I didn't get the idea -- how will it handle situation when I run gulp that should replace files in the directory where currently-running instance work at the moment? 我也看到了正常运行时间,但是我不知道这个想法-当我运行gulp应该替换当前正在运行的实例当前工作的目录中的文件时,它将如何处理情况?

Regarding of handling replaced files during build: if these files is used by Node.js app then all changes will be applied upon process restart (since these files are loaded into memory), browser frontend files could also be cached in application memory to achieve similar behavior (changes applied only upon restart or/and cache invalidation). 关于在构建过程中处理替换文件:如果这些文件由Node.js应用程序使用,则所有更改将在进程重新启动时应用(因为这些文件已加载到内存中),浏览器前端文件也可以缓存在应用程序内存中以实现类似的目的行为(仅在重新启动或/和缓存失效后才应用更改)。

We're using pm2 in cluster mode. 我们在群集模式下使用pm2。

pm2 start app.js -i

The above command starts app.js in cluster mode on all available CPU cores. 上面的命令在所有可用CPU内核上以集群模式启动app.js

zero downtime restart: 零停机重启:

pm2 gracefulReload all

this command restarts all processes sequentially, so if you have more than one process up and running there is always at least one process that servers requests during restart. 此命令将按顺序重新启动所有进程,因此,如果启动并运行了多个进程,则始终有至少一个服务器在重新启动期间请求该进程。 If you have only one process of app.js you can start it in cluster mode and run pm2 scale app.js 2 (starts one more process) then pm2 gracefulReload all and then pm2 scale app.js 1 (removes previously started process). 如果只有一个进程app.js ,则可以在集群模式下启动它,然后运行pm2 scale app.js 2 (再启动一个进程),然后运行pm2 gracefulReload all ,然后再运行pm2 scale app.js 1 (删除先前启动的进程)。

Though I think app restarting is not main problem of zero downtime deployment, we've not managed to handle DB migrations, so full app shutdown is needed to apply DB changes. 尽管我认为重新启动应用程序不是零停机时间部署的主要问题,但我们尚未设法处理数据库迁移,因此需要完全关闭应用程序才能应用数据库更改。 Also there could be an issue with browser frontend files when during deploy user obtained the new version of them, but AJAX request is processed by old version of server process, in this case sticky sessions and API versioning came to the rescue. 当在部署期间用户获得新版本的浏览器前端文件时,浏览器前端文件也可能存在问题,但是AJAX请求由服务器进程的旧版本处理,在这种情况下,粘性会话和API版本得以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM