1. 程式人生 > >lvs+keepalived+nginx

lvs+keepalived+nginx

查看 one del type ipad ati 過程 pvs ech

安裝過程

查看是否有ipvsadm軟件

1. 檢查ipvsadm是否安裝成功.可以直接輸入
[[email protected] ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port Forward Weight ActiveConn InActConn
 
2. 檢查模版是否加入內核
[[email protected] ~]# 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

[[email protected]_lb home]# /etc/init.d/keepalived start
Starting keepalived: [  OK  ]

[[email protected]_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