时间:2021-07-01 10:21:17 帮助过:30人阅读
create table test( id int, name varchar(10) ); -- A中插入 insert into test values(1,‘A‘); -- B中插入 insert into test values(2,‘B‘);
(2)编辑realserver脚本文件
①进入指定文件夹:cd /etc/init.d/
②创建脚本文件:vi realserver
SNS_VIP=192.168.5.200 /etc/rc.d/init.d/functions case "$1" in start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_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) ifconfig lo:0 down route del $SNS_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
这里我们设置虚拟IP为:192.168.5.200
③保存脚本文件后更改该文件权限:chmod 755 realserver
④开启realserver服务:service realserver start
关于keepalived相关知识,请参考:http://bbs.nanjimao.com/thread-855-1-1.html
(1)安装Keepalived相关包
wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
tar -zxvf keepalived-1.2.13.tar.gz
cd keepalived-1.2.13
./configure --disable-fwmark --prefix=/usr/local/keepalived
make && make install
(2)编辑keepalived.conf配置文件
①进入keepalived.conf所在目录:cd /etc/keepalived
②首先清除掉keepalived原有配置:> keepalived.conf
③重新编辑keepalived配置文件:vi keepalived.conf
global_defs { router_id LVS_DEVEL # 设置lvs的id,在一个网络内应该是唯一的 } vrrp_instance VI_1 { state MASTER #指定Keepalived的角色,MASTER为主,BACKUP为备 interface eth1 #指定Keepalived的角色,MASTER为主,BACKUP为备 virtual_router_id 51 #虚拟路由编号,主备要一致 priority 100 #定义优先级,数字越大,优先级越高,主DR必须大于备用DR advert_int 1 #检查间隔,默认为1s authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.5.200 #定义虚拟IP(VIP)为192.168.2.33,可多设,每行一个 } } # 定义对外提供服务的LVS的VIP以及port virtual_server 192.168.5.200 3306 { delay_loop 6 # 设置健康检查时间,单位是秒 lb_algo wlc # 设置负载调度的算法为wlc 基于权重的调度算法 lb_kind DR # 设置LVS实现负载的机制,有NAT、TUN、DR三个模式 nat_mask 255.255.255.0 #persistence_timeout 0 会话保持时间 protocol TCP real_server 192.168.5.122 3306 { # 指定real server1的IP地址 weight 3 # 配置节点权值,数字越大权重越高 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } real_server 192.168.5.123 3306{ # 指定real server2的IP地址 weight 3 # 配置节点权值,数字越大权重越高 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
注意:会话保持时间要注释掉,这样可以看出负载均衡的调度
(3)开启keepalived服务
service keepalived start
停止命令
service keepalived stop
查看状态
service keepalived status
检测:运行命令ipvsadm
从负载服务器与主负载服务器大致相同,只是在keepalived的配置文件中需要改以下两处:
(1)将state由MASTER改为BACKUP
(2)将priority由100改为99
vrrp_instance VI_1 { state BACKUP # 这里改为BACKUP interface eth1 virtual_router_id 51 priority 99 # 这里改为99,master优先级是100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.5.200 } }
在客户端上登录mysql,客户端机器是120,121,122,123之外的机器
mysql -h192.168.5.200 -uroot –pbruce -P3306 –Dbruce
因为mysql是长连接,所以测试时需要开启多个命令窗口进行连接
发现请求被平均分配到A和B服务器
LVS+Keepalived实现mysql的负载均衡
标签:virt 瓶颈 转换 def cti 算法 ip绑定 数据库服务器 class