yum install keepalived -y 单主模式 主:配置文件 ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id proxy1 ID号,每个机器的ID号不能一样 vrrp_mcast_group4 224.1.1.1 } vrrp_instance VI_1 { state MASTER 是否为主,MASTER是主,BACKUP是副 interface eth0 制定网卡 virtual_router_id 66 priority 100 优先级,数字越大优先级越高 advert_int 1 authentication { auth_type PASS auth_pass 123456 两个通信的密码 } virtual_ipaddress { 10.0.0.3 网站IP地址是多 } } 副配置文件: ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id proxy2 vrrp_mcast_group4 224.1.1.1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 66 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 10.0.0.3 } } 启动 systemctl start keepalived keepalived日志 启用keepalive日志功能 配置文件: vim /etc/sysconfig/keepalived 修改为日志级别3 KEEPALIVED_OPTIONS="-D -S 3" vim /etc/rsyslog.conf 添加以下内容 在最后没添加上即可 local3.* /var/log/keepalive.log 重启一下日志服务 systemctl restart rsyslog.service 再来重启一下keepalive服务. systemctl restart keepalived 实现keepalived脚本检测,nginx进程挂掉之后IP飘逸到备用服务器上 主服务器配置文件: ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 192.168.200.1 smtp_connect_timeout 30 ! router_id proxy1 ! vrrp_mcast_group4 224.1.1.1 } vrrp_script chk_nginx { script "/data/nginx_check.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 66 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 172.16.30.155 } track_script { chk_nginx } } 备用服务器配置文件: ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 192.168.200.1 smtp_connect_timeout 30 ! router_id proxy1 ! vrrp_mcast_group4 224.1.1.1 } vrrp_script chk_nginx { script "/data/nginx_check.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 66 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 172.16.30.155 } track_script { chk_nginx } } nginx检测脚本 #!/bin/bash ######################## CHECK_NUM=$(ps -ef|grep nginx|grep -v grep|grep -v check|wc -l) if [ $CHECK_NUM -eq 0 ];then systemctl stop keepalived fi 实现:keepalive 实现主主IP也就是双主 # vim keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id proxy1 vrrp_mcast_group4 224.1.1.1 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 66 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 10.0.0.3 } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 88 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 654321 } virtual_ipaddress { 10.0.0.4 } } vip地址不能一样,优先级不能一样,ID号不能一样 ,密码也不能一样 副主: # vim keepalived.conf notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id proxy2 vrrp_mcast_group4 224.1.1.1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 66 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 10.0.0.3 } } vrrp_instance VI_2 { state MASTE interface eth0 virtual_router_id 88 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 654321 } virtual_ipaddress { 10.0.0.4 } } vip地址不能一样,优先级不能一样,ID号不能一样 ,密码也不能一样 发邮件通知 示例通知脚本 vim /etc/keepalived/notify.sh #!/bin/bash # contact='root@localhost' notify() { mailsubject="$(hostname) to be $1, vip floating" mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed 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}" exit 1 ;; esac KeepAlived双主配置 脚本的调用方法: 在vrrp_instance VI_1 语句块最后面加下面行 notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" keepalive偏移,主变成备或者备变成主