kill -USR2平滑重启php-fpm的案例

需求

之前给部分服务器增加了logrotate日志分割功能,但是因为没有重启php-fpm,导致php-fpm的日志文件引用的仍然是分割前的老文件,达不到日志分割的效果。因此准备在/etc/logrotate.d/nginx中,postrotate之后,通过kill -USR2 `cat path_of_pid_file` 平滑重启php-fpm

问题

做测试的时候,发现平滑重启过程中,会出现502报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ab -c 100 -n 100000 http://cso.10jqka.com.cn/reload.php
执行期间,通过kill -USR2 `cat /var/run/php-fpm.pid` 执行了11次重启操作
结果如下:
Server Software: nginx
Server Hostname: cso.10jqka.com.cn
Server Port: 80

Document Path: /reload.php
Document Length: 7 bytes

Concurrency Level: 100
Time taken for tests: 12.987 seconds
Complete requests: 100000
Failed requests: 21
(Connect: 0, Receive: 0, Length: 21, Exceptions: 0)
Write errors: 0
Non-2xx responses: 19
Total transferred: 15810360 bytes
HTML transferred: 710056 bytes
Requests per second: 7699.75 [#/sec] (mean)
Time per request: 12.987 [ms] (mean)
Time per request: 0.130 [ms] (mean, across all concurrent requests)
Transfer rate: 1188.83 [Kbytes/sec] received

100000个请求,100并发,失败了20个,报502。猜测应该是每次重启后,php-fpm子进程的增长速度不够快,导致了这个情况:

1
2
3
pm.start_servers = 32
pm.min_spare_servers = 16
pm.max_spare_servers = 64

总结

由于我只是想写日志文件,因此应该用USR1来实现,不应该用USR2

INT, TERM 立刻终止 QUIT 平滑终止 USR1 重新打开日志文件 USR2 平滑重载所有worker进程并重新载入配置和二进制模块