首页 > 代码库 > lvs+keepalived+nginx

lvs+keepalived+nginx

安装过程

查看是否有ipvsadm软件

1. 检查ipvsadm是否安装成功.可以直接输入
[root@localhost ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port Forward Weight ActiveConn InActConn
 
2. 检查模版是否加入内核
[root@localhost ~]# lsmod |grep ip_vs
 ip_vs 78081 0 

安装keepalived

1. keepalived-1.2.1.tar.gz     # 解压这个文件并且安装 (tar -zxvf keepalived-1.2.1.tar.gz)
2. ./configure --prefix=/hnisi/keepalived/ --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686
3.make
4.make install
5.配置文件的路径
cp /hnisi/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /hnisi/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /hnisi/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /hnisi/keepalived/sbin/keepalived /sbin/

配置主keepalived

vim /etc/keepalived/keepalived.conf

#global define  
global_defs {  
router_id LVS_T1  
}  
  
  
vrrp_sync_group bl_group {  
group {  
  bl_one  
}  
}  
  
vrrp_instance bl_one {  
    state MASTER  
    interface eth0  
    lvs_sync_daemon_interface eth0  
    virtual_router_id 38  
    priority 150  
    advert_int 3  
    authentication {  
        auth_type PASS  
        auth_pass 1111  
    }  
    virtual_ipaddress {  
      192.168.96.100  
    }  
}  
  
  
virtual_server 192.168.96.100 8099 {  
    delay_loop 3  
    lb_algo rr  
    lb_kind DR  
    persistence_timeout 1  
    protocol TCP  
  
  
    real_server 192.168.96.33 8099 {  
        weight 1                                                                                                                                        
        TCP_CHECK {                                                                                                                                     
            connect_timeout 10                                                                                                                          
            nb_get_retry 3                                                                                                                              
            delay_before_retry 3                                                                                                                        
            connect_port 8099                                                                                                                           
        }                                                                                                                                               
                                                                                                                                                        
    }                                                                                                                                                   
                                                                                                                                                        
    real_server 192.168.96.4 8099 {                                                                                                                     
        weight 1                                                                                                                                        
        TCP_CHECK {                                                                                                                                     
            connect_timeout 10                                                                                                                          
            nb_get_retry 3                                                                                                                              
            delay_before_retry 3                                                                                                                        
            connect_port 8099                                                                                                                           
        }                                                                                                                                               
    }                                                                                                                                                   
}                

配置备keepalived

分别在主备上启动keepalived

[root@ip_lb home]# /etc/init.d/keepalived start
Starting keepalived: [  OK  ]

[root@ip_lb home]# tail -f /var/log/message

# PS:这里说的LVS,并不是用ipvsadm软件配置,而是直接用keepalived的virtual_server配置项控制的。
# 安装ipvsadm只是可以看到负载状况,其实只需要keepalived也可以实现负载均衡集群。

在web1服务器配置LVS-DR

vim /etc/init.d/lvsDR_node(web1主机)

#! /bin/sh   
# descript : start real server DR  
VIP=192.168.96.100  
. /etc/rc.d/init.d/functions  
case "$1" in  
       start)  
             echo "start LVS of RealServer DR"  
             /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up  
             echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore  
             echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce  
               ;;  
       stop)  
             /sbin/ifconfig lo:0 down  
             echo "close LVS of RealServer DR"  
             echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore  
             echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce  
              ;;  
         *)  
              echo "Usage : $0 {start|stop}"  
              exit 1  
esac  

把这个文件复制到web2主机上

在web1和web2上分别启动这个脚本:

/etc/init.d/lvsDR_node start

测试是否可以负载均衡

#! /bin/sh  
for((i=1;i<=100;i++));do   
curl http://192.168.96.100:8099 >> /tmp/q;  
done  

官方文档:http://www.keepalived.org/pdf/sery-lvs-cluster.pdf

lvs+keepalived+nginx