doc-pm2 icon indicating copy to clipboard operation
doc-pm2 copied to clipboard

Load-Balancing | Guide | PM2 Documentation

Open rmonnier9 opened this issue 6 years ago • 19 comments

https://pm2.io/doc/en/runtime/guide/load-balancing/

PM2 - Guide, References, Tutorials and Best Practices about PM2 and Node.js.

rmonnier9 avatar May 30 '18 14:05 rmonnier9

nice work!

falstack avatar Aug 01 '18 03:08 falstack

powerful and great for development thanks

sadeghmohebbi avatar Aug 07 '18 15:08 sadeghmohebbi

Hi, I am new to pm2 and would like to know two things:

  1. Would it be possible to deploy to a machine from local folder/zip/tar instead of a git repository?
  2. If my deploy has two hosts (I am deploying to two different machines), is there any load balancer in pm2 which can handle this or should I use an external load balancer like nginx or haproxy to balance load across machines?

Thanks in advance

Vamsee147 avatar Sep 17 '18 13:09 Vamsee147

@Vamsee147

  1. Not currently.
  2. You need to either use another load balancer or do a DNS round robin approach.

vmarchaud avatar Sep 25 '18 11:09 vmarchaud

While load balancing with pm2 does not require additional installation, configuration, etc. nginx has its own advantages. It is not clear to me exactly what are the pros and cons between pm2 and nginx as load balancing options.

Also, I need to have multiple node instances running on different ports running behind nginx (refer this). That does not seem to be with the case for this clustered load balancing. How does this work out?

chainhead avatar Sep 30 '18 02:09 chainhead

@chainhead It depend of your needs, but mostly the difference is that cluster mode is just simpler to setup. You don't need to take care to which port the applications listen etc.

But the simplicity as a cost, you can only do round robin distribution (so things like socket.io will not work against a cluster since it need a iphash distribution).

vmarchaud avatar Sep 30 '18 07:09 vmarchaud

Hi,

Is it possible to pass a -i argument when using a ecosystem file ? I can't make it work, I would like to control the number of instances using a environment variable and do something like 'pm2 start ecosystem.json --env test -i 2'. I think the config file has a priority over the command line and the -i parameter doesn't get used.

I'm building a Docker container so i want to make it dynamic when using a docker run command.

Thanks

acba avatar Oct 02 '18 21:10 acba

@acba If you use a ecosystem file you must define the instances attributes like this :

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

vmarchaud avatar Oct 03 '18 08:10 vmarchaud

@acba If you use a ecosystem file you must define the instances attributes like this :

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

Hi @vmarchaud, I have thoses parameters and it works fine when I use 'pm2 start eco.js', but i was expecting to override them by using a extra argument like 'pm2 start eco.js -i max' for example. Thanks

acba avatar Oct 03 '18 08:10 acba

@acba You can't override them with the CLI when using a ecosystem, PM2 expect to find all the config in the ecosystem file

vmarchaud avatar Oct 03 '18 08:10 vmarchaud

@vmarchaud @acba You can't override them with the CLI when using a ecosystem, PM2 expect to find all the config in the ecosystem file

Gotcha! I was using a .json file, changed to a .js ecosystem file and did :

const n = (process.argv.indexOf('-i') !== -1) ? Number(process.argv[process.argv.indexOf('-i')+1]) : -1;

... instances: n, ...

Now pm2 can start with all the other parameters configured on the eco file and a i can change the number of instances through my docker env variable.

acba avatar Oct 03 '18 17:10 acba

the cluster supports sticky session? I noticed that process.pid always turns up the same process

ZeroTwink avatar Oct 18 '18 18:10 ZeroTwink

Does 0-second downtime reload work with just one process? / does it launch the new process, move traffic there, and then shut down the old process? Thanks!

Amit-A avatar Nov 13 '18 01:11 Amit-A

Can different values for exec_mode and instances parameters be specified per environment in the ecosystem.config.js file?

smartaz avatar Feb 14 '19 03:02 smartaz

@smartaz No its not possible.

@ZeroTwink No, it doesn't, see https://nodejs.org/dist/latest-v10.x/docs/api/cluster.html#cluster_how_it_works

@Amit-A Yes but we advise to have at least two in case of a crash of the only process, it would take few ms to restart one.

vmarchaud avatar Feb 14 '19 08:02 vmarchaud

I am serving my web application using node js and I am using pm2 as load balancer, For sign-in, I am using passport js (Google, FB, etc). Here the scenario is, whenever user does sign-in, gets redirected back (callback url of my app) to application again which is currently working fine with local system, but what would happen if I am serving my application using pm2 with two instances of app running on same server , when user tries to sign using google from instance 1 (here i am assuming user sent request from instance 1) and after successful login, google redirects to callback url on instance 2 ?

Thanks in advance

Dhiraj3089 avatar Feb 19 '19 11:02 Dhiraj3089

nice docs

lnanhkhoa avatar Apr 25 '19 03:04 lnanhkhoa

I got 404ed on the link

rapando avatar Nov 26 '19 05:11 rapando

I got 404

PedramMarandi avatar Aug 01 '20 00:08 PedramMarandi