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

29,408

Solution 1

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

Solution 2

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

Solution 3

For fresh process

pm2 start app.js --name "my-node-app" -i 2 // to create 2 process

To make existing running process. You have to stop and delete the current running process, if it was fork mode. Then only it can create cluster mode.

pm2 stop my-node-app
pm2 delete my-node-app
pm2 start app.js --name "my-node-app" -i 2 // to create 2 process

Solution 4

I think you might ever start this project with normal mode (fork_mode), so you should delete all process list before change to cluster mode, since pm2 will memorise ur start options

pm2 delete all
pm2 start app.js -i [NUMBER_OF_INSTANCE|max]

Solution 5

You can get the best information here : pm2 cluster mode

To enable the cluster mode, just pass the -i option:

pm2 start app.js -i max

max means that PM2 will auto detect the number of available CPUs and run as many processes as possible

Or via a js/yaml/json file:

module.exports = {
  apps : [{
    script    : "api.js",
    instances : "max",
    exec_mode : "cluster"
  }]
}

NOTE: you need to set the exec_mode to cluster so PM2 know you want to load balance between each instances, by default it will not

Then to start the Process File:

pm2 start processes.json The -i or instances option can be:

0/max to spread the app across all CPUs

-1 to spread the app across all CPUs - 1

number to spread the app across number CPUs.

Share:
29,408
Hitu Bansal
Author by

Hitu Bansal

Experienced mobile / web developer .

Updated on November 17, 2020

Comments

  • Hitu Bansal
    Hitu Bansal over 3 years

    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