linux下LVS+keepalives负载均衡高可用服务器配置

IP说明:

    lvs:10.10.10.3

    lvs(备):10.10.10.4

    virtual:10.10.10.100

1 lvs和 lvs(备)上建目录并将所需软件包放入
 
#mkdir /usr/local/src/lvs
 
2 lvslvs(备)上安装LVS和Keepalived
Lvs的安装
#tar zxvf ipvsadm-1.24.tar.gz
#cd ipvsadm-1.24
#make && make install
 
Keepalived的安装
#tar zxvf keepalived-1.1.15.tar.gz
#cd keepalived-1.1.15
#./configure && make && make install
 
keepalived做成启动脚务,方便管理
#cp/usr/local/etc/rc.d/init.d/keepalived  /etc/rc.d/init.d/
#cp /usr/local/etc/sysconfig/keepalived  /etc/sysconfig/
#mkdir /etc/keepalived
#cp/usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
#cp /usr/local/sbin/keepalived /usr/sbin/
#service keepalived start|stop     
 
3web1、web2服务器上绑定虚拟IP
vim /etc/sysctl.conf
写入  net.ipv4.conf.lo.arp_ignore = 1
        net.ipv4.conf.lo.arp_announce = 2
       net.ipv4.conf.all.arp_ignore = 1
       net.ipv4.conf.all.arp_announce = 2
       [code]
添加虚拟 ip地址
#cd /etc/sysconfig/network-scripts/
#ls
#cp ifcfg-lo ifcfg-lo:0
#vim ifcfg-lo:0
 
#service network restart
#service httpd start
 
4在主从负载均衡服务器上配置 keepalived.conf实现负载均衡和和高可用性
 
#vim /etc/keepalived/keepalived.conf
编缉 keepalived.conf文件,内容如下
 
! Configuration File for keepalived
 
global_defs {
    notification_email {
          [email protected]       \\接收服务器通告邮箱
    }
    notification_email_from [email protected]
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}
 
 
 
vrrp_instance VI_1 {
     state MASTER            \\备份服务器上将 MASTER改为BACKUP  
     interface eth0
     virtual_router_id 51
     priority 100            \\备份服务上将 100改为99
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     virtual_ipaddress {
         10.10.10.100         \\(如果有多个 VIP,继续换行填写.)
     }
}
 
 
virtual_server 10.10.10.100 80 {
     delay_loop 6              \\(每隔 10秒查询realserver状态)
     lb_algo rr                \\(轮询算法)
     lb_kind DR               
     persistence_timeout 10      
     protocol TCP              \\(TCP协议检查realserver状态)
 
     real_server 10.10.10.3 80 {
         weight 3           
         TCP_CHECK {
         connect_timeout 3      \\(3秒无响应超时 )
         nb_get_retry 3
         delay_before_retry 3
         connect_port 80
         }
     }
 
     real_server 10.10.10.4 80 {
         weight 3
         TCP_CHECK {
         connect_timeout 3
         nb_get_retry 3
         delay_before_retry 3
         connect_port 80
         }
      }
}
 
 
 
在主从负载均衡服务器上绑定虚拟 IP
 
vim /etc/sysctl.conf
写入  net.ipv4.conf.lo.arp_ignore = 1
        net.ipv4.conf.lo.arp_announce = 2
       net.ipv4.conf.all.arp_ignore = 1
       net.ipv4.conf.all.arp_announce = 2
       [code]
 
 
#cd /etc/sysconfig/network-scripts/
#ls
#cp ifcfg-eth0 ifcfg-eth0:1
#vim ifcfg-eth0:1    
   
 
#service network restart
 
在主( Master)服务器上查看LVS服务是否正常
主:
#watch ipvsadm –ln
显示结果:
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
  -> RemoteAddressort           Forward Weight ActiveConn InActConn
TCP  10.10.10.100:80 rr persistent 10
  -> 10.10.10.3:80            Route   3      0          0
  -> 10.10.10.4:80             Route   3      0          0
 
 
查看从 (Backup)服务器LVS服务是否正常
#watch ipvsadm –ln
显示结果:
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
  -> RemoteAddressort           Forward Weight ActiveConn InActConn
TCP  10.10.10.100:80 rr persistent 10
  -> 10.10.10.3:80            Route   3      0          0
  -> 10.10.10.4:80             Route   3      0          0
5测试是否实现负载均衡和高可用
   从客户端 ping web服务器的虚拟IP,然后用arp –a查看提供服务的服务器MAC
 
      :停一台负载均衡服务器后,ping过程并没有中断
 
   服务提供者 MAC为:08:00:27:9d:61:a6  
 
 客户机换一个 IP后,再ping 然后用arp –a 查看提供服务的服务器MAC
   服务提供者 MAC为:74:ea:3a:45:ad:38

你可能感兴趣的:(linux,高可用,LVS,负载均衡,安全)