切割haproxy的日志【转】

日志的切割有以下几种方法:

1.写个定时任务,每天某个时间点把旧的日志重命名,并对服务重启使其重新打开日志并写入。

2.通过管道的方式把新产生的日志写到另外一个日志文件里。

3.通过logrotate来切割日志,logrotate是系统自带的服务,可以切割任何日志,不仅仅是nginx。

因为我这里的haproxy是yum 安装的。所以logrotate目录下本身就有haproxy的日志切割,这时我们只要改一些haproxy.log的路径就行了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/data/logs/haproxy/haproxy.log {
    daily
    rotate 10
    missingok
    notifempty
    dateext
    compress
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
        /bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true
    service haproxy restart
    endscript
}

 我们可以在测试环境下试一下

logrotate -f /etc/logrotate.d/haproxy

我发现我这里是成功的

默认情况下,Haproxy日志文件不会按天或按大小的分割日志,会将所有日志输出到一个haproxy.log文件中,这样随着时间的推移,这个文件会越来越大,非常不利于日志的管理和归档,本文将介绍通过logrotate工具对haproxy日志进行分割。

复制代码
/etc/haproxy/haproxy.log {
    daily
    rotate 200
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -sf $(cat /var/run/haproxy.pid);service rsyslog reload
    endscript
}
复制代码
  • monthly: 日志文件将按月轮循。其它可用值为‘daily’,‘weekly’或者‘yearly’。
  • rotate 5: 一次将存储5个归档日志。对于第六个归档,时间最久的归档将被删除。
  • compress: 在轮循任务完成后,已轮循的归档将使用gzip进行压缩。
  • delaycompress: 总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用。
  • missingok: 在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误。
  • notifempty: 如果日志文件为空,轮循不会进行。
  • create 644 root root: 以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件。
  • postrotate/endscript: 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd 进程将立即再次读取其配置并继续运行。

注意:如果没有启动 crontab 需要启动一下,这样任务计划才会执行。

service crond start  

3、验证

手动运行logrotate

logrotate可以在任何时候从命令行手动调用,要调用为/etc/lograte.d/下配置的所有日志调用logrotate:

logrotate /etc/logrotate.conf 

排障过程中的最佳选择是使用‘-d’选项以预演方式运行logrotate。要进行验证,不用实际轮循任何日志文件,可以模拟演练日志轮循并显示其输出。

logrotate -d /etc/logrotate.d/haproxy

1
2
3
4
5
6
7
8
9
10
11
12
[root@LB1_Redis1-live ~]# man logrotate
[root@LB1_Redis1-live ~]# logrotate -d /etc/logrotate.d/haproxy
reading config file /etc/logrotate.d/haproxy
reading config info for /etc/haproxy/haproxy.log
 
Handling 1 logs
 
rotating pattern: /etc/haproxy/haproxy.log  after 1 days (15 rotations)
empty log files are not rotated, old logs are removed
considering log /etc/haproxy/haproxy.log
  log does not need rotating
not running postrotate script, since no logs were rotated
 

配置非常的简单,但这么做有一个弊端,就是在分割日志时需要重载haproxy这样会造成丢流量,这对于高流量、高可靠的系统来说是不允许的,所以可以根据需求选择业务低峰的时间段来执行分割日志,也可以根据下边这篇文章来配置《真正零停机 HAProxy 重载》http://www.open-open.com/lib/view/open1430094706677.html

这里我们根据业务的低峰期来重启haproxy

我看了一下,我这里也的低峰期是凌晨5点

所以,我这里配置一个定时任务

[root@LB1_Redis1-live ~]# crontab -l
*/30 * * * * /usr/sbin/ntpdate cn.pool.ntp.org >/dev/null 2>&1
0 5 * * * /usr/sbin/logrotate -f /etc/logrotate.d/haproxy

文章摘自:

http://www.tuicool.com/articles/yym6nub

http://opensgalaxy.com/2016/08/30/haproxy%E6%97%A5%E5%BF%97%E5%88%86%E5%89%B2/

http://hao360.blog.51cto.com/5820068/1342986/

转自

切割haproxy的日志 - BigBao的博客 - 博客园
https://www.cnblogs.com/smail-bao/p/6093122.html

原文地址:https://www.cnblogs.com/paul8339/p/15293796.html