首页 > 代码库 > keepalived+lvs

keepalived+lvs

########hearbeat####
server1
yum install -y heartbeat-*
cd /usr/share/doc/heartbeat-3.0.4/
cp ha.cf authkeys  haresources /etc/ha.d/
cd /etc/ha.d/
vim /ha.cf
 48 keepalive 2
 55 deadtime 30
 60 warntime 10
 70 initdead 60
 75 udpport 717
 90 bcast   eth0  
156 auto_failback on
210 node    server1
211 node    server4
219 ping 172.25.23.250
252 respawn hacluster /usr/lib64/heartbeat/ipfail
258 apiauth ipfail gid=haclient uid=hacluster



vim authkeys
 23 auth 1
 24 1 crc
chmod 600 authkeys 


vim haresources
150 server1 IPaddr::172.25.23.100/24/eth0 httpd

scp ha.cf authkeys haresources server4:/etc/ha.d/

/etc/init.d/httpd status
/etc/init.d/heartbeat start
测试:当一台主机的服务挂掉之后,另一台主机会接替他继续执行服务

ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:d1:96:0a brd ff:ff:ff:ff:ff:ff
    inet 172.25.23.1/24 brd 172.25.23.255 scope global eth0
    inet 172.25.23.100/24 scope global secondary eth0
    inet6 fe80::5054:ff:fed1:960a/64 scope link
       valid_lft forever preferred_lft forever
   
#server1/4 

ip addr add 172.25.23.100/24 dev eth0                     ##添加vip

ipvsadm -A -t 172.25.23.100:80 -s rr                        ##设置负载均衡方式为轮询

ipvsadm -a -t 172.25.23.100:80 -r 172.25.23.2:80 -g  ##设置VIP和RIP

ipvsadm -a -t 172.25.23.100:80 -r 172.25.23.3:80 -g

/etc/init.d/ipvsadm save                                             ##保存设置



server2,server3

yum install arptables_jf -y

ip addr add 172.25.23.100/32  dev  eth0
arptables -A IN -d 172.25.23.100 -j DROP   ##直接连接IP就丢弃

arptables -A OUT -s 172.25.23.100 -j mangle --mangle-ip-s 172.25.23.3  #出去的是RIP

/etc/init.d/arptables_jf save  ##保存设置

/etc/init.d/arptables_jf start  ## 开启服务
vim /var/www/html/index.html
vm2:westos2

vm3:westos3

技术分享

技术分享










vm1 vm4

##Heartbeat与Lvs负载均衡

cd /usr/share/doc/ldirectord-3.9.5/

cp ldirectord.cf /etc/ha.d/

vim ldirectord


25 virtual=172.25.23.100:80
 26         real=172.25.23.2:80 gate
 27         real=172.25.23.3:80 gate
 28         fallback=127.0.0.1:80 gate
 29         service=http
 30         scheduler=rr
 31         #persistent=600
 32         #netmask=255.255.255.255
 33         protocol=tcp
 34         checktype=negotiate
 35         checkport=80
 36         request="index.html"
 37 #       receive="Test Page"
 38 #       virtualhost=www.x.y.z


ipvsadm -C   ##刷掉以前的设置

  ip addr del 172.25.23.100/24 dev eth0  ##删除之前设置的VIP

  vim haresources

技术分享

  scp haresources ldirectord.cf 172.25.2.4:/etc/ha.d/   ##复制文件到另一台热备主机

  /etc/init.d/heartbeat start

 测试

 Server1的heartbeat停止后,server4作热备,继续执行

技术分享

技术分享



###keepalived

mkdir /etc/keepalived

 tar -zxf keepalived-1.3.5.tar.gz

    cd keepalived-1.3.5

   ./configure --prefix=/usr/local/keepalived

   make && make install   ##安装服务

   scp -r /usr/local/keepalived/ 172.25.23.4:/usr/local/   ##给另一台热备主机复制配置文件

   ln -s /usr/local/keepalived/sbin/keepalived /sbin/

   ln -s /usr/local/keepalived/etc/keepalived/ /etc/

   ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

   ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/


chmod +x /usr/local/keepalived/etc/rc.d/init.d/keepalived


/etc/keepalived
vim keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
        root@localhost
        }
   notification_email_from keep@server1
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 23
    priority 100
    advert_int 1
    authentication {
auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.23.100
}
}

virtual_server 172.25.23.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    #persistence_timeout 50
    protocol TCP

    real_server 172.25.23.2 80 {
        weight 1
         TCP_CHECK{
        connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
real_server 172.25.23.3 80 {
        weight 1
        TCP_CHECK{
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

}
scp keepalived.conf root@172.25.23.4:/etc/keepalived/   ##给另一台热备主机拷贝配置文件,注意要改priority值

    etc/init.d/keepalived start

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

一台主机的服务挂掉之后,另一台主机会接替他继续执行服务

技术分享

技术分享


keepalived+lvs