Nginx+keepalived 主从双机热备自动切换解决方案
测试环境如下:
系统:Ceentos 6.4 64 位
主 nginx 服务器:192.168.122.5
备 nginx 服务器:192.168.122.6
VIP:192.168.122.15
一、Nginx+keepalived 安装—脚本安装
#!/bin/bash
# author: kuangl
# mail: kuangl@orient-media.com
# description: The installation of Nginx files.
# -------------------------------------------------------- #
## Nginx_install
# -------------------------------------------------------- #
# Nginx installation
#CURRENT_PATH=$(pwd)
for i in $(rpm -q gcc gcc-c++ kernel-devel openssl-devel zlib-devel popt-devel popt-static libnl-devel wget make |grep 'not installed' | awk '{print $2}')
do
yum -y install $i
done
[ -d /root/software ]
[ "$?" != 0 ] && mkdir /root/software
cd /root/software
[ !-e pcre-8.33.tar.gz ] && wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
tar -zxvf pcre-8.33.tar.gz
cd pcre-8.33
./configure
make && make install
echo $? || [ $? != 0] || echo" installation pcrefailed" || exit 1
cd /root/software
[ ! -e nginx-1.2.9.tar.gz ] && wget http://nginx.org/download/nginx-1.2.9.tar.gz
tar -zxvf nginx-1.2.9.tar.gz
cd nginx-1.2.9
./configure--prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module--with-http_gzip_static_module
make && make install
echo $? || [ $? != 0] || echo" installationnginxfailed" || exit 1
# -------------------------------------------------------- #
## Keepalived_intsall
# -------------------------------------------------------- #
# Keepalived installation
cd /root/softwarae
[ ! -e keepalived-1.2.4.tar.gz ] &&wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gz
tar -zxvf keepalived-1.2.4.tar.gz
cd keepalived-1.2.4
ln -s /usr/src/kernels/$(uname -r) /usr/src/kernels/linux
./configure --prefix=/usr--bindir=/usr/bin--sbindir=/usr/bin--libexecdir=/usr/libexec --localstatedir=/var --libdir=/lib64--infodir=/usr/share/info--sysconfdir=/etc --mandir=/usr/local/share/man--with-kernel-dir=/usr/src/kernels/linux
make && make install
echo $? || [ $? != 0] || print " installation keepalivedfailed" || exit 1
chkconfig --add keepalived
chkconfig --level 345 keepalived on
二、主Nginx 配置
[root@node5 conf]# mkdir -p /var/www/html
[root@node5 conf]# cat "192.168.122.5" > /var/www/html/index.html
[root@node5 conf]# vim nginx.conf
[root@node5 conf]# ../sbin/nginx -s reload
[root@node5 conf]# curl http://192.168.122.5
192.168.122.5
三、主Keepalived配置
[root@node6 conf]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
404060@qq.com
138162@139.com
}
notification_email_from 404060@qq.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/chk_nginx.keepalived.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass kuangling
}
track_script {
chk_nginx.keepalived
}
virtual_ipaddress {
192.168.122.15
}
}
四、备nginx配置
[root@node6 conf]# mkdir -p /var/www/html
[root@node6 conf]# cat "192.168.122.6" > /var/www/html/index.html
[root@node6 conf]# vim nginx.conf
[root@node6 conf]# ../sbin/nginx -s reload
[root@node6 conf]# curl http://192.168.122.6
192.168.122.6
五、备keepalived配置
[root@node6 conf]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
404060@qq.com
138162@139.com
}
notification_email_from 404060@qq.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/chk_nginx.keepalived.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass kuangling
}
track_script {
chk_nginx.keepalived
}
virtual_ipaddress {
192.168.122.15
}
}
六、分别在2台nginx服务器上添加检测脚本:
[root@node6 conf]# vim /etc/keepalived/chk_nginx.keepalived.sh
#!/bin/bash
# description:
# 定时查看 Nginx 是否存在,如果不存在则启动 Nginx
# 如果启动失败,则停止 keepalived
status=`ps -C nginx --no-header |wc -l`
if [ $status -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
[root@node6 conf]#chmod +x /etc/keepalived/chk_nginx.keepalived.sh
七、测试
分别在2台nginx上启动nginx和keepalived服务,然后分别用ip a 查看ip
来源:https://blog.51cto.com/kling/1240359
© 版权声明
博主的文章没有高度、深度和广度,只是凑字数。利用读书、参考、引用、抄袭、复制和粘贴等多种方式打造成自己的纯镀 24k 文章!如若有侵权,请联系博主删除。
喜欢就点个赞吧