Linux多种负载均衡、高可用环境搭建

方案一:ipvsadm+nginx(web)

lvs-master 172.16.34.236
web1    172.16.34.237
web2    172.16.34.238
lvs-dr-vip 172.16.34.225

一、lvs-master:
1.安装ipvsadm
2.编写并运行lvs.sh
-----------------------
#!/bin/bash
vip=172.16.34.225
rs1=172.16.34.237
rs2=172.16.34.238
#set virttual ip address
/sbin/ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up
/sbin/route add -host $vip dev eth0:0
#clear ipvs tables
/sbin/ipvsadm -C
#set LVS Server
/sbin/ipvsadm -A -t $vip:80 -s rr
/sbin/ipvsadm -a -t $vip:80 -r $rs1:80 -g
/sbin/ipvsadm -a -t $vip:80 -r $rs2:80 -g
#run lvs
/sbin/ipvsadm
-------------------------

sh lvs.sh
3.查看IP是否配置成功
[root@CentOS5-B ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:64:59:80 
          inet addr:172.16.34.236  Bcast:172.16.255.255  Mask:255.255.0.0
          inet6 addr: fe80::20c:29ff:fe64:5980/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9847 errors:0 dropped:0 overruns:0 frame:0
          TX packets:336 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:852188 (832.2 KiB)  TX bytes:39262 (38.3 KiB)
          Interrupt:67 Base address:0x2000

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:64:59:80 
          inet addr:172.16.34.225  Bcast:172.16.34.225  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:67 Base address:0x2000

eth1      Link encap:Ethernet  HWaddr 00:0C:29:64:59:8A 
          inet addr:192.168.44.129  Bcast:192.168.44.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe64:598a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:92 errors:0 dropped:0 overruns:0 frame:0
          TX packets:201 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:7656 (7.4 KiB)  TX bytes:14066 (13.7 KiB)
          Interrupt:67 Base address:0x2080

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:560 (560.0 b)  TX bytes:560 (560.0 b)

二、web1 web2:
1.编写并运行realserver.sh
----------------------
#!/bin/sh
vip=172.16.34.225
/etc/init.d/functions
case "$1" in
        start)
        echo "Start lvs of Real Server"
        /sbin/ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
        /sbin/route add -host $vip dev lo:0
        echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
        echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
        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 Director Server"
        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
        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
-------------------------------------
sh realserver.sh start

2.查看IP是否配置成功
[root@CentOS5-C lvs]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:3C:9C:CD 
          inet addr:172.16.34.237  Bcast:172.16.255.255  Mask:255.255.0.0
          inet6 addr: fe80::20c:29ff:fe3c:9ccd/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6967 errors:0 dropped:0 overruns:0 frame:0
          TX packets:240 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:622137 (607.5 KiB)  TX bytes:32440 (31.6 KiB)
          Interrupt:67 Base address:0x2000

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:560 (560.0 b)  TX bytes:560 (560.0 b)

lo:0      Link encap:Local Loopback 
          inet addr:172.16.34.225  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
3.web1 web2开启web服务
执行/usr/local/nginx/sbin/nginx
[root@CentOS5-C lvs]# netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State     
tcp        0      0 0.0.0.0:686                 0.0.0.0:*                   LISTEN     
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN     
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN     
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN     
tcp        0      0 :::22                       :::*                        LISTEN     
tcp        0    132 ::ffff:172.16.34.237:22     ::ffff:172.16.34.80:49557   ESTABLISHED


三、测试
1.访问http://172.16.34.225
2.在lvs服务器上查看结果。
[root@CentOS5-B ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  localhost:http rr
  -> localhost:http               Route   1      2          2        
  -> localhost:http               Route   1      1          2 
 
 
=================================================================================================================================
方案二:keepalived+nginx(web)

lvs-master 172.16.34.235
lvs-backup 172.16.34.236
web1    172.16.34.237
web2    172.16.34.238
lvs-dr-vip 172.16.34.225

1.安装nginx  web服务器安装
2.安装keepalived  主备都需要安装
cd keepalived-1.1.19
./configure --prefix=/usr/local/keepalived
make && make install

cp /usr/local/keepalived/sbin/keepalived /usr/sbin
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

(1)lvs-master:
/etc/keepalived/keepalived.conf
-----------------------------------
[root@CentOS5-A keepalived]# less keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100  #此处主priority值设置为100,备设置为80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.34.225
    }
}

virtual_server 172.16.34.225 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 172.16.34.237 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        }
     }

    real_server 172.16.34.238 80 {
        weight 1
        TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        }
        }      
     } 
[root@CentOS5-A keepalived]#
---------------------------------------
(2)lvs-bakcup:
[root@CentOS5-B keepalived]# less keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.34.225
    }
}

virtual_server 172.16.34.225 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 172.16.34.237 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        }
     }

    real_server 172.16.34.238 80 {
        weight 1
        TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        }
        }      
     } 
[root@CentOS5-B keepalived]#

3.启动keepalived服务
service keepalived restart
4.测试
1.访问http://172.16.34.225
2.在lvs服务器上查看结果。
[root@CentOS5-B ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  localhost:http rr
  -> localhost:http               Route   1      2          2        
  -> localhost:http               Route   1      1          2 
 
========================================================================================================

方案三:heartbeat+nginx(web)

lvs-master 172.16.34.235 eth0  192.168.44.128 eth1(心跳线)
lvs-backup 172.16.34.236 eth0  192.168.44.129 eth1(心跳线)
web1    172.16.34.237
web2    172.16.34.238
lvs-dr-vip 172.16.34.225


1.heartbeat安装
tar -zxvf libnet-1.1.4.tar.gz
cd libnet-1.1.4
./configure
make && make install

tar zxvf heartbeat-2.1.3.tar.gz
cd hearbeat-2.1.3
./ConfigureMe configure --disable-swig --disable-snmp-subagent
make && make install
cp doc/ha.cf doc/haresources doc/authkeys /etc/ha.d/
cp ldirectord/ldirectord.cf /etc/ha.d/
groupadd -g 694 haclient
useradd -u 694 -g haclient hacluster

2.lvs-master
[root@CentOS5-A ha.d]# cd /etc/ha.d
[root@CentOS5-A ha.d]# ls
authkeys  ha.cf  harc  haresources  ha.sh  ldirectord.cf  rc.d  README.config  resource.d  shellfuncs
[root@CentOS5-A ha.d]#
[root@CentOS5-A ha.d]# cat authkeys |grep ^# -v
auth 1
1 crc

[root@CentOS5-A ha.d]# cat ha.cf |grep ^# -v
logfile /var/log/ha-log
logfacility     local0
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport 694
baud    19200
serial  /dev/ttyS0      # Linux
bcast   eth1            # Linux
ucast eth1 192.168.44.129
auto_failback on
node    CentOS5-A
node    CentOS5-B
ping 172.16.34.1
ping_group group1 192.168.44.129 192.168.44.128
respawn hacluster /usr/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster

[root@CentOS5-A ha.d]# cat haresources |grep -v ^#
CentOS5-A IPaddr::172.16.34.225/16/eth0 ldirectord

[root@CentOS5-A ha.d]# cat ldirectord.cf |grep -v ^#
checktimeout=3
checkinterval=1
fallback=127.0.0.1:80
autoreload=yes
logfile="/var/log/ldirectord.log"
quiescent=yes

virtual=172.16.34.225:80
        real=172.16.34.237:80 gate
        real=172.16.34.238:80 gate
        #fallback=127.0.0.1:80 gate
        service=http
        #request="index.html"
        #receive="Test Page"
        scheduler=rr
        protocol=tcp
        checktype=negotiate
        checkport=80
       
       
3.lvs-backup
[root@CentOS5-B ha.d]# cat authkeys |grep -v ^#
auth 1
1 crc
[root@CentOS5-B ha.d]# cat ha.cf |grep -v ^#
logfile /var/log/ha-log
logfacility     local0
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport 694
baud    19200
serial  /dev/ttyS0      # Linux
bcast   eth1            # Linux
ucast eth1 192.168.44.128
auto_failback on
node    CentOS5-A
node    CentOS5-B
ping 172.16.34.1
ping_group group1 192.168.44.128 192.168.44.129
respawn hacluster /usr/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster

[root@CentOS5-B ha.d]# cat haresources |grep -v ^#
CentOS5-A IPaddr::172.16.34.225/16/eth0 ldirectord

[root@CentOS5-B ha.d]# cat ldirectord.cf |grep -v ^#
checktimeout=20
checkinterval=10
fallback=127.0.0.1:80
autoreload=yes
logfile="/var/log/ldirectord.log"
quiescent=yes

virtual=172.16.34.225:80
        real=172.16.34.237:80 gate
        real=172.16.34.238:80 gate
        #fallback=127.0.0.1:80 gate
        service=http
        #request="index.html"
        #receive="Test Page"
        scheduler=rr
        protocol=tcp
        checktype=negotiate
        checkport=80

4.启动服务
/etc/init.d/heartbeat start

5.测试

 


  
 

你可能感兴趣的:(LVS,keepalived,heartbeat,ipvsadm,nginx(web))