linux下初步实现Keepalived+Nginx高可用


一、Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived 可以用来防止服务器单点故障的发生,通过配合Nginx可以实现web前端服务的高可用。

二、基于上篇搭建好Nginx+Tomcat环境信息

      Linux下Nginx+Tomcat配置反向代理

1、两台Linux机器地址为:

主机名    ip地址
node1    192.168.182.130
node2    192.168.182.129

2、在node1安装Keepalived 

#下载目录
cd /usr/local/src/
#下载
wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
#解压
sudo tar -zxvf keepalived-1.2.19.tar.gz
#切换目录
cd keepalived-1.2.19/
#编译
./configure --prefix=/usr/local/keepalived
#安装
make && make install
# 拷贝执行文件
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# 将init.d文件拷贝到etc下,加入开机启动项
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
# 将keepalived文件拷贝到etc下
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# 创建keepalived文件夹
mkdir -p /etc/keepalived
# 将keepalived配置文件拷贝到etc下
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
# 添加可执行权限
chmod +x /etc/init.d/keepalived
#添加系统服务
chkconfig --add keepalived
#开机自启
chkconfig keepalived on
View Code

3、修改配置文件,需要根据个人机器信息配置

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {

}

vrrp_script chk_health {
    script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"
    interval 1    #每隔1秒执行上述的脚本,去检查用户的程序ngnix
    weight -2
}

vrrp_instance VI_1 {
    state MASTER  #指定A节点为主节点 备用节点上设置为BACKUP即可
    interface ens33  #绑定虚拟IP的网络接口
    virtual_router_id 51 ##VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
    priority 100 #主节点的优先级(1-254之间),备用节点必须比主节点优先级低
    advert_int 1 #组播信息发送间隔,两个节点设置必须一样
    authentication { #设置验证信息,两个节点必须一致
        auth_type PASS
        auth_pass 1111
    }
    
    track_script { #心跳检测
        chk_health
    }

    virtual_ipaddress { #指定虚拟IP, 两个节点设置必须一样
       192.168.182.139/24
    }

    notify_master "/usr/local/nginx/sbin/nginx"
    notify_backup "/usr/local/nginx/sbin/nginx -s stop &&  /usr/local/nginx/sbin/nginx"
    notify_fault "/usr/local/nginx/sbin/nginx -s stop"    
}
View Code

4、在node2安装Keepalived ,和2-3一致,其node2上的keepalived.conf内容,只需要修改state,interface,priority,virtual_ipaddress这四个地方就可以了

! Configuration File for keepalived

global_defs {

}

vrrp_script chk_health {
    script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"
    interval 1    #每隔1秒执行上述的脚本,去检查用户的程序ngnix
    weight -2
}

vrrp_instance VI_1 {
    state BACKUP  #指定A节点为主节点 备用节点上设置为BACKUP即可
    interface ens33  #绑定虚拟IP的网络接口
    virtual_router_id 51 ##VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
    priority 99 #主节点的优先级(1-254之间),备用节点必须比主节点优先级低
    advert_int 1 #组播信息发送间隔,两个节点设置必须一样
    authentication { #设置验证信息,两个节点必须一致
        auth_type PASS
        auth_pass 1111
    }
    
    track_script { #心跳检测
        chk_health
    }

    virtual_ipaddress { #指定虚拟IP, 两个节点设置必须一样
       192.168.182.139/24
    }

    notify_master "/usr/local/nginx/sbin/nginx"
    notify_backup "/usr/local/nginx/sbin/nginx -s stop &&  /usr/local/nginx/sbin/nginx"
    notify_fault "/usr/local/nginx/sbin/nginx -s stop"    
}
View Code

5、测试

  a、启动和关闭keepalived

service keepalived start | stop

  启动node1和node2的上nginx启动keepalived,在node1上查看ip信息(ip add),会发现多了一个虚拟ip(192.168.182.139),关闭node1上的keepalived或者nginx,会发现这个虚拟IP已经漂移到node2这个机器上了,访问nginx时,是使用虚拟ip访问的。

      虚拟IP漂移并没有执行的notify_master等所在的脚本,查了很久,也没有成功,不知道有没有大神知道。

 

yexiangyang

moyyexy@gmail.com


 

智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



猜您在找
linux下实现keepalived+nginx高可用 Keepalived+Nginx实现Nginx的高可用 keepalived+nginx实现nginx的高可用 keepalived+nginx实现高可用(三) keepalived+nginx实现高可用(二)
智能推荐
 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告