Shuls
Shuls

Reputation: 65

How to achieve rolling update with pm2?

I want to achieve zero downtime deployment using pm2, i've read lots of articles, guides, github issues and they all kinda point to the same approach, which is to use the clustering approach like this

pm2 start app.js -i 2 <number of instances>

Well, that works, it starts 2 process of the same app, thing is, my express app runs on PORT 3000 by default, and as i undestand, this cluster is supposed to work like docker, so i shouldn't worry about the port 3000 being used, but i'm getting that error.

It's that supposed to happen? How can i achieve zero downtime deployment with this approach? It's not clear to me if the port should be occupied or if i'm doing something wrong.

I've tried everything in this guide https://medium.com/learnwithrahul/zero-downtime-deployments-with-pm2-93013713df15 and related ones.

This is my pm2 config file (ecosystem.config.js)

module.exports = {
  apps : [{
    name   : "enterprise",
    script : "yarn",
    instances: "2",
    exec_mode: "cluster",
    error_file: "./error-log",
    cwd: "project/packages/backend",
    args: "start:stag"
  }]
}

Also i've tried wait-ready param, but it's not my priority right now.

Upvotes: 0

Views: 414

Answers (1)

Mutu A.
Mutu A.

Reputation: 268

Not entirely sure if this will help your specific scenario, but maybe can help someone else.

I had the same issue with a NextJS app, when I was trying to run it in cluster mode, the 2nd instance of the app was failing because the port 3000 was already in use.

This was caused because I had in the script property nx next start my-app, after changing it to script: 'node_modules/next/dist/bin/next' PM2 was able to run it in cluster mode correctly.

Make sure that you have the node_modules folder inside your cwd folder.

Also, for zero downtime deployment it is better to use startOrReload path/to/ecosystem.config.js_file

Upvotes: 0

Related Questions