IP负载均衡(LVS)

DR模式负载均衡、keepalived、lvs高可用

1.配置均衡负载

机器 IP
server1 172.25.14.1
server2 172.25.14.2
server3 172.25.14.3

server1

yum install -y ipvsadm
ip addr add 172.25.14.100/24 dev eth0
ipvsadm -A -t 172.25.14.100:80 -s rr
ipvsadm -a -t 172.25.14.100:80 -r 172.25.14.2:80 -g
ipvsadm -a -t 172.25.14.100:80 -r 172.25.14.3:80 -g
ipvsadm -l
ipvsadm -ln

IP负载均衡(LVS)
IP负载均衡(LVS)

server2

yum install -y httpd
echo server2 > /var/www/html/index.html
systemctl start httpd

server3

yum install -y httpd
echo server3 > /var/www/html/index.html
systemctl start httpd

IP负载均衡(LVS)

物理机进行curl 172.25.14.100
IP负载均衡(LVS)
因为server2,server3上没有ip172.25.14.100,所以无结果。
此时server1上显示server2,server3各有俩次访问
IP负载均衡(LVS)

给server2,server3添加vip 172.25.14.100
IP负载均衡(LVS)
此时物理机再次访问curl 172.25.14.100,server2、server3各访问5次
IP负载均衡(LVS)
物理机切换到root用户

arp -d 172.25.14.100									#删除vip
for i in {1..10};do curl 172.25.14.100;done 			#再次访问,只访问server3

IP负载均衡(LVS)

server2、server3安装arptables,设置策略,使得物理机访问时禁止掉其上的172.25.14.100

yum install -y arptables.x86_64

IP负载均衡(LVS)
server2

arptables -A INPUT -d 172.25.14.100 -j DROP
arptables -A OUTPUT  -s 172.25.14.100 -j mangle --mangle-ip-s 172.25.14.2
arptables-save 
arptables-save > /etc/sysconfig/arptables 
arptables -nL
systemctl restart arptables.service 
scp /etc/sysconfig/arptables root@172.25.14.3://etc/sysconfig/arptables

IP负载均衡(LVS)

server3

cd /etc/sysconfig/
vim arptables 
systemctl restart arptables.service 
arptables -nL

IP负载均衡(LVS)
物理机再次删掉vip100并重新ping,mac地址显示如下
IP负载均衡(LVS)
server1的mac地址如下
IP负载均衡(LVS)
物理机再次访问到调度器
IP负载均衡(LVS)

2.keepalived

server1

yum install -y keepalived
yum install -y mailx

vim /etc/keepalived/keepalived.conf  #编辑配置文件

IP负载均衡(LVS)
IP负载均衡(LVS)

删除server1上的vip
IP负载均衡(LVS)
清除策略,重启keepalived,vip再次出现在server1上
IP负载均衡(LVS)
IP负载均衡(LVS)

关闭server3的apache
IP负载均衡(LVS)
物理机访问
IP负载均衡(LVS)

server1上出现邮件,报道server3down掉

IP负载均衡(LVS)
server3重启apache服务
物理机再次访问
IP负载均衡(LVS)
server1上出现第二封邮件,显示server3 up
IP负载均衡(LVS)

3.lvs高可用

增加server4:172.25.14.4

yum install -y keepalived
yum install -y ipvsadm.x86_64

修改keepalived配置文件

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     	root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server  127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 14
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       172.25.14.100 
    }
}

virtual_server 172.25.14.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    #persistence_timeout 50
    protocol TCP

    real_server 172.25.14.2 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.25.14.3 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

开启keepalived服务,便可显示出ipvsadm策略

systemctl start keepalived.service
ipvsadm -ln

IP负载均衡(LVS)

server1关闭keepalived
IP负载均衡(LVS)
物理机访问curl 172.25.14.100
查看server4日志,显示由BACKUP变为MASTER
IP负载均衡(LVS)
server1开启keepalived后查看server4日志,由MASTER 变为BACKUP
IP负载均衡(LVS)
server1的日志显示变为MASTER
IP负载均衡(LVS)

上一篇:七章——Keepalived双机热备(应用——linux防护与群集)


下一篇:mycat高可用-安全管理-监控 看这一篇就够了