Simulate nginx hot deployment

Keywords: Web Server Nginx sudo

Hot deployment is to upgrade the software while the application is running without restarting the application.

First, simulate an online environment where Nginx needs to be upgraded. Assuming that the old version is nginx-1.0.15, it needs to be upgraded to nginx-1.16.0.

Configure older versions

# Download nginx-1.0.15
wget http://nginx.org/download/nginx-1.0.15.tar.gz

# Decompress the package
tar -zxf nginx-1.0.15.tar.gz

# Enter the unzipped directory
cd nginx-1.0.15

#  Configure nginx
./configure --prefix=/home/nginx

# Compilation and installation
make && make install

# Run nginx
sudo /home/nginx/sbin/nginx

At this time, you can see the welcome page of nginx by visiting the server address.

Get new version binary

# Download nginx-1.16.0
wget http://nginx.org/download/nginx-1.16.0.tar.gz

# Decompress the package
tar -zxf nginx-1.16.0

# Enter the unzipped directory
cd nginx-1.16.0/

# Configure nginx
./configure --prefix=/home/nginx

# Compile only without installation
make

In the compiled objs directory, you can see the binary nginx.

Thermal deployment

After the above steps, we have implemented a running version of nginx and a compiled version of nginx binary execution file.

The hot deployment process is as follows:

  1. Backing up old nginx executables
  2. The new nginx executable directly replaces the old one (the old nginx process is still running)
  3. Send the hot deployment signal to the nginx master process, the new nginx process starts, and the old worker no longer receives the request.
  4. Close the old worker process and complete the hot deployment.
# backups
cp /home/nginx/sbin/nginx /home/nginx/sbin/nginx.old

# replace 
cp -f objs/nginx /home/nginx/sbin/nginx

# View master pid
ps -ef | grep nginx 
root     23712     1  0 21:21 ?        00:00:00 nginx: master process /home/nginx/sbin/nginx
nobody   23715 23712  0 21:21 ?        00:00:00 nginx: worker process

# Send the hot deployment signal. Here, the master pid is replaced by the one queried by itself
kill -USR2 23712

# Check the current nginx process. 27522 is the new master process
ps -ef | grep nginx 
root     23712     1  0 21:21 ?        00:00:00 nginx: master process /home/nginx/sbin/nginx
nobody   23715 23712  0 21:21 ?        00:00:00 nginx: worker process
root     27522 23712  0 21:41 ?        00:00:00 nginx: master process /home/nginx/sbin/nginx
nobody   27524 27522  0 21:41 ?        00:00:00 nginx: worker process

# Close old worker
kill -WINCH 23712

# If you look at the process again, you can see that the old worker process is closed
ps -ef | grep nginx 
root     23712     1  0 21:21 ?        00:00:00 nginx: master process /home/nginx/sbin/nginx
root     27522 23712  0 21:41 ?        00:00:00 nginx: master process /home/nginx/sbin/nginx
nobody   27524 27522  0 21:41 ?        00:00:00 nginx: worker process

The purpose of keeping the old master process is to quickly fallback to the original version when there is a problem with the new version. Do you need to roll back urgently if you find problems?

cp -f nginx.old nginx
# Pull up the old version of worker process (- HUP is equivalent to - s reload)
kill -HUP old_master_pid
# Make the new version of worker no longer accept the request
kill -USR2 new_master_pid
# Close the new version of woker process
kill -WINCH new_master_pid

If you want to exit the old version of nginx, you can execute the command:

kill -QUIT old_master_pid

Posted by fatmikey on Fri, 01 Nov 2019 14:26:35 -0700