如何在群集模式下使用pm2启动node.js应用程序?

时间:2022-10-15 23:26:12

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.

我们正试图在ubuntu 14.04上使用octa core proccessor以pm2 0.12.8开始我们的应用程序。 git hub上的read me有一个非常直接的命令,用于在集群模式下运行节点应用程序。

# 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.

但上述命令对我们不起作用。当我们尝试运行这些命令时,pm2只列出了一个实例。

Why? Any suggestion

为什么?任何建议

Thanks

谢谢

2 个解决方案

#1


32  

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?

什么“pm2 monit”告诉你什么?

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

如果你在pm2停止一个进程,即使它没有运行,它仍然为它保留一个cpu。你应该总是使用pm2删除

#2


4  

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

由于您希望使用流程文件来管理pm2,因此流程文件应类似于以下内容:

    // 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

#1


32  

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?

什么“pm2 monit”告诉你什么?

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

如果你在pm2停止一个进程,即使它没有运行,它仍然为它保留一个cpu。你应该总是使用pm2删除

#2


4  

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

由于您希望使用流程文件来管理pm2,因此流程文件应类似于以下内容:

    // 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