时间:2021-07-01 10:21:17 帮助过:22人阅读
解压keepalived到/usr/local/并进入目录执行配置编译
# ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-431.5.1.el6.x86_64/ Keepalived configuration ------------------------ Keepalived version : 1.2.13 Compiler : gcc Compiler flags : -g -O2 Extra Lib : -lssl -lcrypto -lcrypt Use IPVS Framework : Yes IPVS sync daemon support : Yes IPVS use libnl : No fwmark socket support : Yes Use VRRP Framework : Yes Use VRRP VMAC : Yes SNMP support : No SHA1 support : No Use Debug flags : No # make && make install
默认情况下keepalived启动时会去/etc/keepalived目录下找配置文件,将需要的配置文件拷贝到指定位置
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ # cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ # cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ # cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ # chkconfig mysqld on # chkconfig keepalived on
ipvsadm下载地址
需要安装以下软件包
# yum install -y libnl* popt*
查看是否加载lvs模块
# modprobe -l |grep ipvs
解压安装
# ln -s /usr/src/kernels/2.6.32-431.5.1.el6.x86_64/ /usr/src/linux # tar -zxvf ipvsadm-1.26.tar.gz # make && make install
LVS安装完成,查看当前LVS集群
# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn
这里不再赘述,请参考MySQL复制
下面是LVS1节点(Keepalived主节点)上的Keepalived配置,LVS2类似
# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS1 } vrrp_instance VI_1 { state MASTER #指定instance初始状态,实际根据优先级决定.backup节点不一样 interface eth0 #虚拟IP所在网 virtual_router_id 51 #VRID,相同VRID为一个组,决定多播MAC地址 priority 100 #优先级,另一台改为90.backup节点不一样 advert_int 1 #检查间隔 authentication { auth_type PASS #认证方式,可以是pass或ha auth_pass 1111 #认证密码 } virtual_ipaddress { 192.168.1.100 #VIP } } virtual_server 192.168.1.100 3306 { delay_loop 6 #服务轮询的时间间隔 lb_algo wrr #加权轮询调度,LVS调度算法 rr|wrr|lc|wlc|lblc|sh|sh lb_kind DR #LVS集群模式 NAT|DR|TUN,其中DR模式要求负载均衡器网卡必须有一块与物理网卡在同一个网段 #nat_mask 255.255.255.0 persistence_timeout 50 #会话保持时间 protocol TCP #健康检查协议 ## Real Server设置,3306就是MySQL连接端口 real_server 192.168.1.5 3306 { weight 3 ##权重 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } real_server 192.168.1.6 3306 { weight 3 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
编写LVS启动脚本/etc/init.d/realserver
#!/bin/sh VIP=192.168.1.100 . /etc/rc.d/init.d/functions case "$1" in # 禁用本地的ARP请求、绑定本地回环地址 start) /sbin/ifconfig lo down /sbin/ifconfig lo up echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce /sbin/sysctl -p >/dev/null 2>&1 /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 up #在回环地址上绑定VIP,设定掩码,与Direct Server(自身)上的IP保持通信 /sbin/route add -host $VIP dev lo:0 echo "LVS-DR real server starts successfully.\n" ;; stop) /sbin/ifconfig lo:0 down /sbin/route del $VIP >/dev/null 2>&1 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce echo "LVS-DR real server stopped.\n" ;; status) isLoOn=`/sbin/ifconfig lo:0 | grep "$VIP"` isRoOn=`/bin/netstat -rn | grep "$VIP"` if [ "$isLoON" == "" -a "$isRoOn" == "" ]; then echo "LVS-DR real server has run yet." else echo "LVS-DR real server is running." fi exit 3 ;; *) echo "Usage: $0 {start|stop|status}" exit 1 esac exit 0
将lvs脚本加入开机自启动
# chmod +x /etc/init.d/realserver # echo "/etc/init.d/realserver" >> /etc/rc.d/rc.local
分别启动LVS和keepalived
# service realserver start # service keepalived start
注意此时网卡的变化,可以看到虚拟网卡已经分配到了realserver上。
此时查看LVS集群状态,可以看到集群下有两个Real Server,调度算法,权重等信息。ActiveConn代表当前Real Server的活跃连接数
# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.1.100:3306 wrr persistent 50 -> 192.168.1.5:3306 Route 3 4 1 -> 192.168.1.6:3306 Route 3 0 2
此时LVS+Keepalived+MySQL主主复制已经搭建完成。
关闭MySQL Server2
# service mysqld stop
在LVS1查看/var/log/messages中关于keepalived日志,LVS1检测到了MySQL Server2宕机,同时LVS集群自动剔除了故障节点
Sep 9 13:50:53 192.168.1.2 Keepalived_healthcheckers[18797]: TCP connection to [192.168.1.6]:3306 failed !!! Sep 9 13:50:53 192.168.1.2 Keepalived_healthcheckers[18797]: Removing service [192.168.1.6]:3306 from VS [192.168.1.100]:3306
从新启动MySQL Server2后自动将故障节点自动加入LVS集群
Sep 9 13:51:41 192.168.1.2 Keepalived_healthcheckers[18797]: TCP connection to [192.168.1.6]:3306 success. Sep 9 13:51:41 192.168.1.2 Keepalived_healthcheckers[18797]: Adding service [192.168.1.6]:3306 to VS [192.168.1.100]:3306
关闭LVS1上的Keepalived(模拟宕机操作),查看LVS1上的日志,可以看到Keepalived移出了LVS1上的VIP
Sep 9 14:01:27 192.168.1.2 Keepalived[18796]: Stopping Keepalived v1.2.13 (09/09,2014) Sep 9 14:01:27 192.168.1.2 Keepalived_healthcheckers[18797]: Removing service [192.168.1.5]:3306 from VS [192.168.1.100]:3306 Sep 9 14:01:27 192.168.1.2 Keepalived_healthcheckers[18797]: Removing service [192.168.1.6]:3306 from VS [192.168.1.100]:3306 Sep 9 14:01:27 192.168.1.2 Keepalived_vrrp[18799]: VRRP_Instance(VI_1) sending 0 priority Sep 9 14:01:27 192.168.1.2 Keepalived_vrrp[18799]: VRRP_Instance(VI_1) removing protocol VIPs.
同时查看LVS2上日志,可以看到LVS2成为了Master,并接管了VIP
Sep 9 14:11:24 192.168.1.11 Keepalived_vrrp[7457]: VRRP_Instance(VI_1) Transition to MASTER STATE Sep 9 14:11:25 192.168.1.11 Keepalived_vrrp[7457]: VRRP_Instance(VI_1) Entering MASTER STATE Sep 9 14:11:25 192.168.1.11 Keepalived_vrrp[7457]: VRRP_Instance(VI_1) setting protocol VIPs. Sep 9 14:11:25 192.168.1.11 Keepalived_vrrp[7457]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.100 Sep 9 14:11:25 192.168.1.11 Keepalived_healthcheckers[7456]: Netlink reflector reports IP 192.168.1.100 added Sep 9 14:11:25 192.168.1.11 avahi-daemon[1407]: Registering new address record for 192.168.1.100 on eth0.IPv4. Sep 9 14:11:30 192.168.1.11 Keepalived_vrrp[7457]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.100
在LVS2上查看LVS集群状态,一切正常。
# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.1.100:3306 wrr persistent 50 -> 192.168.1.5:3306 Route 3 2 0 -> 192.168.1.6:3306 Route 3 1 0
MySQL主主复制+LVS+Keepalived实现MySQL高可用性
标签:cas stopped 时间 openssl 存在 type netmask sed ast