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

2020-05-23 08:10发布

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

5条回答
一纸荒年 Trace。
2楼-- · 2020-05-23 08:52

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]
查看更多
再贱就再见
3楼-- · 2020-05-23 08:54

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
查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-05-23 08:54

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
查看更多
太酷不给撩
5楼-- · 2020-05-23 08:57

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

查看更多
叼着烟拽天下
6楼-- · 2020-05-23 09:00

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.

查看更多
登录 后发表回答