Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start node.js app with pm2 in cluster mode?

We are trying to start our app with pm2 0.12.8 on ubuntu 14.04 with octa core proccessor. The read me on the git hub has a very straight forward command for running node app in cluster mode.

# Cluster mode

$ pm2 start app.js -i 0        **# Will start maximum processes with LB depending on available CPUs**
$ pm2 start app.js -i max      **# Same as above, but deprecated yet.**

But the above command are not working for us. When we try to run these commands only one instance is listed by pm2.

Why? Any suggestion

Thanks

like image 571
Hitu Bansal Avatar asked Mar 20 '15 10:03

Hitu Bansal


People also ask

How do I start a pm2 cluster?

PM2 Cluster Module You can create your application and hand off the clustering to PM2. This command will start two processes of app. js . Using the -i <number-of-instances> option tells PM2 to start the process in cluster mode instead of fork mode.

Does pm2 use cluster?

exec_mode: "cluster" tells PM2 to use the Node. js cluster module to execute the code. instances:0 when this is set to 0, PM2 will automatically spawn a number of child processes that are equal to the available number of cores. Respawning on termination is handled out of the box.

What is cluster mode pm2?

The cluster mode allows networked Node. js applications (http(s)/tcp/udp server) to be scaled across all CPUs available, without any code modifications. This greatly increases the performance and reliability of your applications, depending on the number of CPUs available.


2 Answers

Since you are looking to use a process file to manage your pm2, the process file should look similar to this:

    // ecosystem.js
    {
    "apps" : [{
      "name"      : "API",
      "script"    : "server.js",// name of the startup file
      "instances" : 4,          // number of workers you want to run
      "exec_mode" : "cluster",  // to turn on cluster mode; defaults to 'fork' mode 
      "env": {
        "PORT"      : "9090" // the port on which the app should listen
      }
      // for more options refer : http://pm2.keymetrics.io/docs/usage/application-declaration/#process-file
    }]
    }

Run this app using the following command to start and stop respectively:

$ pm2 start ecosystem.js
$ pm2 stop ecosystem.js
like image 37
riddhi_agrawal Avatar answered Oct 23 '22 05:10

riddhi_agrawal


have you tried starting a fixed number of processes? i.e.

pm2 start app.js -i 2 //should start two instances.

what does "pm2 monit" show you?

also try

pm2 stop all
pm2 delete all 

and then

pm2 start app.js -i 0

if you stop a process in pm2 it still reserves one cpu for it even if its not running. you should allways use pm2 delete

like image 82
Holger Will Avatar answered Oct 23 '22 04:10

Holger Will