准备工作

虚拟机vmware workstations

安装ubuntu系统15.04 64位系统


配置主节点


安装keepalived,ubuntu下安装和其他系统下安装还是有很有区别的!!!

下载http://www.keepalived.org/software/keepalived-1.2.22.tar.gz

cmd下指令

如果已经是超级管理员 可以不用加sudo切换

$ sudo apt-get install libpopt-dev
$ sudo apt-get install daemon
$ sudo tar zxfv keepalived-1.2.22.tar.gz
$ cd keepalived-1.2.22
$ ./configure --prefix=/usr/local/keepalived
$ make
$ sudo make install
$ sudo mkdir /etc/sysconfig
$ sudo cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
$ sudo cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
$ sudo cp /usr/local/keepalived/sbin/keepalived /sbin/
$ sudo mkdir /etc/keepalived
$ sudo cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
$ sudo vi /etc/keepalived/keepalived.conf

内容如下

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.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_script chk_tomcat
{
     script "/home/wm5920/keepalived-1.2.22/scripts/tomcat_check.sh"
     interval 2
     timeout 2
     fall 3
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       192.168.186.251
    }
    track_script {
         chk_tomcat
    }

}

其中

chk_tomcat为检验tomcat存活脚本,内容如下

#!/bin/bash 
netstat -ant|grep 8080
ALIVE=$?
LOGFILE="/home/wm5920/keepalived-1.2.22/scripts/keepalived-check.log" 
echo "[CHECK]" >> $LOGFILE
date >> $LOGFILE
if [ $ALIVE -eq 0 ]; then : 
   echo "8080 is ok" >> $LOGFILE 2>&1
   exit 0 
else 
   echo "8080 is down" >> $LOGFILE 2>&1
   exit 1 
fi 

$?为上一个命令的结果

virtual_ipaddress为配置的虚拟ip(同一个网段的)


:wq  (保存并退出)
$ sudo mkdir /var/lock/subsys   (注:这个目录重启服务器后会消失,需要将其写道开机启动项中,如写到/etc/rc.local里)
$ service keepalived start
开机自启动
$ sudo vi /etc/rc.local
按a或i进入编辑模式
mkdir /var/lock/subsys
service keepalived start
按Esc键退出编辑模式
:wq (保存并退出)
$ sudo ip a   (可以看到eth0多了个192.168.186.251的ip)

BACKUP服务器步骤同上,只需将/etc/keepalived/keepalived.conf里的MASTER改为BACKUP,priority改为50即可
BACKUP服务器ip a显示eth0没有多出192.168.186.251这个ip,等MASTER服务器宕后就会显示出来,当MASTER服务器正常后会自动消失


安装tomcat,在webapp下放ROOT文件夹,里面放index.html,内容为

欢迎 8080


配置备节点


克隆第一个虚拟机

完成后将keepalived.conf里的MASTER改为BACKUP即可,同时将index.html内容改成“”欢迎 backup 8080“”

测试结果


service keepalived start

./startup.sh

启动两个节点的keepalived和tomcat


在浏览器中输入
http://192.168.186.251:8080/

查看页面打印,出现

欢迎 8080

关闭主节点下tomcat

再次请求,出现

欢迎 backup 8080

再次启动主节点tomcat

又回到 欢迎 8080


cmd下也可以通过ip a观察虚拟ip飘来飘去。。。。。



Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐