Original text: medium.com/hackernoon/…
Maintaining your code base and keeping it relevant can be difficult; By contrast, the biggest challenge in maintaining an application is keeping it alive and running. Not only that, but with Node.js becoming the core language for most back-end apis, scaling single-threaded programs becomes difficult; This is where PM2 comes in and is popular.
There are a lot of process management tools out there, well-known ones like Forever, strong-PM, or the nice Linux tool SystemD. And then there’s PM2, the star of this article, with over 41 million downloads and 31K GitHub stars. The reason, in a nutshell, is that it is easy to use and makes managing a production environment seamless.
What is a PM2?
As stated on the homepage of the new edition, PM2 is a “Battle hardened” Node.js application runtime and process management tool suitable for production environments. It also comes with a built-in load balancer, which makes it easier to scale applications. Best of all, it works on Linux, Windows, and macOS.
Using a process.json configuration file Json, processes. Json, or server. json, which is passed as a parameter to commands like pm2 start), and you can specify which processes you want to run and how many processes you want to scale to. When PM2 is started, it does everything else according to the above configuration file (even a little more 😉 than the configuration file specifies).
All of this means that PM2 can help you keep node.js applications running forever and automatically restart with zero downtime when you update your application or server.
Install the PM2
Installing the PM2 is a piece of cake. First, make sure you have configured the process.json file to start the process.
To install the yarn Add Global PM2, run yarn Add Global PM2. If you use a Docker container (right, also support the Docker), according to the pm2. Keymetrics. IO/docs/usage /… Instructions for installation.
If you’re really curious what it looks like, here’s an example of the process_prod.json file for an open source RSS & podcast application:
// process_prod.json
{
"apps": [{
"name": "api"."cwd": "api/dist"."script": "server.js"."watch": false
},
{
"name": "conductor"."cwd": "api/dist/workers"."script": "conductor.js"."watch": false
},
{
"name": "rss-worker"."cwd": "api/dist/workers"."script": "rss.js"."instances": 2."exec_mode": "cluster"."watch": false
},
{
"name": "podcast-worker"."cwd": "api/dist/workers"."script": "podcast.js"."instances": 2."exec_mode": "cluster"."watch": false
},
{
"name": "og-worker".// Application name
"cwd": "api/dist/workers".// Start the application directory
"script": "og.js".// Start the script
"instances": 2.// Number of started application instances
"exec_mode": "cluster".// Start mode, fork by default
"watch": false.// If watch is allowed, file changes in it will cause a reboot
"max_memory_restart": "150M" // The maximum amount of memory used to trigger a restart}}]Copy the code
As you can see, we ran several processes and PM2 took control of everything with ease; It automatically uses node.js’s Cluster API to implement multiple processes.
: the configuration file complete introduction see pm2. Keymetrics. IO/docs/usage /…
Skills & Experience
For any application (or, in this case, a process management tool), it’s good to know some tips and experience from the pioneers.
Automatic restart
Once PM2 is started, your application lives forever and restarts automatically when the application crashes and the machine reboots — all with a simple command (to get an automatically configured startup script for the machine you’re on) :
pm2 startup
Copy the code
Such as:
$ pm2 startup
[PM2] You have to run this command as root. Execute the following command:
sudo su -c "env PATH=$PATH: / home/unitech/NVM/versions/node/v4.3 / bin pm2 startup < distribution > -u < user > - HP < home - path >Copy the code
Simply copy and run the resulting script as printed.
Process management
No matter how many applications you run, PM2’s set of commands allows you to manage their respective states. Here are some common commands (in no particular order) :
pm2 start process_prod.json
– Start processes using the process configuration filepm2 ls
– List all applicationspm2 stop <app>
– Stop the specified applicationpm2 start <app>
– Start a specified applicationpm2 <app> scale N
– Scale the number of application instancespm2 kill
– Kill all running applicationspm2 restart
– Restart all running applicationspm2 reload
— Reload the application configuration (this comes in handy if you change the application’s environment variables)
Process monitoring
Running the command pm2 monit returns rich data about application health. For example, you’ll see CPU utilization, memory usage, request minutes, and so on.
Log management
The PM2 has the log management function built in. It aggregates log data from all your applications and writes it to a source file for reading. You can even see what’s going on behind the scenes with the fact tracking log. PM2’s log management also provides a log loop, which is important, especially if the application is frequently exporting detailed logs.
Here are three commands I use a lot, and I think you should too:
pm2 logs
– Output logs from all running applicationspm2 logs <app>
– Logs are generated only from the specified applicationpm2 flush
– Refresh all log data to release disk space
Keep in mind that ensuring a log loop is the most important thing. Doing so will split a large log file into smaller files that are easy for PM2 to manage. To do this, run the following command:
pm2 install pm2\-logrotate
Copy the code
Log management. More information can be in pm2 keymetrics. IO/docs/usage /… To find it.
If you find that your instances are frequently filled with logs, consider using a centralized logging service, such as Loggly, Papertrail, or ELK.
Best practices
In general, I’m happy to follow the practices described in 12Factor.net/. This will help… All the advantages of PM2. If you’ve read the statement, you’ve seen these 12 rules:
- One code base under version control, multiple deployments
- Explicitly declare and isolate dependencies
- Store configuration in the environment rather than in code
- Think of backend services as additional resources
- Strictly distinguish between the build and run phases
- Run the application as one or more stateless processes
- Output services through port binding
- Extend through the process model
- Maximum robustness with fast startup and graceful shutdown
- Keep development and online environments as consistent as possible
- Think of logs as a stream of events
- Run background administration tasks as one-off processes
If you follow these rules, you will be able to scale any application efficiently and safely with PM2.
–End–
Search fewelife concern public number reprint please indicate the source