keepalived 安装和使用
1. Keepalived介绍
Keepalived的作用是检测服务器的状态,如果有一台服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其它服务器代替该服务器的工作,当服务器工作 正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。
2. Keepalived安装
yum install -y keepalived
3.安装完成后编辑配置文件
#打开 /etc/keepalived/keepalived.conf
vim /etc/keepalived/keepalived.conf
4.编辑MASTER配置文件
! Configuration File for keepalived global_defs { notification_email { www@qq.com #配置收的邮件 } notification_email_from www@qq.com #配置发送邮箱的邮件 smtp_server smtp.qq.com ## 邮箱服务器 smtp_connect_timeout 30 router_id app_router vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_haproxy { script "/etc/keepalived/haproxy_check.sh" ## 检测 haproxy 状态的脚本路径 interval 2 ## 检测时间间隔 weight 2 ## 如果条件成立,权重+2 } vrrp_instance VI_1 { state MASTER ##配置主机 interface ens33 ##配置网卡 virtual_router_id 51 priority 200 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.31.110 } track_script { haproxy_check } }
4.同时添加 检测脚本 /etc/keepalived/haproxy_check.sh
#!/bin/bash START_HAPROXY="/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg" #haproxy启动命令 LOG_FILE="/usr/local/keepalived/log/haproxy-check.log" # 日志文件 HAPS=`ps -C haproxy --no-header |wc -l` # 检测haproxy的状态,0代表未启动,1已经启动 date "+%Y-%m-%d %H:%M:%S" >> $LOG_FILE #在日志文件当中记录检测时间 echo "check haproxy status" >> $LOG_FILE # 记录haproxy的状态 if [ $HAPS -eq 0 ];then #执行haproxy判断 echo $START_HAPROXY >> $LOG_FILE #记录启动命令 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg #启动haproxy sleep 3 if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then echo "start haproxy failed, killall keepalived" >> $LOG_FILE killall keepalived service keepalived stop fi fi
5.编辑BACKUP配置文件
! Configuration File for keepalived global_defs { notification_email { www@qq.com ##配置接受邮箱 } notification_email_from www@qq.com ##配置发送邮箱的邮件 smtp_server smtp.qq.com smtp_connect_timeout 30 router_id app_router vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_haproxy { script "/etc/keepalived/haproxy_check.sh" ## 检测 haproxy 状态的脚本路径 interval 2 ## 检测时间间隔 weight 2 ## 如果条件成立,权重+2 } vrrp_instance VI_1 { state BACKUP #配置备机 interface ens33 #配置网卡 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.31.110 } track_script { haproxy_check } }
6.同时添加以上同样的检测脚本
#!/bin/bash START_HAPROXY="/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg" #haproxy启动命令 LOG_FILE="/usr/local/keepalived/log/haproxy-check.log" # 日志文件 HAPS=`ps -C haproxy --no-header |wc -l` # 检测haproxy的状态,0代表未启动,1已经启动 date "+%Y-%m-%d %H:%M:%S" >> $LOG_FILE #在日志文件当中记录检测时间 echo "check haproxy status" >> $LOG_FILE # 记录haproxy的状态 if [ $HAPS -eq 0 ];then #执行haproxy判断 echo $START_HAPROXY >> $LOG_FILE #记录启动命令 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg #启动haproxy sleep 3 if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then echo "start haproxy failed, killall keepalived" >> $LOG_FILE killall keepalived service keepalived stop fi fi
当停掉MASTER中的 haproxy服务的时候 ,数据库连接丝毫不受影响
至此keepalived 搭建完成!!