Linux Nginx server smooth upgrade and rollback

First, the hot deployment concepts

Process 1.1 nginx services

nginx is a multi-process architecture, multi-process architecture is designed to ensure high reliability and high availability nginx, includes:

  • master process: the parent process is responsible for managing the worker process.
  • worker process: the child process is, in general worker processes configured for the same number of server CPU core, worker process to handle the specific request.
  • cache process: is the child process, including the cache manager and cache loader process, mainly to do caching reverse proxy use.

Principle 1.2 nginx can be hot deployment

Hot deployment, that is, after nginx.conf modify the configuration file, does not need to stop Nginx, do not need to interrupt request, it can make the configuration file to take effect, namely online upgrade, that does not interrupt the user's request later.We already know the worker through the above process is responsible for dealing with specific requests, so if you want to achieve the effect of hot deployment, after Nginx can modify the configuration file nginx.conf, regenerate a new worker process, of course, will be processed the request to the new configuration, and a new request must be handed over to the new worker process, as the old worker process, such as those previous requests after processing, kill off the can. Then use the new configuration to create a new worker, an updated version of it can be done online, new and old versions of the process can exist at the same time, it does not affect the customer's visit.

Second, the smooth deployment of upgrade and hot rolled back

lab environment : Build a successful version 1.15.9 of nginx server <Previous>

1, added on Release Notes vim src/core/nginx.h

Here Insert Picture DescriptionHere Insert Picture Description
2, return to the previous compilation environment

1) installed before deleting the directory nginx:rm -fr /usr/local/nginx

  • Before deleting, turn off the nginx service.

2) Back to the build directory: make clean

Here Insert Picture Description
3) Check the current process of nginx ps aux | grep nginx

Here Insert Picture Description
4) Compile again:

./configure --prefix=/usr/local/nginx --with-file-aio

5)安装:make && make install
Here Insert Picture Description

Here Insert Picture Description

6) open nginx: /usr/local/nginx/sbin/nginx
7) to see nginx processes: ps aux|grep nginx
8)View nginx version number: / usr / local / nginx / sbin / nginx -v
Here Insert Picture Description

2.1 nginx smooth upgrade

1) First, unpack:tar zxf nginx-1.17.4.tar.gz
2) In the nginx-1.17.4 directory compiled,./configure --prefix=/usr/local/nginx --with-file-aio

Here Insert Picture Description
3)make

  • Note: The updated when you can not make install, otherwise nginx will restore all previous service configuration

Here Insert Picture Description
4) In the nginx/nginx-1.17.4/objs/directory, run make after the resulting binary file nginx. In this case, execute the command ./nginx -Vto view the nginx version. Found version is nginx / 1.17.4, but the version in force or nginx / 1.15.9

  • After make the emergence of binaries nginx objs / down, make install to actually copy the binaries and some configuration files to / usr / local / nginx directory

Here Insert Picture Description

Here Insert Picture Description

5) ps -ef | grep nginxyou can see the original two processes

Here Insert Picture Description
6)pid kill -USR2 original master processLet the old version of the worker process is no longer accepts the request , ps -ef | grep nginxwe can see four processes

Here Insert Picture Description
Here Insert Picture Description
7)kill -WINCH original pid master the process, close the child process of the original process, master is not the end, to prevent the update failed. ps -ef | grep nginxYou can see three processes, this time a new version is available for use

Here Insert Picture Description
8)/usr/local/nginx/sbin/nginx -V You can see the version has been updated

Here Insert Picture Description

At this point, the version upgrade has been completed l.

After the rollback of 2.2 nginx version update fails

If we just update fails, go back to the original version of nginx / 1.15.9 of
1) reducing nginx script

Here Insert Picture Description

2)kill -HUP older version of the master process PID, renewed the old version of the master process, let it receives a request

Here Insert Picture Description
3)
kill -USR2 27129, so that the new version of the master process does not receive a request
kill -WINCH 27129, close the new version of the worker process
Here Insert Picture Description
Here Insert Picture Description
4) Check nginx version: / usr / local / nginx / sbin / nginx -v

Here Insert Picture Description
5) compile again, and makeit rolled back.

Actually in the enterprise, it will be rolled back if the update fails immediately, and updates the time only once, immediately rollback failure

Published 105 original articles · won praise 0 · Views 1411

Guess you like

Origin blog.csdn.net/weixin_45029822/article/details/104655287