doc-pm2
doc-pm2 copied to clipboard
Load-Balancing | Guide | PM2 Documentation
https://pm2.io/doc/en/runtime/guide/load-balancing/
PM2 - Guide, References, Tutorials and Best Practices about PM2 and Node.js.
nice work!
powerful and great for development thanks
Hi, I am new to pm2 and would like to know two things:
- Would it be possible to deploy to a machine from local folder/zip/tar instead of a git repository?
- 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
- Not currently.
- You need to either use another load balancer or do a DNS round robin approach.
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 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).
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 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"
}]
}
@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 You can't override them with the CLI when using a ecosystem, PM2 expect to find all the config in the ecosystem file
@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.
the cluster supports sticky session? I noticed that process.pid always turns up the same process
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!
Can different values for exec_mode and instances parameters be specified per environment in the ecosystem.config.js file?
@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.
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
nice docs
I got 404ed on the link
I got 404