1、LoadBalance – LVS官网介绍2、LVS介绍LVS全称为 L inux V irtual S erver,LVS本质上是一个内模块(ip_vs.so),因为LVS的负载调度技术是在Linux内中实现。# 如果以下命令什么都没有显示,可能是你的lvs模块还没有加载, # 在运行keepaliaved之后就会出现。 [root@test02 mysql_data]#  modprobe -l | grep ipvs kernel/net/netfilter/ipvs/ip_vs.kokernel/net/netfilter/ipvs/ip_vs_rr.kokernel/net/netfilter/ipvs/ip_vs_wrr.kokernel/net/netfilter/ipvs/ip_vs_lc.kokernel/net/netfilter/ipvs/ip_vs_wlc.kokernel/net/netfilter/ipvs/ip_vs_lblc.kokernel/net/netfilter/ipvs/ip_vs_lblcr.kokernel/net/netfilter/ipvs/ip_vs_dh.kokernel/net/netfilter/ipvs/ip_vs_sh.kokernel/net/netfilter/ipvs/ip_vs_sed.kokernel/net/netfilter/ipvs/ip_vs_nq.kokernel/net/netfilter/ipvs/ip_vs_ftp.kokernel/net/netfilter/ipvs/ip_vs_pe_sip.koLVS的部署模式大致分成3种:1. NAT 2. DR(Director Server) 3. Tunnel我们常用的部署模式为 DR模式 ,且 DR模式的性能也是三者中最好的。3、LVS–DR模式的原理DR模式实现的三个关键点:(1)MAC地址替换(ARP 欺骗 ) 将发送给LVS 请求包的目标MAC地址替换为某一个( 由调度器据算法调度分配 ) Server的MAC地址 ; 然后将这 修改过目标MAC地址的请求包发送给对应的Server;(2)虚拟IP 虚拟IP存在于LVS上,用于接受用户的请求; 同时虚拟IP还绑定在 server 的 lo:0 上(loopback 口的别名 )假如没有绑定虚拟IP在 lo:0 上: 1. 结合 MAC地址替换 ,此时数据包调度给了某一个server; 2. server收到这个数据包后,首先 检查目标MAC是否是自己 的,由于之前做了MAC地址替换,所以检查的结果为真; 3. 然后检查目标IP ,由于 虚拟IP没有绑定在lo:0 上,即检查结果为假,则server就会丢弃该数据包。(3)lo:0接口的ARP抑制 由于server上绑定虚拟IP在lo:0上,那此时看来,同一个网络(VLAN)中存在多个同的IP,那不就IP地址冲突了么IP是否冲突主要看在一个网络(VLAN)内询问某一个IP地址是否有多台主机应答(ARP 广播 );在server的 lo:0 上启用ARP抑制 ,这一来当网络中有设备询问虚拟IP地址 时, server将不做应答 , 此时只有LVS进行应答 ,确保不会引起冲突( 即 IP-MAC 的映射关系在该网络中唯一 )4、Keepalived/LVS的配置(1) Keepalived由于LVS本质上是一个内模块,所以我们需要借助一些 用户层工具 去配置管理,主要有以下两个工具:1. keepalived2. ipvsadm以上两种工具都是用来配置LVS 的,大致区别如下1. ipvsadm是命令行工具 ,每次重启都需要重新配置 (或者写入开机脚本中),但是配置参数十分灵活; 2. keepalived 是一个服务(Daemon),通过编辑配置文件( /etc/keepalived/keepalived.conf ),可以很方便的对LVS的参数做调整,且系统重启后不会丢失; 3. Keepalived 还有 多机热备 的功能( VRRP 协议 ),可以将多台服务器组成Master-Slave集群,通过虚拟IP的方式对外提供服务,使得LVS不会成为单点故障; 4. Keepalived 还可以配置后端server检测,当发现服务不可用时,将该server从转发列表中剔除,具有健康检查机制;我们这里选择使用Keepalived对LVS进行配置。(2)安装keepalivedyum install -y keepalived 这里我配置keepalive的高可用(2台服务器)ip1  masterip2  backup(3)配置keepalived#master配置如下:/etc/keepalived/keepalived.conf ! Configuration File for keepalivedglobal_defs {    notification_email {              }     notification_email_from admin@test.com    smtp_server 127.0.0.1    smtp_connect_timeout 30    router_id LVS_MASTER}vrrp_instance VI_1 {    state MASTER    interface eth1    virtual_router_id 60    priority 100    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {        192.168.175.200    }}virtual_server 192.168.175.200 3306 {    delay_loop 6    lb_algo rr     lb_kind DR    nat_mask 255.255.255.0    persistence_timeout 0    protocol TCP    real_server 192.168.175.17 3306 {        weight 1        TCP_CHECK {            connect_timeout 3            nb_get_retry 3            delay_before_retry 3            connect_port 3306        }    }    real_server 192.168.175.18 3306 {        weight 1        TCP_CHECK {            connect_timeout 3            nb_get_retry 3            delay_before_retry 3            connect_port 3306        }    }        real_server 192.168.175.19 3306 {        weight 3        TCP_CHECK {            connect_timeout 3            nb_get_retry 3            delay_before_retry 3            connect_port 3306        }    }}#backup配置如下:/etc/keepalived/keepalived.conf ! Configuration File for keepalivedglobal_defs {    notification_email {              }     notification_email_from admin@test.com    smtp_server 127.0.0.1    smtp_connect_timeout 30    router_id LVS_MASTER}vrrp_instance VI_1 {    state BACKUP     interface eth1    virtual_router_id 60    priority 90    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {        192.168.175.200    }}virtual_server 192.168.175.200 3306 {    delay_loop 6    lb_algo rr     lb_kind DR    nat_mask 255.255.255.0    persistence_timeout 0    protocol TCP    real_server 192.168.175.17 3306 {        weight 1        TCP_CHECK {            connect_timeout 3            nb_get_retry 3            delay_before_retry 3            connect_port 3306        }    }    real_server 192.168.175.18 3306 {        weight 1        TCP_CHECK {            connect_timeout 3            nb_get_retry 3            delay_before_retry 3            connect_port 3306        }    }        real_server 192.168.175.19 3306 {        weight 3        TCP_CHECK {            connect_timeout 3            nb_get_retry 3            delay_before_retry 3            connect_port 3306        }    }}(4)配置realserver.shvim /usr/local/real_server.sh#!/bin/bashSNS_VIP=192.168.175.200case "$1" instart) 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/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/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce echo "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1esacexit 0#添加权限并启动[所有slave上面都要执行]chmod +x /usr/local/real_server.shsh /usr/local/real_server.sh start注意:将以上脚本保存成realserver.sh ,将该 脚本配合虚拟IP加入到开机脚本中,以便开机可以自动运行。echo "sh /usr/local/bin/realserver.sh  start" >> /etc/rc.local 可以看到vip[root@test02 bin]# ip a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00    inet 127.0.0.1/8 scope host lo    inet 192.168.175.200/32 brd 192.168.175.200 scope global lo:0    inet6 ::1/128 scope host        valid_lft forever preferred_lft forever2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 00:0c:29:5a:f9:7d brd ff:ff:ff:ff:ff:ff    inet 192.168.175.17/24 brd 192.168.175.255 scope global eth1    inet6 fe80::20c:29ff:fe5a:f97d/64 scope link        valid_lft forever preferred_lft forever3: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN     link/ether ee:01:11:4b:3b:b7 brd ff:ff:ff:ff:ff:ff(5)分别启动master及backup的keepalive/etc/init.d/keepalived  start[root@test03 ~]# tail -f /var/log/messages Aug 25 19:21:24 test03 Keepalived_healthcheckers[6887]: Registering Kernel netlink reflectorAug 25 19:21:24 test03 Keepalived_healthcheckers[6887]: Registering Kernel netlink command channelAug 25 19:21:24 test03 Keepalived_healthcheckers[6887]: Opening file '/etc/keepalived/keepalived.conf'.Aug 25 19:21:24 test03 Keepalived_healthcheckers[6887]: Configuration is using : 17687 BytesAug 25 19:21:24 test03 kernel: IPVS: ipvs loaded.Aug 25 19:21:24 test03 kernel: IPVS: [rr] scheduler registered.Aug 25 19:21:24 test03 Keepalived_healthcheckers[6887]: Using LinkWatch kernel netlink reflector...Aug 25 19:21:24 test03 Keepalived_healthcheckers[6887]: Activating healthchecker for service [192.168.175.17]:3306Aug 25 19:21:24 test03 Keepalived_healthcheckers[6887]: Activating healthchecker for service [192.168.175.18]:3306Aug 25 19:21:24 test03 Keepalived_healthcheckers[6887]: Activating healthchecker for service [192.168.175.19]:33065、 LVS与MHA集群测试主要测试keepalive的负载均衡[root@test05 bin]# mysql -u root -p1 -h 192.168.175.200 -P3306  -e "select @@hostname" +------------+| @@hostname |+------------+| test02     |+------------+[root@test05 bin]# mysql -u root -p1 -h 192.168.175.200 -P3306  -e "select @@hostname" +------------+| @@hostname |+------------+| test04     |+------------+[root@test05 bin]# mysql -u root -p1 -h 192.168.175.200 -P3306  -e "select @@hostname" +------------+| @@hostname |+------------+| test04     |+------------+[root@test05 bin]# mysql -u root -p1 -h 192.168.175.200 -P3306  -e "select @@hostname" +------------+| @@hostname |+------------+| test02     |+------------+[root@test05 bin]# mysql -u root -p1 -h 192.168.175.200 -P3306  -e "select @@hostname" +------------+| @@hostname |+------------+| test04     |+------------+[root@test05 bin]# mysql -u root -p1 -h 192.168.175.200 -P3306  -e "select @@hostname" +------------+| @@hostname |+------------+| test03     |+------------+6、测试keepalive的高可用关掉其中一个keepalive测试即可[这里就不测试了]