For online projects, if you start directly through the node app, if you report an error, you may directly stop the entire service crash, generally monitoring node has several options.


  • supervisor: Generally used for development environments.

  • forever: Manage multiple sites, generally do not need to monitor each site if they do not have a large number of visitors.

  • PM2: The site is more heavily visited and needs a full monitoring page.


The company’s original project used the form of forever, but if there is a problem with the node, there is no way to get effective monitoring data for error troubleshooting, so the newly developed system is going to use the form of pm2 to monitor the front-end and node layer.

 Main characteristics of PM2


  • Built-in load balancing (using the Node cluster cluster module)
  •  background operation

  • 0 seconds downtime for reloading, which I understand probably means no downtime for maintenance upgrades.

  • Boot scripts with Ubuntu and CentOS
  •  Stop unstable processes (avoid infinite loops)
  •  Console Detection
  •  Provide HTTP API

  • Remote control and real-time interface API ( Nodejs module, allows interaction with PM2 process manager )


npm install -g pm2

  •  Basic startup commands

pm2 start


$ pm2 start ./build/server.js
  •  Booting via configuration file will be explained in detail later

 After starting, the console will see the following message:


As shown in the above figure, you can see that the project kafazhe was started successfully, the id is 0, and the status is online.

  •  View detailed status information

pm2 show (appname|id)

$ pm2 show kaifazhe


You can see the details of the kaifazhe process as shown in the image above

  •  View a list of all started processes

pm2 list

$ pm2 list

  • Monitor cpu and memory usage for each node process

    pm2 monit

$ pm2 monit


You can use the pm2 monit function to monitor all node processes, including various responses and error messages.

  •  Display log messages for all processes

pm2 logs

$ pm2 logs
  •  Monitor the status of the machines running these processes

pm2 web

$ pm2 web


I can only say, this is too NB, not only can monitor these processes, but also can monitor the status of the machine running these processes, against all odds. Then it will automatically start a service on the specified port, as shown in the figure at 9615 started a service, we can access. Although I do not quite understand, but for the test operation and maintenance students, it should be quite useful.

  •  Stop specified/all processes

pm2 stop (id|all)


$ pm2 stop 0

$ pm2 stop all


As shown in the figure, we ran two services with online status, and after using stop 0, kaifazhe’s service became stopped, and then using stop all, all processes became stopped.

  •  Restart specified/all processes

pm2 restart (id|all)


$ pm2 restart 0

$ pm2 restart all
  •  Kill specified/all processes

pm2 delete (id|all)


$ pm2 delete 0

$ pm2 delete all


From the above figure, we can see that after restarting 0, process 0 went from stopped to online, then we use delete 0, process 0 disappeared, and then we delete all, we can see that there is no process running now.

 Configuring the PM2 Startup File


The way pm2 is started can be extended in many ways, such as setting up the environment, setting up error message printing, setting up input message printing, and other advanced functions. A single command cannot accomplish all these tasks, so pm2 provides a configuration file to start up.

pm2.config.js


module.exports = {
  apps: [
    {
      name: 'kaifazhe', 
      script: './build/server.js', 
      cwd: './', 
      watch: [
       
        'src',
        'build',
      ],
      ignore_watch: [

        'node_modules',
        'logs',
        'public',
      ],
      node_args: '--harmony',
      env: {
        NODE_ENV: 'development',
        ORIGIN_ADDR: 'http://www.yoduao.com'
      },
      env_production: {
        NODE_ENV: 'production',
      },
      out_file: './logs/out.log', 
      error_file: './logs/err.log',
      merge_logs: true,
      log_date_format: 'YYYY-MM-DD HH:mm Z',
    },
  ],
};


For the env above, we can add many parameter variables inside so that the process.env.XXX we use will change accordingly, for example, above, the value of our process.env.ORIGIN_ADDR is http://www.youdao.com ~.

 load balancing


The most 666 function is coming ~ automatically do load balancing for you, only need a command, before those complex concepts do not matter if you understand.

pm2 start server.js -i (number|max)


pm2 start app.js -i 3

pm2 start app.js -i max

 corresponding in the configuration file: "instance": (number|max)

// pm2.config.js
"instances": 2,  

  Log Related


Here is after a year to add, at first just to simply write a summary, but I did not expect so many people point like, then a year later with the use of deepening, for pm2 there is a summary of the other, just add a ~!

 pm2 log


As you can see from the configuration file above, we can configure logs, including normal out and error logs. In fact, we do not need to do anything, we just need to configure the config inside the line, he will automatically write logs to it:


It’s a very simple feature that includes logging, which is fabulous, but is it really that fabulous? But is it really that great? Our logs are all output to the err.log and out.log within the accumulation, OMG can not imagine, troubleshooting problems must be very laborious, so there is the following log split ~!

 log segmentation


Our normal log, for example, node, should use log4js to write by date, so can pm2 write by date? The answer is yes.


pm2 provides us with a plugin system, and the date splitting function uses the plugin system: pm2-logrotate

pm2 install pm2-logrotate 


It starts up automatically after you install it, and then you can configure various parameters

 The log is then split by date ~


Careful partners may have found, you let me install the above is pm2-logrotate , why you install is pm2-logrotate-ext , well, because it is said that the official pm2-logrotate there is a bug, that is, the date will be normal split, but if you do not write the previous day’s file full such as you set up a 1M but only 500K then the next day’s logs are still inserted into the original out.log (err.log), so the big bulls on writing the This solves the problem pm2-logrotate-ext

 Monitoring visualization with pm2-web


Many people may not like the console, like to visualize the monitoring data more convenient to view and analyze. It does not matter, the masters have provided us with tools, pm2-web, at first glance is specifically with pm2 to use.

npm install -g pm2-web


By default pm2-web automatically starts a port 8080, but we prefer a controlled state, so we start it as per the configuration file.

$ pm2-web --config pm2-web-config.json
// pm2-web-config.json
{
  "www": {
      "host": "localhost",
      "address": "0.0.0.0",
      "port": 6688
  }                         
}

By lzz

Leave a Reply

Your email address will not be published. Required fields are marked *