Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Haproxy + keepalived load balancer log customization

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Benefits of customizing output logs

All user requests go through the load balancer, so collecting logs here is much more efficient than collecting logs and summarizing them on each back-end real server.

Work goal

 records user access logs: haproxy generation.

 records keepalived logs: keepalived startup, running status, for troubleshooting.

The master plan  uses separate partitions to store logs. Because the keepalived log has less content, the storage space can be shared with the haproxy log. For example, in my project, I use / data/logs as the storage path of the log (/ data is better to use a separate partition or disk), haproxy log name haproxy.log,keepalived log name keepalived.log. If log retention is required, archiving and rotation are required.  system logs are separated from haproxy logs and do not repeat records-the poorly configured solution will cause system logs and haproxy logs to be recorded repeatedly, taking up a lot of disk space. Once the partitions related to the system are filled, the whole load balancer will fail. Components involved in customizing logs

 Syslog service rsyslog.

 load balancer haproxy.

 highly available keepalived.

Technical realization

Select the standby machine from the load balancer to configure it first, which has the advantage that it will not affect the existing business. Then create the storage directory / data/logs and make sure that it can create and write files.

 Syslog configuration: modify the / etc/rsyslog.conf file as follows (excluding comment lines and blank lines): $ModLoad imuxsock # provides support for local system logging (e.g. Via logger command) $ModLoad imjournal # provides access to the systemd journal$ModLoad imklog # reads kernel messages (the same are read from journald) $ModLoad immark # provides-- MARK-- message capability$ModLoad imudp$UDPServerRun 514$ WorkDirectory / var/lib/rsyslog$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat$IncludeConfig / etc/rsyslog.d/*.conf$OmitLocalLogging on$IMJournalStateFile imjournal.statemail.none;authpriv.none Cron.none / var/log/messagesauthpriv.* / var/log/securemail.*-/ var/log/maillogcron.* / var/log/cron*.emerg : omusrmsg:*uucp News.crit / var/log/spoolerlocal7.* / var/log/boot.loglocal3.* / data/logs/haproxy.loglocal4.* / data/logs/keepalived.log

Important note:

The field "* .info;" is deleted from the mail.none;authpriv.none;cron.none / var/log/messages line, and if it is not deleted, the access log is repeatedly recorded in the / var/log/messages file and / data/logs/haproxy.log.

The last two behaviors added manually, especially the last line "local4.* / data/logs/keepalived.log", match the keepalived log requirements, otherwise they won't work.

 configuration keepalived: there is no need to modify the configuration file keepalived.conf, just add the option "- S 4" when starting the service. So, how did I know to add this option? Execute the instruction keepalived-- help, and you can see the output item "- S,-- log-facility= [0-7] Set syslog facility to LOG_LOCAL [0-7]". When configuring rsyslog, "local4." is defined, so the option to start the keepalived service is "- S 4". The Keepalived startup command is as follows.

/ usr/local/keepalived/sbin/keepalived-D-d-S 4

For simplicity and versatility (suitable for all kinds of nix), I write it directly to the file / etc/rc.local.

 configures haproxy and sets log output. Add the line "log 127.0.0.1 local3" to the global configuration code block of the main configuration file.

 starts related services:

 starts rsyslog:service rsyslog restart

 starts haproxy: there is keepalived running again. As long as you kill the process with the instruction killall-9 haproxy, haproxy will be automatically restarted by keepalived.

 starts keepalived:/usr/local/keepalived/sbin/keepalived-D-d-S4. Verify the configuration

Change to the log storage directory / data/logs/, to see if there are two log files generated.

Manually bind the physical ip address of the load balancer, and then use the browser to access a hostname set by the load balancer to see if the log file / data/logs/haproxy.log generates new record lines. If it is as expected, proceed to the next step.

Restart or temporarily shut down the main load balancer system, so that all requests drift to the system that has set keepalived and haproxy logs, and use the instruction tail-f / data/logs/haproxy.log to check the log generation. If the scrolling screen is like a motor flying, dizzying, then it is done.

Finally, log the master load balancer in the same way.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report