lvs+keepalived负载均衡

OS:ubuntu 7.10
VIP:192.168.6.7
keepalived:192.168.6.3
keepalived:192.168.6.4
apache:192.168.6.5
apache2:192.168.6.6
 
在 keepalived和keepalived2  hosts 添加
192.168.6.3     keepalived
192.168.6.4    keepalived2

 
软件:ipvsadm   keepalived
 
一. keepalived 和 keeplived2安装
1.apt-get install  ipvsadm  keepalived
2.在 /etc/keepalived 里添加一个keepalived.conf 文件默认是没有的,
vim keepalived.conf
 
global_defs {
   notification_email {
     
[email]ludy@edgora.com[/email]
   }
   notification_email_from 
[email]keepalived@staff.sina.com.cn[/email]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER    # keepalived2 机器为 BAUCK
    interface eth0
    virtual_router_id 51
    priority 102        # keepalived  机器为 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       192.168.6.7
    }
}
virtual_server 192.168.6.7 80 {
    delay_loop 6
    lb_algo rr    
    lb_kind DR
   persistence_timeout 50   # 默认50秒钟都访问同一台机器
    protocol TCP
    real_server 192.168.6.5 80 {
        weight 1
        TCP_CHECK {
        connect_port 80
        connect_timeout 30
        }
    }
    real_server 192.168.6.6 80 {
        weight 1
        TCP_CHECK {
        connect_port 80
        connect_timeout 30
        }
    }
}
 
 
二.配置 apache 和 apache2
1.apache 安装与配置不多说,在默认页面
apache:192.168.6.5   index.html  5
apache2:192.168.6.6 index.html  6
 
2.在 /etc/sysctl.conf  添加
net.ipv4.ip_forward=0
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
 
3.修改 /etc/network/interfaces 固定IP 和 轮循  lo:0
auto lo
iface lo inet loopback
auto lo:0
iface lo:0 inet static
address 192.168.6.7
netmask 255.255.255.255
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.6.5    #apache 2 为 address 192.168.6.6
netmask 255.255.255.0
gateway 192.168.6.1
 
三.测试
在 keepalived 和 keepalived2 上启动服务~
/etc/init.d/keepalived start
然后在  浏览器上 输入 192.168.6.7 显示 5
在打开一个浏览器输入 192.168.6.7 显示 6
说明 lvs工作正常
然后把  apache 5  停掉
在ha上执行
root@ha:/etc/default# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.6.7:80 rr persistent 50
  -> 192.168.6.6:80               Route   1      2          1
只显示有 6 ``说明`` keepalived 工作正常
把 apache 5启来``
把ha 网卡禁掉``
在用浏览器 访问 192.168.6.7 显示 网页``说明正常```
 
ps: php session 共享同步请看我写的blog ,呵呵简单的web集群!
 


本文转自Deidara 51CTO博客,原文链接:http://blog.51cto.com/deidara/95078,如需转载请自行联系原作者
上一篇:云上快速搭建Serverless AI实验室


下一篇:VTDecoderXPCService意外退出