至於爲什麼開啓日誌記錄,即使我不說,你們這羣老司機也懂。
開啓日誌分割,主要涉及到兩個知識點:
①HAproxy關於日誌記錄的配置
②Rsyslog的配置
環境:
CentOS6.5
haproxy-1.6.11
配置日誌記錄的前提是HAproxy可以正常使用,操作者熟悉Haproxy的配置文件
配置HAproxy的日誌記錄選項
[root@node1 haproxy]# vim /etc/haproxy/haproxy.cfg defaults log global ##開啓全局日誌,對所有池子生效 log 127.0.0.1:514 local3 ##將3級別日誌信息發送到本機514端口的rsyslog程序 :wq!
創建Haproxy日誌存儲的文件
[root@node1 haproxy]# mkdir -p /var/log/haproxy/ [root@node1 haproxy]# touch /var/log/haproxy/haproxy.log
配置Rsyslog配置,開啓514端口的偵聽
[root@node1 haproxy]# vim /etc/rsyslog.conf $ModLoad imudp #默認是註釋,將註釋去掉 $UDPServerRun 514 #默認是註釋,將註釋去掉 local3.* /var/log/haproxy/haproxy.log ##添加一行該信息,該信息的local3要和ha裏面的相對應 ##後面的便是你希望haproxy的日誌存儲到哪裏,指定文件
檢查rsyslog是否開啓監聽
[root@node1 haproxy]# netstat -tunlp|grep 514 udp 0 0 0.0.0.0:514 0.0.0.0:* 2615/rsyslogd udp 0 0 :::514 :::* 2615/rsyslogd
重啓兩個服務服務
[root@node1 haproxy]# service rsyslog restart Shutting down system logger: [ OK ] Starting system logger: [ OK ] [root@node1 haproxy]# service haproxy restart Shutting down haproxy: [ OK ] Starting haproxy: [ OK ]
測試日誌是否開始記錄
[root@node1 haproxy]# tail -f /var/log/haproxy/haproxy.log Sep 26 10:56:40 localhost haproxy[2561]: Stopping proxy stats in 0 ms. Sep 26 10:56:40 localhost haproxy[2561]: Stopping frontend dev_posp in 0 ms. Sep 26 10:56:40 localhost haproxy[2561]: Stopping backend dev_posp in 0 ms. Sep 26 10:56:40 localhost haproxy[2561]: Proxy stats stopped (FE: 0 conns, BE: 0 conns). Sep 26 10:56:40 localhost haproxy[2561]: Proxy dev_posp stopped (FE: 6 conns, BE: 0 conns). Sep 26 10:56:40 localhost haproxy[2561]: Proxy dev_posp stopped (FE: 0 conns, BE: 6 conns). Sep 26 10:56:40 localhost haproxy[2635]: Proxy stats started. Sep 26 10:56:40 localhost haproxy[2635]: Proxy dev_posp started. Sep 26 10:56:40 localhost haproxy[2635]: Proxy dev_posp started. Sep 26 10:57:01 localhost haproxy[2636]: Connect from 192.168.222.1:57057 to 192.168.222.130:80 (dev_posp/TCP) Sep 26 10:57:01 localhost haproxy[2636]: Connect from 192.168.222.1:57058 to 192.168.222.130:80 (dev_posp/TCP)
技巧根據不同的需求來打印不同的日誌級別
日誌級別有local0~local7
#define KERN_EMERG "<0>" /* system is unusable */ #define KERN_ALERT "<1>" /* action must be taken immediately */ #define KERN_CRIT "<2>" /* critical conditions */ #define KERN_ERR "<3>" /* error conditions */ #define KERN_WARNING "<4>" /* warning conditions */ #define KERN_NOTICE "<5>" /* normal but significant condition */ #define KERN_INFO "<6>" /* informational */ #define KERN_DEBUG "<7>" /* debug-level messages */