

















Hi there,
Good news, PM2 actually handles this natively. You do not need Kubernetes or anything complex for a single Droplet setup.
The simplest fix is using PM2’s reload instead of restart:
pm2 reload app-name
Unlike pm2 restart which kills the process and starts a new one, pm2 reload does a rolling reload. It starts a new instance of your app, waits for it to be ready, then gracefully shuts down the old one. No downtime.
For this to work properly your app needs to handle the SIGINT signal cleanly and stop accepting new connections before exiting:
process.on('SIGINT', () => {
server.close(() => {
process.exit(0);
});
});
If you want to go a step further, PM2 has a built in cluster mode that runs multiple instances of your app and reloads them one by one:
// ecosystem.config.js
module.exports = {
apps: [{
name: 'app-name',
script: 'server.js',
instances: 'max',
exec_mode: 'cluster'
}]
}
Then deploy with:
pm2 reload ecosystem.config.js
Nginx keeps serving requests to the instances that are still up while each one reloads, so users never hit a dead process.
A couple of things to keep in mind:
Make sure your app is stateless or that sessions are stored externally (Redis, database) before enabling cluster mode. In-memory sessions will not work across multiple instances.
If you are doing database migrations as part of your deploy, run those before reloading PM2, not during.
The PM2 docs have a good section on this if you want to go deeper: https://pm2.keymetrics.io/docs/usage/cluster-mode/
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。