配置Keepalived实现HTTPD高可用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了配置Keepalived实现HTTPD高可用相关的知识,希望对你有一定的参考价值。

一、环境说明

 

安装环境:

主机名

主机IP地址

集群角色

集群服务

虚拟IP地址

keepalived-master

192.168.1.1

MASTER(主节点)

HTTPD

192.168.1.3

keepalived-backup

192.168.1.2

BACKUP(备用节点)

HTTPD


官方地址:http://www.keepalived.org/

 

一、软件部署

 

分别在keepalived-master keepalived-backup 安装keepalived软件

 

yum install openssl-devel -y

 

tar zxvf keepalived-1.2.20.tar.gz

./configure --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64

make && make install

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

chkconfig --add keepalived

chkconfig --level 35 keepalived on

 

二、创建配置文件

 

Keepalived-master 配置文件:

 

vim /etc/keepalived/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_script check_httpd {

   script "killall -0 httpd"

   interval 2

   }

 

vrrp_instance HA_1 {

    state MASTER

    interface eth0

    virtual_router_id 80

    priority 100

    advert_int 2

 

    authentication {

        auth_type PASS

        auth_pass qweasdzxc

    }

    

    notify_master "/etc/keepalived/master.sh"

    notify_backup "/etc/keepalived/backup.sh"

    notify_fault "/etc/keepalived/fault.sh"

 

    track_script {

    check_httpd

    }

 

    virtual_ipaddress {

        192.168.1.3/24 dev eth0

    }

}

 

其中,master.sh文件内容为:

 

#!/bin/bash

LOGFILE=/var/log/keepalived-mysql-state.log

echo "[Master]" >> $LOGFILE

date >> $LOGFILE

 

其中,backup.sh文件内容为:


#!/bin/bash

LOGFILE=/var/log/keepalived-mysql-state.log

echo "[Backup]" >> $LOGFILE

date >> $LOGFILE

 

其中,fault.sh文件内容为:

#!/bin/bash

LOGFILE=/var/log/keepalived-mysql-state.log

echo "[Fault]" >> $LOGFILE

date >> $LOGFILE

 

Keepalived-backup 配置文件:

 

Keepalived-backup节点上的keepalived.conf配置文件与keepalived-master节点上基本相同,需要修改的地方有两个:

将“state MASTER”更改为 “state BACKUP”。

priority 100 更改为一个较小的值,这里改为“priority 80”。

 

三、启动服务

 

分别在keepalived-masterkeepalived-backup节点启动keepalived服务

 

chkconfig --level 35 httpd on

/etc/init.d/httpd start

/etc/init.d/keepalived start

 

四、通过日志可查看启动信息

 

tail -f /var/log/keepalived-mysql-state.log

tail -f /var/log/messages

 

命令查看:ip add

 

五、模拟故障切换

 

停掉keepalived-master 节点的 HTTPD服务,发现192.168.1.3 IP正确转移至 keepalived-backup节点上。

 

报错:

[[email protected] ~]# /etc/init.d/keepalived start

正在启动 keepalived/bin/bash: keepalived: command not found

                                                           [失败]

解决:

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


本文出自 “高路川” 博客,请务必保留此出处http://luchuangao.blog.51cto.com/11311715/1760477

以上是关于配置Keepalived实现HTTPD高可用的主要内容,如果未能解决你的问题,请参考以下文章

LVS+KeepAlived构建高可用集群

keepalived

keepalived高可用部署

MySQL集群搭建-双主+keepalived高可用

基于HA机制的Nginx配置实现

Keepalived介绍及架设