Location>code7788 >text

Nuxt3+PM2 cluster mode startup and errata

Popularity:731 ℃/2024-10-14 08:45:31

a factor (leading an effect)

Having previously written about Nuxt3.Nuxt3 environment variable configurationI used PM2, but there was a problem with some of the configurations in it, and I recently had time to verify it again, so here's an errata.

concern

One of the startup configurations for PM2 isexec_modeThe default isforkThe other optional value isclusterfork is in single-process mode.cluster It is multiprocess mode, which is often referred to as cluster mode.

The earliest time to start using Nuxt3, but also in the testing phase, when we found a lot of information are based on Nuxt2, Nuxt3 documentation is not very complete, so many configurations are referenced to Nuxt2, a bit of an emergency, and finally cobbled together a set of configurations that can be started properly and did not have to adjust the details can be seen in the previous article, where there are problems with the pick out of the field.

{
  exec_mode: "cluster",
  instances: "max", // Or a number of instances
  script: "npm",
  args: "start",
}

What is the problem caused by the configuration here? The first thing you can see is that the program is started via npm, via thenpm run startThis command executes thehit the nail on the headstartscriptsnode .output/server/, first of all this is possible to start, but there are some problems:

Occasionally, the server will have a high CPU usage situation, check the CPU usage situation and found that it is the node process, then locate the PM2, check the operation situation and found that only one of the multiple processes can start successfully, and the other processes are restarted repeatedly, so the CPU naturally rises. Then I went to check the logs and found that only one process could start successfully because the port was occupied. At this point, I realized that there might be a problem with the configuration, but I didn't have the time to adjust it at that time, so I've always solved the problem by manually stopping the other processes.

settle (a dispute)

There are problems to be solved, of course. I don't have much exposure to cluster mode, but since I have this configuration, surely I won't be unable to start it just because of port occupancy, and there have been major version changes in Nuxt3 so far, so I went through the Nuxt3 docs again to see what the official way to configure PM2 is:

docs/getting-started/deployment

 = {
  apps: [
    {
      name: "NuxtAppName",
      port: "3000",
      exec_mode: "cluster",
      instances: "max",
      script: "./.output/server/",
    },
  ],
};

At first I didn't think the problem was with the startup script until I found another issue on githubPort Already in Use in Cluster ModeThe question, including the one associated with it, mentions that you should not use npm to start it, but rather use the script path directly.

You can't cluster application via npm start. To make cluster work, in the script attribute of your , directly put the path of your javascript app.

The process is not directly hosted by pm2 when it is started via npm, but it is started via npm, and pm2 just monitors this process, so pm2 can't manage this process very well. You can also see in the above screenshot that the version number is 0.39.7, which is in fact the version of the nvm on the server, and not the version of the nuxt3 program.

About Cluster Mode

At first I suspected that it was a cluster mode issue, but even after changing to fork, I still got the port occupancy error, although it can be changed by modifying theinstancesto solve the problem, but vaguely feel that it is not the right way, after solving the startup error problem, I tried again to use the cluster mode, but I'm still not quite sure if I should use this configuration, so I did some tests, just some tests in the local environment, the results are not accurate, only a reference.

instancesis the number of processes to be used, which can have a value of'max'-1or other numbers.'max'indicates that the maximum number of instances is used.-1It means that the least used is basically 1. The other numbers indicate the number of instances using the specified number of instances, which is calculated based on the number of CPU cores, such as a single-core dual-threaded CPU, the number of instances is 2.

First I started a nuxt3 project locally, simulating a server environment with only 2 instances, and then executed it using two different pm2 startup methods.

utilizationpm2 monitMonitor the process, you can see that at this point there is not much difference, this time with jmeter pressure test, create 500 threads to access the cluster mode process, you can see that both processes are running:

Then the same pressure test, but using fork mode, you can see that only one process is running:

I do not understand the pressure test, just a simple comparison, you can see that the cluster mode CPU fluctuation is still more obvious, fork mode CPU fluctuation is not obvious, but the memory consumption will climb extremely fast, did not capture the figure. Continue to increase the pressure test thread, memory changes are not obvious, the cluster mode CPU occupation will be more obvious, it may also be because the pressure test application and the pressure test application are in the local, so this test result is not accurate, in terms of error rate, also did not measure the difference between the two. Through a simple test and did not feel the advantages and disadvantages, the advantages of clustering may be reflected in the performance and fault tolerance, in the hardware can not see too much effect, here is just to make a record, the specific need for more practical experience.

Cluster mode allows networked applications (http(s)/tcp/udp servers) to scale across all available cpu's without any code changes. Depending on the number of available cpu's, this can greatly improve the performance and reliability of the application.