????????????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
????LVS???
????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
????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
????LVS+Keepalived????
??????MySQL????????
?????????????????ο?MySQL????
????????Keepalived
??????????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
??????д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??????????????????????Server Array???????????????Real Server??
????LVS???????????????????????????????????Real Server????Real Server?????????????????????
????Keepalived??????LVS??????????????LVS???????????????????????????????л???????????