IP

主机名

节点类型

192.168.100.61

server1

master

192.168.100.62

server2

backup

建议关闭防火墙和selinux学习目标: 1.部署keepalived; 2.配置keepalived日志; 3.配置自定义监测httpd服务脚本;

相关内容推荐

  • keepalived+LVS搭建

一、安装keepalived和httpd(两个节点都需要执行)

[root@server1 ~]# yum -y install keepalived httpd

二、修改keepalived配置文件master节点:

[root@server1 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {     //设置健康检查脚本
	script "/usr/local/sbin/chk_httpd.sh"
	inverval 3     //3秒执行一次健康检查
}

vrrp_instance VI_1 {
    state MASTER   //节点名称
    interface ens33   //用哪个网卡监听VIP (" Virtual IP ",即浮动IP)
					  //以实际情况修改,可能会不一样
					  //可以使用“ ip a ”命令查看自己所拥有的网卡名
    virtual_router_id 51
    priority 100     //节点的权重,值越大权重越大值最大的为master节点
    				 //如果值变得比backup节点小就会变成backup节点,而backup节点就会升级成master节点
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.100.16   //自定义VIP  
    }

    track_script {
	chk_httpd	//定义监控脚本,上面设置健康检查脚本的名称,需要和上面保持一致
    }
}

backup节点:

[root@server2 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
	script "/usr/local/sbin/chk_httpd.sh"
	inverval 3
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90   		 //权重需要比master节点小
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.100.16
    }

    track_script {
	chk_httpd	
    }
}

三、创建健康检查脚本并授执行权限 (以下两个节点都需要执行)

# vim /usr/local/sbin/chk_httpd.sh 

#!/bin/bash

d=`date --date today +%Y%m%d_%H:%M:%S`
n=`netstat -lntp | grep httpd | wc -l`

if [ $n -eq 0 ]; then
	systemctl start httpd
	n2=`netstat -lntp | grep httpd | wc -l`
	if [ $n2 -eq 0 ]; then
		echo "$d httpd down,keepalived will stop" >> /var/log/check_ng.log
		systemctl stop keepalived
	fi
fi

授权

# chmod 755 /usr/local/sbin/chk_httpd.sh

四、设置日志系统(两个节点都设置,以便日后运维排错)

# vim /etc/sysconfig/keepalived 

KEEPALIVED_OPTIONS="-D -d -S 0"
# vim /etc/rsyslog.conf

//找到此行
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
//在下面添加此行
local0.* /var/log/keepalived.log

五、重启rsyslog和keepalived生效配置(两个节点都执行)

# systemctl restart rsyslog
# systemctl restart keepalived

六、测试

关闭server1的keepalived,测试VIP是否会跳到backup节点

keepalived 没有日志 keepalived日志配置_vim

查看server2的网卡信息,查看VIP是否已经转到server2

keepalived 没有日志 keepalived日志配置_linux_02

查看两个主机的日志,可以看到刚刚主机的活动过程,这里使用“ tail -f [文件名] ”可实施查看日志的更新

keepalived 没有日志 keepalived日志配置_vim_03

keepalived 没有日志 keepalived日志配置_vim_04

配置告警系统(两个节点都配置邮件告警)

一、安装邮件服务

[root@server1 ~]# yum -y install mailx sendmail

修改邮件服务配置文件

[root@server1 ~]# vim /etc/mail.rc 
##在配置文件中添加以下参数
set from=XXXXXXXX@qq.com  //邮箱账号
set smtp=smtp.qq.com
set smtp-auth-user=XXXXXXX@qq.com     //邮箱账号
set smtp-auth-password=XXXXXXXXXXX   //邮箱smtp验证码
set smtp-auth=login

二、编写邮件通知脚本

[root@server1 ~]# vim  /etc/keepalived/notify.sh 
#!/bin/bash
contact='123456789@qq.com'
notify() {
	mailsubject="$(hostname) to be $1 ,vip转移"
	mailbody="$(date +'%F %T'): vrrp trasition, $(hostname) change to be $1"
	echo " $mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
	notify master
	;;
backup)
	notify backup
	;;
fault)
	notify fault
	;;
*)
	echo "Usage: $(basename $0) {master|backup|fault}"
	ecit 1
	;;
esac

三、开启触发动作

[root@server1 ~]# vim /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.100.16
    }

    track_script {
    chk_httpd
    }

##添加以下参数
notify_master "/bin/bash /etc/keepalived/notify.sh master"  //当此节点成为master的时候执行后面的动作
notify_backup "/bin/bash /etc/keepalived/notify.sh backup" //当此节点成为backup的时候执行后面的动作
notify_fault "/bin/bash /etc/keepalived/notify.sh fault" //当此节点状态报错的时候执行后面的动作
}

四、测试 关闭server节点的keepalived

[root@server1 ~]# systemctl stop keepalived

qq邮箱收到邮件,提示server2已经成为新的节点

keepalived 没有日志 keepalived日志配置_linux_05