Search This Blog

Wednesday, January 29, 2025

How to use PM2 to Manage Multiple Node Services in production

 If you want to manage and monitor the services, you can use PM2, a process manager for Node.js. This is particularly useful in production environments.

  1. Install PM2 globally:


    npm install pm2 -g
  2. Create an ecosystem file (e.g., ecosystem.config.js) to define all services:


    module.exports = { apps: [ { name: 'microservice1', script: './microservice1/server.js', watch: true, env: { NODE_ENV: 'production', }, }, { name: 'microservice2', script: './microservice2/server.js', watch: true, env: { NODE_ENV: 'production', }, }, { name: 'microservice3', script: './microservice3/server.js', watch: true, env: { NODE_ENV: 'production', }, }, ], };
  3. Start all services:


    pm2 start ecosystem.config.js
  4. Monitor services:

    pm2 list
Cheers,

Kapil 

How can package all microservices using docker

 If each of your microservices is containerized using Docker, you can use Docker Compose to define and run all of your services in one go. This is one of the most common ways to manage multiple microservices.

Steps:

  1. Create Dockerfiles for each microservice (if you haven't already): Each microservice should have its own Dockerfile to build the image. For example:

    dockerfile:
    # Dockerfile for microservice 1 FROM node:14 WORKDIR /app COPY . . RUN npm install CMD ["npm", "start"
  2. Create a docker-compose.yml file at the root of your project: This will define all your microservices and how they interact.


    version: "3" services: microservice1: build: context: ./microservice1 ports: - "3001:3001" environment: - NODE_ENV=production microservice2: build: context: ./microservice2 ports: - "3002:3002" environment: - NODE_ENV=production microservice3: build: context: ./microservice3 ports: - "3003:3003" environment: - NODE_ENV=production
  3. Run all services with Docker Compose:

    • In the root directory where your docker-compose.yml file is, simply run:

    docker-compose up --build

    This command will build the Docker images (if necessary) and start all the services.

  4. Stopping the services:


    docker-compose down
Cheers,
Kapil 

How can package all node js microservices and start in one command

 

Using npm scripts to start all services

If your microservices are not containerized, you can set up an npm script to run all services with one command.

  1. Create an npm script in your root package.json that runs all microservices in parallel using something like concurrently or npm-run-all.

    First, install the necessary package:

    npm install concurrently --save-dev

  2. Add a script to your root package.json:

    json:
    { "scripts": { "start": "concurrently \"npm run start:microservice1\" \"npm run start:microservice2\" \"npm run start:microservice3\"", "start:microservice1": "cd microservice1 && npm start", "start:microservice2": "cd microservice2 && npm start", "start:microservice3": "cd microservice3 && npm start" } }
  3. Run all services with a single command:

    npm start

This will start all your microservices concurrently.


There are other ways as well. If if you have rest services in different tech like JAVA aor Python etc. we can use docker compose. 

Check docker compose on this link -> how-can-package-all-microservices-using-docker

Other option could be PM2 -> how-to-use-pm2-to-manage-multiple-node-services

Cheers:

Kapil

Popular Posts