Search code examples
node.jsvue.jspm2serve

PM2 node application spawning each cluster instance on a new port


I have a node app with the following start scrip npm run build && serve -s dist. I am looking to utilize PM2 cluster for 0 downtime when deploying. As I understand it, PM2 should be starting multiple instances of the application that all share the same port. I think we have everything configured right but something with the node app is causing the instances to run on a separate port so they are not sharing the port. I tested with a simple Hello World app and it works exactly as I expected it to (all instances sharing one port) so that is pointing me to something within app stack causing it. Wondering if there is any reason why it might be spawning instances on its own port or any direction I can be pointed to.

My PM2 ecosystem.config -

module.exports = {
    apps : [{
        name: "Application",
        script: "npm",
        args: "start",
        exec_mode: "cluster",
        instances : 2,
        wait_ready: true,
        listen_timeout: 30000
    }]
}

Package.json -

  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "test:ci": "vue-cli-service test:unit",
    "test": "vue-cli-service test:unit \"--verbose\"",
    "lint": "vue-cli-service lint",
    "start": "npm run build && serve -s dist"
  },

PM2 ls command -

➜  application-fe git:(develop) ✗ pm2 ls
┌─────┬────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name           │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ Application    │ default     │ N/A     │ cluster │ 62342    │ 67s    │ 0    │ online    │ 0%       │ 46.5mb   │ user… │ disabled │
│ 1   │ Application    │ default     │ N/A     │ cluster │ 62476    │ 37s    │ 0    │ online    │ 0%       │ 45.4mb   │ user… │ disabled │
└─────┴────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

Ports opened for each instance -

➜  application-fe git:(develop) ✗ sudo lsof -i -P -n | grep LISTEN | grep node
node      62522      user   23u  IPv6 0x724bf7c95bff58bb      0t0    TCP *:5000 (LISTEN)
node      62551      user   21u  IPv6 0x724bf7c95322025b      0t0    TCP *:51494 (LISTEN)

Solution

  • Looks like it came down to using "serve -s dist" in the package.json.

    Switched to using express to serve the static files and PM2 creates two instances that share the port and behaves as expected.