小知识:RHEL7上设置Keepalived日志重定向

之前在MySQL主主+Keepalived架构安装部署也有描述Keepalived日志重定向的部分,其中提到RHEL7(实际是RHEL6.8以上)要配置rsyslog,回顾下之前的配置,主要就是改两个地方:

1.配置 /etc/sysconfig/keepalived 文件

vi /etc/sysconfig/keepalived
修改KEEPALIVED_OPTIONS的值:

# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp               -P    Only run with VRRP subsystem.
# --check              -C    Only run with Health-checker subsystem.
# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
# --dump-conf          -d    Dump the configuration data.
# --log-detail         -D    Detailed log messages.
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)
#

# KEEPALIVED_OPTIONS="-D"
KEEPALIVED_OPTIONS="-D -d -S 0"

2.添加keepalived日志保存位置的配置

因为第一步相当设置了log-facility=0,(-S 0),那么添加local0的日志都重定向到/var/log/keepalived.log中:

vi /etc/rsyslog.conf

..这里省略源文件内容.. 最后追加
# keepalived -S 0
local0.* /var/log/keepalived.log

但是实际验证发现,如果对于RHEL7,这样设置还不行,实际观察ps -ef|grep keep发现keepalived的进程还是KEEPALIVED_OPTIONS="-D"方式运行的,我们设置的KEEPALIVED_OPTIONS="-D -d -S 0"并未生效,大概怀疑到是RHEL7服务管理与RHEL6有较大区别,从这方面去找原因,以RHEL7为关键字进行相关问题搜索,最终发现RHEL7的确还需多做一步如下操作,才可以保证读取到我们上面的环境配置文件:

3.修改 /lib/systemd/system/keepalived.service 文件

将配置文件中的这两行:

EnvironmentFile=-/usr/local/keepalived/etc/sysconfig/keepalived
ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS

重新修改为:

EnvironmentFile=/etc/sysconfig/keepalived
ExecStart=/sbin/keepalived $KEEPALIVED_OPTIONS

可以看到这里/sbin/keepalived $KEEPALIVED_OPTIONS定义的变量是通过EnvironmentFile定义的配置文件去找的。

修改完成后,需要注意重新加载下service,否则后面启动keepalived服务会报错:

systemctl daemon-reload

最后重启rsyslog和keepalived服务,就可以实现RHEL7上Keepalived日志重定向到/var/log/keepalived.log了。

This entry was posted in MySQL安装部署 and tagged , , . Bookmark the permalink.