keepalived + lvs

Keepalived+lvs

服务器 IP 分配:(系统均是:RHEL6.0 内核:2.6.32-71.el6_x86)
Virtual IP:192.168.0.111
Load Balancer:192.168.0.1
Backup:192.168.0.2
Real Server 1:eth0: 192.168.0.11
Real Server 2:eth0: 192.168.0.12

(一)、
主备机上的软件包安装与配置
yum install ipvsadm kernel­-devel gcc popt-devel libnl-devel ­-y
wget http://www.keepalived.org/software/keepalived­1.2.2.tar.gz
tar zxf keepalived­1.2.2.tar.gz
cd keepalived­1.2.2
./configure --­­prefix=/usr/local/keepalived ­­--with­-kernel-­dir=/usr/src/kernels/2.6.32­-71.el6.x86_86/

Keepalived configuration
­­­­­­­­­­­­­­­­­­­­­­­­---------------------------------
Keepalived version         : 1.2.2
Compiler            : gcc
Compiler flags        : ­g ­O2
Extra Lib            : ­lpopt ­lssl ­lcrypto
Use IPVS Framework          : Yes       ;注意编译时一定要支持 lvs
IPVS sync daemon support    : Yes
Use VRRP Framework          : Yes
Use LinkWatch               : No
Use Debug flags             : No
-----------------------------------
make
make install

ln ­-s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln ­-s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
ln ­-s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/

vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
        root@localhost       #接收警报的 email 地址,可以添加多个
   }
   notification_email_from root@localhost
   smtp_server 127.0.0.1      #使用本机转发 email
   smtp_connect_timeout 30
   router_id LVS_DEVEL         #load balancer 的标识 ID,用于 email 警报
}
vrrp_instance VI_1 {
    state MASTER            #备机改为 BACKUP,此状态是由 priority 的值来决定的,当前
                                priority 的值小于备机的值,那么将会失去 MASTER 状态

    interface eth0              #HA 监测网络接口
    virtual_router_id 51        #在做实验时路由id要与其他人不同,主备机的virtual_router_id相同
    priority 100                #主机优先级高于备机 ,备机  改为50
    advert_int 1            #主备之间的通告间隔秒数
    authentication {            #主备切换时的验证
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.111            虚拟ip可以是多个,分别指定不同的服务
    }
}
    
        
 
virtual_server 192.168.0.111 80 {
    delay_loop 6                   #每隔 6 秒查询 realserver状态
    lb_algo rr                     #lvs 调度算法,这里使用轮叫
    lb_kind DR                     #lvs 负载均衡机制,这里使用直连路由
#   persistence_timeout 50         #同一 IP 的连接 60 秒内被分配到同一台 realserver
    protocol TCP                   #用 TCP 协议检查 realserver 状态
    real_server 192.168.0.1 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.0.2 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3                       #故障重试秒数
            delay_before_retry 3                 #重试延迟
        }
    }
}


(二)、

分别在 realserver 上执行以下命令
vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
sysctl -p

ifconfig eth0:0 192.168.0.200 netmask 255.255.255.255 up
route add -host 192.168.0.200 dev eth0:0

echo `hostname` > /var/www/html/index.html
service httpd start


测试:
先开启主机服务/etc/init.d/keepalived start   查看日志 tail -f /var/log/messege,确认开启无误
再开启备机服务/etc/init.d/keepalived start  查看日志 tail -f /var/log/messege,确认开启无误
访问192.168.0.200 看到页面切换成功即可(刚开始可能只能出现一个,主备机切几次就会好)

当主机服务停掉时realserver的服务由备机接管,BACKUP 变为MASTER ,主机服务开启后自动切换到主机接管
当主/备机网卡出现问题(测试时ifconfig eth0 down),服务由备/主机来接管。

 

你可能感兴趣的:(keepalived,LVS)