Nginx + Keepalived使用文档
第一步:
下载keepalived地址:http://www.keepalived.org/download.html
解压安装:
tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/
yum install -y openssl openssl-devel(需要安装一个软件包)
cd keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived
make && make install
第二步:
将keepalived安装成Linux系统服务,因为没有使用keepalived的默认安装路径(默认路径:/usr/local),安装完成之后,需要做一些修改工作:
首先创建文件夹,将keepalived配置文件进行复制:
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
然后复制keepalived脚本文件:
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
可以设置开机启动:chkconfig keepalived on,到此我们安装完毕!
启动服务:service keepalived start[restart重启|stop关闭]
第三步:
对配置文件进行修改:vim /etc/keepalived/keepalived.conf
keepalived.conf配置文件说明:
(一)Master
! Configuration File for keepalived global_defs { router_id bhz005 ##标识节点的字符串,通常为hostname } ## keepalived 会定时执行脚本并且对脚本的执行结果进行分析,动态调整vrrp_instance的优先级。这里的权重weight 是与下面的优先级priority有关,
## 如果执行了一次检查脚本成功,则权重会-20,也就是由100 - 20 变成了80,Master 的优先级为80 就低于了Backup的优先级90,那么会进行自动的主备切换。 ## 如果脚本执行结果为0并且weight配置的值大于0,则优先级会相应增加。 ## 如果脚本执行结果不为0 并且weight配置的值小于0,则优先级会相应减少。 vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" ##执行脚本位置 interval 2 ##检测时间间隔 weight -20 ## 如果条件成立则权重减20(-20) } ## 定义虚拟路由 VI_1为自定义标识。 vrrp_instance VI_1 { state MASTER ## 主节点为MASTER,备份节点为BACKUP ## 绑定虚拟IP的网络接口(网卡),与本机IP地址所在的网络接口相同(我这里是eth6) interface eth6 virtual_router_id 172 ## 虚拟路由ID号 mcast_src_ip 192.168.1.172 ## 本机ip地址 priority 100 ##优先级配置(0-254的值) Nopreempt ## advert_int 1 ## 组播信息发送间隔,俩个节点必须配置一致,默认1s authentication { auth_type PASS auth_pass bhz ## 真实生产环境下对密码进行匹配 } track_script { chk_nginx } virtual_ipaddress { 192.168.1.170 ## 虚拟ip(vip),可以指定多个 } }
(二)Backup
! Configuration File for keepalived global_defs { router_id bhz006 } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface eth7 virtual_router_id 172 ##要与Master的对应属性值相同 mcast_src_ip 192.168.1.173 priority 90 ##优先级配置 advert_int 1 authentication { auth_type PASS auth_pass bhz } track_script { chk_nginx } virtual_ipaddress { 192.168.1.170 } }
(三)nginx_check.sh 脚本:
#!/bin/bash A=`ps -C nginx –no-header |wc -l` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx sleep 2 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then killall keepalived fi fi
(四)我们需要把master的keepalived配置文件 copy到master机器(172)的 /etc/keepalived/ 文件夹下,在把backup的keepalived配置文件copy到backup机器(173)的 /etc/keepalived/ 文件夹下,最后把nginx_check.sh脚本分别copy到两台机器的 /etc/keepalived/文件夹下。
(五)nginx_check.sh脚本授权。赋予可执行权限:chmod +x /etc/keepalived/nginx_check.sh
(六)启动2台机器的nginx之后。我们启动两台机器的keepalived
/usr/local/nginx/sbin/nginx
service keepalived start
ps -ef | grep nginx
ps -ef | grep keepalived
可以进行测试,首先看一下俩台机器的ip a 命令下 都会出现一个虚拟ip,我们可以停掉 一个机器的keepalived,然后测试,命令:service keepalived stop。结果发现当前停掉的机器已经不可用,keepalived会自动切换到另一台机器上。
(七)我们可以测试在nginx出现问题的情况下,实现切换,这个时候我们只需要把nginx的配置文件进行修改,让其变得不可用,然后强杀掉nginx进程即可,发现也会实现自动切换服务器节点。