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

5

23

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

Misfeasor answered 20/3, 2015 at 10:0 Comment(0)
A
73

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

Androsphinx answered 20/3, 2015 at 10:16 Comment(5)
like pm2 start app.js -- test -i 2Misfeasor
use a JSON App declaration to pass parameters github.com/Unitech/PM2/blob/master/ADVANCED_README.md#a10Androsphinx
The delete all is key. PM2 will remember the last time you started the app in fork mode so unless you delete it it will restart as fork.Neuroblast
pm2 start app.js -i max = Start with maximum instanceTaster
@HolgerWill pm2 monit for me displays blank screen regardless of what i attempt. any clues?Laurinda
E
12

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
Ethnomusicology answered 22/9, 2016 at 5:15 Comment(0)
E
8

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
Eire answered 3/8, 2019 at 7:25 Comment(1)
In my case using the --name flag forces pm2 to use cluster modeStoa
N
1

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]
Neela answered 20/5, 2019 at 3:34 Comment(0)
T
1

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.

Tso answered 16/11, 2019 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.