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

wKiom1l9oGiR3YE2AAAh8I_StLc061.png

wKiom1l9oGmiY6HQAAAEqG4uGWY840.png










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

wKioL1l4tMSgzxGZAAAPXkVACog195.png

  scp haresources ldirectord.cf 172.25.2.4:/etc/ha.d/   ##複製文件到另一臺熱備主機

  /etc/init.d/heartbeat start

 測試

 Server1的heartbeat停止後,server4作熱備,繼續執行

wKiom1l4tSyg2swrAABL1dv2Tek059.png

wKioL1l4tS2i7ByqAAAh8A9RePI621.png



###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 [email protected]:/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/

一臺主機的服務掛掉之後,另一臺主機會接替他繼續執行服務

wKioL1l4vBLgMlJ8AADEOtbXnzM173.png

wKiom1l4vBPTRdevAADMPywBCPU323.png


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章