vlambda博客
学习文章列表

超详细!nginx高可用方案:keepalived+nginx

    


    nginx的高可用,广为流传的便是keepalived+nginx了,下面就详细介绍下这个方案的搭建



环境准备
1


192.168.30.135【主】

192.168.30.128【备】


    主备都安装nginx,并且将index页面加入IP信息(方便后续观察VIP飘逸,主备切换)

超详细!nginx高可用方案:keepalived+nginx

超详细!nginx高可用方案:keepalived+nginx



keepalived安装
2



【1】下载安装包:

wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz

【2】解压安装包:[root@bogon src]# tar xvf keepalived-1.3.5.tar.gz

【3】编译安装


[root@bogon src]# cd keepalived-1.3.5

[root@bogon keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived

[root@bogon keepalived-1.3.5]# make

[root@bogon keepalived-1.3.5]# make install

 


需要安装openssl  不然会报错

yum -y install openssl-devel

超详细!nginx高可用方案:keepalived+nginx


注册为系统服务
3


【1】将解压之后的安装包中的keepalived文件复制到/etc/init.d/目录下


[root@bogon keepalived-1.3.5]# cp /root/keepalived/keepalived-1.3.5/keepalived/etc/init.d/keepalived /etc/init.d/keepalived

[root@bogon keepalived-1.3.5]# ll /etc/init.d/ | grep keepalived

-rwxr-xr-x  1 root root  1308 8月   2 14:15 keepalived



【2】修改/etc/init.d/keepalived启动脚本


需要修改的数据有以下几项:


# config: /etc/keepalived/keepalived.conf 文件的位置;

# Source configuration file (we set KEEPALIVED_OPTIONS there) 文件的位置。. /etc/sysconfig/keepalived

修改为实际安装的文件路径,我的安装路径如下:


# config: /usr/local/keepalived/etc/keepalived/keepalived.conf

. /usr/local/keepalived/etc/sysconfig/keepalived

另外添加如下变量,并修改文件中相应的部分:


keepalived=/usr/local/keepalived/sbin/keepalived

keepalived_config=/usr/local/keepalived/etc/keepalived/keepalived.conf

keepalived_pid=/var/run/keepalived.pid



【3】systemctl命令管理keepalived


systemctl start keepalived.service


systemctl stop keepalived.service


systemctl restart keepalived.service


systemctl status keepalived.service


如果报错,可以journalctl -xe查看具体失败信息


 



keepalived配置
4


配置文件:/usr/local/keepalived/etc/keepalived/keepalived.conf


下面提供最简单的配置,更多的可以自行查询


4.1·主节点配置

global_defs {

   router_id LVS_DEVEL

}


vrrp_instance VI_1 {

    state MASTER

    interface ens33

    virtual_router_id 51

    priority 100

    mcast_src_ip 192.168.30.135

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.30.140

    }

}



4.2·备节点配置

global_defs {

   router_id LVS_DEVEL

}


vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    virtual_router_id 51

    priority 95

    mcast_src_ip 192.168.30.128

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.30.140

    }

}



 

使用验证
5


    按照上面的配置并启动之后,在主节点192.168.30.135上ip addr就可以看到配置文件中配置的VIP 192.168.30.140 了(备节点看不到这个vip)


超详细!nginx高可用方案:keepalived+nginx


    访问VIP 192.168.30.140   也可以看到访问到了主节点的nginx上


超详细!nginx高可用方案:keepalived+nginx


 


    将主节点的keepalive停掉之后,主节点ip addr上就卡不到该VIP了,在备节点可以看到,并且浏览器访问VIP会访问到备节点的nginx上

超详细!nginx高可用方案:keepalived+nginx



    但是如果主机点恢复了,那么VIP又会自动漂移会主节点


    一般keepalived的使用都会匹配值健康检测脚本,如果检测到对应的实例不可用,则干掉本机的keepalived,将VIP切换到备节点


 

问题记录
6


【1】Unable to find configuration fille

超详细!nginx高可用方案:keepalived+nginx

    这里是因为默认的配置文件路径还有没有修改的地方,需要修改如下文件的相应内容


    vi /usr/local/keepalived/etc/sysconfig/keepalived

超详细!nginx高可用方案:keepalived+nginx


【2】PID file /usr/local/keepalived/var/run/keepalived.pid not readable (yet?) after start

    查看下面文件中pid文件的路径配置,建议配置成  /var/run/keepalived.pid


vi /usr/lib/systemd/system/keepalived.service

vi  /etc/init.d/keepalived


 


【3】keepalive启动ok,但是vip无法访问(无法ping通)

将keepalived.conf中的vrrp_strict删除或者注释掉