keepalived:vrrp协议 双主模型
第一步配置出始环境: 准备两节点: 主节点:172.18.57.7 long1 备节点:172.18.57.8 long2 (1) 各节点时间必须同步; 同步时间: ~]# yum -y install chrony ~]# vim /etc/chrony.conf ~]# systemctl start chronyd ~]# chronyc sources ~]# chronyc sourcestats (2) 确保iptables及selinux不会成为阻碍; 清空防火墙: ~]# iptables -F 关闭SELinux ~]# setenforce 0 清空规则: ~]# ipvsadm -C 重新启动网络: ~]#systemctl restart network (3) 各节点之间可通过主机名互相通信(对KA并非必须); 修改域名解析使用/etc/hosts文件实现; ~]#vim /etc/hosts 172.18.57.7 long1 172.18.57.8 long2 第二步:keepalived安装 long1 ~]#yum -y install keepalived long2 ~]#yum -y install keepalived
第三步:修改配置文件 [root@long1 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id long1 vrrp_mcast_group4 224.0.101.57 } vrrp_instance myr1 { state MASTER interface ens33 virtual_router_id 57 priority 100 advert_int 1 authentication { auth_type PASS auth_pass dAcLuTSt } virtual_ipaddress { 172.18.57.57/16 dev ens33 } } vrrp_instance myr2 { state BACKUP interface ens33 virtual_router_id 58 priority 98 advert_int 1 authentication { auth_type PASS auth_pass dAcLUTSt } virtual_ipaddress { 172.18.57.157/16 dev ens33 } } [root@long2 ~]#vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id long2 vrrp_mcast_group4 224.0.101.57 } vrrp_instance myr1 { state BACKUP interface ens33 virtual_router_id 57 priority 98 advert_int 1 authentication { auth_type PASS auth_pass dAcLuTSt } virtual_ipaddress { 172.18.57.57/16 dev ens33 } } vrrp_instance myr2 { state MASTER interface ens33 virtual_router_id 157 priority 100 advert_int 1 authentication { auth_type PASS auth_pass dAcLUTSt } virtual_ipaddress { 172.18.57.157/16 dev ens33 } }
第四步:启动服务,查看状态 [root@long1 ~]#systemctl start keepalived.service [root@long2 ~]#systemctl start keepalived.service [root@long1 ~]# systemctl status keepalived.service [root@long2 ~]# systemctl status keepalived.service [root@long1 ~]# tcpdump -i ens33 host 224.0.101.57
模拟宕机long1: [root@long1 ~]# iptables -A OUTPUT -s 172.18.57.7 -d 224.0.101.57 -j REJECT [root@long1 ~]# systemctl status keepalived.service [root@long1 ~]# iptables -F
实验节点ip发生转移是发送邮件:通知脚本 [root@long1 ~]#systemctl stop keepalived [root@long2 ~]#systemctl stop keepalived [root@long1 ~]# vim /etc/keepalived/notify.sh #!/bin/bash # contact='root@localhost' notify () { mailsubject="vrrp:$(hostname) to be $1" mailbody="$(hostname) to be $1, vrrp transition,$(date)." 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 [root@long1 ~]# chmod +x notify.sh [root@long1 ~]# vim /etc/keepalived/keepalived.conf notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" [root@long2 ~]# vim /etc/keepalived/notify.sh #!/bin/bsah # contact='root@localhost' notify () { mailsubject="vrrp:$(hostname) to be $1" mailbody="$(hostname) to be $1, vrrp transition,$(date)." 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 [root@long2 ~]# vim /etc/keepalived/keepalived.conf notify_master "/etc/keepalived/notify.sh master" notify_backup "/etc/keepalived/notify.sh backup" notify_fault "/etc/keepalived/notify.sh fault" [root@long2 ~]#systemctl start keepalived [root@long2 ~]#mail [root@long2 keepalived]#mail Heirloom Mail version 12.5 7/5/10. Type ? for help. "/var/spool/mail/root": 2 messages 1 new 2 unread >N 1 root Sun May 14 20:53 19/684 "vrrp: long2 to be backup" N 2 root Sun May 14 20:53 18/674 "vrrp: long2 to be master" [root@long1 ~]#systemctl start keepalived [root@long1 ~]#mail Heirloom Mail version 12.5 7/5/10. Type ? for help. "/var/spool/mail/root": 2 messages 2 new >N 1 root Sun May 14 20:53 18/674 "vrrp: long1 to be backup" N 2 root Sun May 14 20:53 18/674 "vrrp: long1 to be master" [root@long2 ~]#mail Heirloom Mail version 12.5 7/5/10. Type ? for help. "/var/spool/mail/root": 3 messages 1 new 3 unread U 1 root Sun May 14 20:53 19/684 "vrrp: long2 to be backup" U 2 root Sun May 14 20:53 19/684 "vrrp: long2 to be master" >N 3 root Sun May 14 20:54 18/674 "vrrp: long2 to be backup"
原创文章,作者:CL80516000,如若转载,请注明出处:http://www.178linux.com/75758