nginx是個用起來很爽的webserver。今天介紹一下nginx的平滑升級。
爲了驗證nginx是真正的平滑升級,在升級過程中使用webbench進行壓力測試,查看升級過程中nginx是否工作正常。
平滑升級完全參照張宴的nginx書操作,之前需要查看現有nginx版本、編譯安裝的參數、進程數據等;
查看版本、編譯參數:
[root@test shell]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.2.7 built by gcc 4.1.2 20080704 (Red Hat 4.1.2-54) TLS SNI support disabled configure arguments: --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
進程數據:
[root@test shell]# ps -ef |egrep -egrep|egrep nginx root 14859 1 0 Oct22 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf appusr 16499 14859 0 Oct22 ? 00:00:00 nginx: worker process appusr 16500 14859 0 Oct22 ? 00:00:00 nginx: worker process appusr 16501 14859 0 Oct22 ? 00:00:00 nginx: worker process appusr 16502 14859 0 Oct22 ? 00:00:00 nginx: worker process
下面進行平滑升級操作:
1、下載想要升級nginx版本,然後編譯覆蓋安裝(默認新的nginx將繼續安裝在舊的nginx目錄下)。爲了體現出升級的不同我在編譯時還增加了關於user、group的定義。
wget http://nginx.org/download/nginx-1.5.6.tar.gz tar -zvxf nginx-1.5.6.tar.gz cd nginx-1.5.6 ./configure --user=appusr --group=appusr --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module && make && make install
2、執行
[root@test nginx-1.5.6]# kill -USR2 14859 【老版本的Nginx主進程號】
3、舊版本 nginx 的主進程將重命名它的 pid 文件例如 .oldbin(如:/usr/local/nginx/logs/nginx.pid.oldbin)
4、此時,新舊兩個版本的nginx進程都在運行中(默認,新版本的nginx在安裝後就自動運行。)此時運行 kill -WINCH 【老版本的Nginx主進程號】 使老版本的nginx 的worker process 逐步結束;
[root@test shell]# kill -WINCH 14859 [root@test shell]# ps -ef |egrep nginx root 4906 14859 0 11:19 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf appusr 4907 4906 6 11:19 ? 00:00:04 nginx: worker process appusr 4908 4906 6 11:19 ? 00:00:04 nginx: worker process appusr 4909 4906 3 11:19 ? 00:00:02 nginx: worker process appusr 4910 4906 6 11:19 ? 00:00:04 nginx: worker process root 4978 29465 0 11:20 pts/0 00:00:00 egrep nginx root 14859 1 0 Oct22 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf appusr 16499 14859 0 Oct22 ? 00:00:25 nginx: worker process is shutting down appusr 16500 14859 0 Oct22 ? 00:00:27 nginx: worker process is shutting down appusr 16502 14859 0 Oct22 ? 00:00:27 nginx: worker process is shutting down
可以看到nginx: worker process
is
shutting down,說明老版本的nginx worker process正在逐步關閉。
5,待所有舊版本的worker process全部退出,
僅由新的工作進程來處理輸入的請求了。 使用kill -QUIT 14859 【老版本的Nginx主進程號】 從容關閉舊版本master(主進程號)。完成版本升級。
此時查看nginx版本信息、編譯信息、及進程信息;
[root@test nginx-1.5.6]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.5.6 built by gcc 4.1.2 20080704 (Red Hat 4.1.2-54) TLS SNI support disabled configure arguments: --user=appusr --group=appusr --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module 【可以看到新版本添加的--user --group 參數】 以下爲新版本的進程信息 [root@test shell]# ps -ef |egrep nginx root 4906 1 0 11:19 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf appusr 4907 4906 8 11:19 ? 00:00:15 nginx: worker process appusr 4908 4906 8 11:19 ? 00:00:15 nginx: worker process appusr 4909 4906 8 11:19 ? 00:00:15 nginx: worker process appusr 4910 4906 9 11:19 ? 00:00:16 nginx: worker process root 5082 29465 0 11:22 pts/0 00:00:00 egrep nginx
再查看開頭提到的webbench 測試我總共請求10分鐘併發1000 共88萬個請求僅5個請求失敗。證明這次平滑升級沒有影響線上服務。