server-side/haproxy

haproxy log setup

C/H 2017. 6. 9. 15:04

/etc/haproxy/haproxy.cfg

global
	log		 127.0.0.1 local0 info
	log		 127.0.0.1 local1 notice
	#chroot	 /var/lib/haproxy
	#pidfile		/var/run/haproxy.pid
	maxconn	 80000		   # 최대 동시접속 설정, 1GM 40,000 ~ 50,000
	#user	 haproxy
	#group	 haproxy
	tune.bufsize	65536
	tune.ssl.default-dh-param   2048
	#nbproc	 4			   # 실행 프로세스 수
	daemon					  # daemon 모드실행(권장)
	# turn on stats unix socket
	stats socket /var/lib/haproxy/stats.sock mode 600 level admin
	stats timeout 2m

defaults
	log global
	option tcplog
	option  dontlognull
	option  redispatch
	retries 2
	timeout queue 45s
	timeout connect 3s
	timeout server 5s
	timeout client 5s
	timeout check 5s

frontend mysql-read
	bind :3306

	acl read hdr(host) -i readmcs.doamin.com
	acl read hdr(host) -i readmcs2.doamin.com
	acl read hdr(host) -i mcsread.doamin.com
	acl cud hdr(host) -i mcscud.doamin.com
	acl read3 hdr(host) -i mcsread3.doamin.com
	acl read5 hdr(host) -i mcsread5.doamin.com
	acl read6 hdr(host) -i mcsread6.doamin.com
	acl read7 hdr(host) -i mcsread7.doamin.com


	use_backend mysql-read if read
	use_backend mysql-cud if cud
	use_backend mysql-read3 if read3
	use_backend mysql-read5 if read5
	use_backend mysql-read6 if read6
	use_backend mysql-read7 if read7

	default_backend	mysql-cud

backend mysql-cud
	mode tcp
	option mysql-check user haproxy_check
	balance roundrobin
	server node0 mycud:3306 check

backend mysql-read
	mode tcp
	option mysql-check user haproxy_check
	balance roundrobin
	server node5 myr5:3306 check
	server node6 myr6:3306 check
	server node7 myr7:3306 check
backend mysql-read3
	mode tcp
	option mysql-check user haproxy_check
	balance roundrobin
	server node3 myr3:3306 check
backend mysql-read5
	mode tcp
	option mysql-check user haproxy_check
	balance roundrobin
	server node5 myr5:3306 check
backend mysql-read6
	mode tcp
	option mysql-check user haproxy_check
	balance roundrobin
	server node6 myr6:3306 check
backend mysql-read7
	mode tcp
	option mysql-check user haproxy_check
	balance roundrobin
	server node7 myr7:3306 check

/etc/hosts

127.0.0.xx0 mycud
127.0.0.xx3 myr3
127.0.0.xx5 myr5
127.0.0.xx6 myr6
127.0.0.xx7 myr7

/etc/rsyslog.conf

# line 15,16: uncomment, lne 17: add, TCP 와 혼동할 수 있으니 주의.
$ModLoad imudp
$UDPServerRun 514
$AllowedSender UDP, 127.0.0.1

# line 54: change like follows
*.info;mail.none;authpriv.none;cron.none,local0.none;local1.none   /var/log/messages
local0.*                                               /var/log/haproxy/haproxy.info.log
local1.*                                               /var/log/haproxy/haproxy.notice.log
systemctl restart rsyslog 

tail -f /var/log/haproxy/*

# 이후 데이터베이스 접속을 하면 로그가 찍힌다.
Jun  8 18:34:38 localhost haproxy[32269]: 121.131.164.xxx:52904 [08/Jun/2017:18:34:38.505] mysql-read mysql-read/node5 1/0/59 9283 -- 0/0/0/0/0 0/0

/etc/rsyslog.d/haproxy.conf

다른 방식으로 설정

if ($programname == 'haproxy') then -/var/log/haproxy/haproxy.log
if ($programname == 'haproxy' and $syslogserverity-text == 'info') then -var/log/haproxy/haproxy-info.log

# 혹은
local0.*                                               /var/log/haproxy/haproxy.info.log
local1.*                                               /var/log/haproxy/haproxy.notice.log

Log rotation

# /etc/logrotate.d/haproxy
/var/log/haproxy/*.log {
    copytruncate
    daily
    rotate 28
    missingok
    notifempty
    compress
    dalaycompress
    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
    endscript
}
반응형