keepalived安装配置

安装keepalived

wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
tar zxvf nginx-1.2.7.tar.gz
cd nginx-1.2.7
./configure --with-http_stub_status_module
make && make install

 
-------------------------
注意:在执行./configure --with-http_stub_status_module
的时候报错因为少一个库

keepalived执行./configure --prefix=/usr/local/keepalived时报错:configure: error: Popt libraries is required
出现此错误的原因:
未安装popt的开发包

解决方法:
yum install popt-devel
安装好popt的开发包。

----------------------------------------------

运行环境

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived  /etc/rc.d/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/

 
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

 

加入启动服务

echo "/etc/init.d/keepalived start" >> /etc/rc.local

---------------------------------------------------

 

 

 

Keepalived 加载的脚本

keepalived -help中

keepalived --use-file           -f    Use the specified configuration file.

                                Default is /etc/keepalived/keepalived.conf

默认使用的是 /etc/keepalived/keepalived.conf配置文件

 

配置keepalived

按照上面的安装方法,keepalived的配置文件在/etc/keepalived/keepalived.conf。主、从服务器的配置相关联但有所不同。如下:

Master:

global_defs {
   router_id nginx_master
}

 
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        50.1.1.2
    }
}

 

Backup:

global_defs {
notification_email {
   router_id nginx_backup
}

 
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        50.1.1.2
    }
}

 
验证:
                    先后在主、从服务器上启动keepalived: /etc/init.d/keepalived start
                    在主服务器上查看是否已经绑定了虚拟IP: ip addr
                    停止主服务器上的keepalived: /etc/init.d/keepalived stop 然后在从服务器上查看是否已经绑定了虚拟IP:
                    启动主服务器上的keepalived,看看主服务器能否重新接管虚拟IP

 
让keepalived监控NginX的状态
                    经过前面的配置,如果主服务器的keepalived停止服务,从服务器会自动接管VIP对外服务;一旦主服务器的keepalived恢复,会重新接管VIP。 但这并不是我们需要的,我们需要的是当NginX停止服务的时候能够自动切换。
                    keepalived支持配置监控脚本,我们可以通过脚本监控NginX的状态,如果状态不正常则进行一系列的操作,最终仍不能恢复NginX则杀掉keepalived,使得从服务器能够接管服务。

 
如何监控NginX的状态
                    最简单的做法是监控NginX进程,更靠谱的做法是检查NginX端口,最靠谱的做法是检查多个url能否获取到页面。

 
如何尝试恢复服务
                    如果发现NginX不正常,重启之。等待3秒再次校验,仍然失败则不再尝试。
根据上述策略很容易写出监控脚本。这里使用nmap检查nginx端口来判断nginx的状态,记得要首先安装nmap。监控脚本如下:
#!/bin/sh
# check nginx server status
NGINX=/usr/local/nginx/sbin/nginx
PORT=80

 
nmap localhost -p $PORT | grep "$PORT/tcp open"
#echo $?if [ $? -ne 0 ];then
    $NGINX -s stop
    $NGINX
    sleep 3
    nmap localhost -p $PORT | grep "$PORT/tcp open"
    [ $? -ne 0 ] && /etc/init.d/keepalived stopfi

 
不要忘了设置脚本的执行权限,否则不起作用。
假设上述脚本放在/opt/chk_nginx.sh,则keepalived.conf中增加如下配置:

 
vrrp_script chk_http_port {
    script "/opt/chk_nginx.sh"
    interval 2
    weight 2
}
track_script {
    chk_http_port
}

 
这里又遇到了问题就是脚本怎么都不执行:
最后找到解决办法:

将 track_script  嵌入到 vrrp_instance 里面

如下:

 

global_defs {

   router_id nginx_master

}

vrrp_script chk_http_port

{

    script "/opt/chk_nginx.sh"

    interval 1

    weight -2

}

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 101

    advert_int 1

 

    track_script {

       chk_http_port

    }

 

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        172.16.1.111

    }

}

 

 

本博客记录Sylon的学习总结和学习情况做记录 望今后能记录自己的学习之路,同时分享高品质博文!!!
原文地址:https://www.cnblogs.com/Sylon/p/11747477.html