1.heartbeat

   yum install heartbeat-* -y

   cd /usr/share/doc/heartbeat-3.0.4/

   cp ha.cf authkeys haresources /etc/ha.d/

   cd /etc/ha.d

   vim ha.cf

wKiom1l4sEWCx072AAAg2BT7oS0182.png

wKioL1l4sEbRccs9AAASdJCeJR8796.png

heartbeat+lvs+Keepalive_第1张图片

heartbeat+lvs+Keepalive_第2张图片


   vim authkeys

  

    vim haresources

wKiom1l4sJXRxgGLAAATyjcujx0152.png

          chmod 600 authkeys

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

   /etc/init.d/heartbeat start

 测试:

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

heartbeat+lvs+Keepalive_第3张图片

    

      2.Lvs负载均衡

   Server1 

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

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

    ipvsadm -a -t 172.25.2.100:80 -r 172.25.2.2:80 -g  ##设置VIP和RIP

    ipvsadm -a -t 172.25.2.100:80 -r 172.25.2.3:80 -g

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

heartbeat+lvs+Keepalive_第4张图片

    Server2 和 server3上

     ip addr add 172.25.2.100/32 dev eth0

     arptables -A IN -d 172.25.2.100 -j DROP   ##直接连接IP就丢弃

     arptables -A OUT -s 172.25.2.100 -j mangle --mangle-ip-s 172.25.2.2  ##出去的是RIP

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

     /etc/init.d/arptables_jf start   ##开启服务

heartbeat+lvs+Keepalive_第5张图片

  测试

heartbeat+lvs+Keepalive_第6张图片

wKioL1l4s7LyEbQBAAAq7_WYXYg104.png

       3.HeartbeatLvs负载均衡

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

   cp ldirectord.cf /etc/ha.d/

   vim ldirectord

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

  ip addr del 172.25.2.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作热备,继续执行

heartbeat+lvs+Keepalive_第7张图片

heartbeat+lvs+Keepalive_第8张图片

     4.Keepalive

   tar -zxf keepalived-1.2.20.tar.gz

    cd keepalived-1.2.20l

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

   make && make install   ##安装服务

   scp -r /usr/local/keepalived/ 172.25.2.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/

heartbeat+lvs+Keepalive_第9张图片

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

   vim keepalived.conf

heartbeat+lvs+Keepalive_第10张图片

   scp keepalived.conf [email protected]:/etc/keepalived/   ##给另一台热备主机拷贝配置文件,注意要改priority值

    etc/init.d/keepalived start

  测试

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

heartbeat+lvs+Keepalive_第11张图片