一、安装keepalived和ipvsadm
注意:ipvsadm并不是lvs,它只是lvs的配置工具。
为了方便起见,在这里我们使用yum的安装方式
分别在10.18.1.140和10.18.1.141两台主机上安装keepalived和ipvsadm
# yum install -y keepalived ipvsadm
安装完成后查看版本号是否一致 keepalived --version ipvsadm --version
主机配置文件
! Configuration File for keepalived
global_defs {
# notification_email {
# acassen@firewall.loc
# failover@firewall.loc
# sysadmin@firewall.loc
# }
# notification_email_from Alexandre.Cassen@firewall.loc
# smtp_server 192.168.200.1
# smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth2
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.18.1.146
}
}
virtual_server 10.18.1.146 8066 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 10.18.1.143 8066 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 8066
}
}
real_server 10.18.1.143 8066 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 8066
}
}
}
从机配置文件
! Configuration File for keepalived
global_defs {
# notification_email {
# acassen@firewall.loc
# failover@firewall.loc
# sysadmin@firewall.loc
# }
# notification_email_from Alexandre.Cassen@firewall.loc
# smtp_server 192.168.200.1
# smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth2
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.18.1.146 #测试过程中发现,该虚拟ip只有和真实ip在同一网段,才好用
}
}
virtual_server 10.18.1.146 8066 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 10.18.1.143 8066 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 8066
}
}
real_server 10.18.1.143 8066 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 8066
}
}
}
注意:ipvsadm并不是lvs,它只是lvs的配置工具
LVS由两部分组成,包括ipvs和ipvsadm:
1.ipvs:ipvs是工作在内核空间netfilter的input链上的框架,通过用户空间工具进行管理,是真正生效实现调度的代码。
2.ipvsadm:ipvsadm负责为ipvs内核框架编写规则,管理配置内核中ipvs程序的用户空间的管理工具
二、启动两台机器的keepalived服务
1.分别在两台机器执行命令 service keepalived start
2.通过ipvsadm -L命令查看VIP是否能成功映射到后端mycat服务。如果失败了,可通过/var/log/messages日志定位keepalived启动失败的原因。
三、安装Mycat
- 解压Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz到/usr/local/目录下
- 配置schema.xml rule.xml server.xml
- 编写脚本realserver.sh,如下:
#!/bin/bash
#description: Config realserver
VIP=10.18.1.146
/etc/rc.d/init.d/functions
case "$1" in
start)
/sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
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
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
/sbin/ifconfig lo:0 down
/sbin/route del $VIP >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
- 如果/etc/rc.d/init.d/functions报错执行chmod 777 /etc/rc.d/init.d/functions
- 执行脚本 sh realserver.sh start,绑定到指定VIP(10.18.1.146)上
- 启动mycat,执行/bin/local/mycat/bin/mycat start
四、最后验证
分别在keepalived的两台机器上执行ipvsadm -L查看LVS规则是否正确,如下是连接两个mycat规则
在客户机10.18.1.138机器上执行“mysql -h10.18.1.146 -P8066 -uvps -pvps -DTESTSMARTBUS -e 'select @@hostname'”查看是否理想情况一致(负载规则轮询)。