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: [email protected]
# 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 {
[email protected]
[email protected]
}
notification_email_from [email protected]
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配置
t@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 {
[email protected]
[email protected]
}
notification_email_from [email protected]
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
nginx+keepalived实现双机热备的高可用
64人收藏此文章, 我要收藏发表于15天前(2013-09-12 23:11) , 已有 664次阅读 ,共 1个评论
目录:[ - ]
keepalived安装
keepalived的配置
keepalived的测试
这篇文章简单介绍利用keepalived软件,实现对nginx服务器的高可用,即实现故障自动切换。假设你已经安装好nginx,下面介绍keepalived的安装和使用。
keepalived安装
yum install openssl-devel
cd /tmp
wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
tar xzf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure
make && make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
chmod +x /etc/init.d/keepalived
chkconfig --add keepalived
chkconfig keepalived on
mkdir /etc/keepalived
ln -s /usr/local/sbin/keepalived /usr/sbin/
keepalived的配置
更详细的keepalived配置文件说明可以执行man keepalived.conf查看。
我们假设主服务器IP:192.168.1.103,从服务器ip:192.168.1.101 虚拟ip:192.168.1.110
下面对主服务器的keepalived进行配置:
vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/opt/nginx_pid.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER ############ 辅机为 BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.1.103
priority 102 ########### 权值要比 back 高
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ### 执行监控的服务
}
virtual_ipaddress {
192.168.1.110
}
}
从服务器:
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/opt/nginx_pid.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.1.101
priority 101 ##########权值 要比 master 低。。
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ### 执行监控的服务
}
virtual_ipaddress {
192.168.1.110
}
}
之后分别在主从服务器建立nginx的监控脚本:
vi /opt/nginx_pid.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -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
然后分别启动主从服务器的keepalived:
service keepalived start
keepalived的测试
我们在主服务器上执行命令ip a,显示如下:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:aa:a1:e4 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.103/24 brd 255.255.255.255 scope global eth0
inet 192.168.1.110/32 scope global eth0
证明主服务器已经绑定了虚拟ip 192.168.1.110
在从服务器上执行命令ip a,显示如下:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:2b:94:3b brd ff:ff:ff:ff:ff:ff
inet 192.168.1.101/24 brd 255.255.255.255 scope global eth0
显示表明从服务器上没有绑定vip 192.168.1.110,只有本机真实ip192.168.1.101
下面我们停止主服务器的nginx进程,再看看ip绑定情况:
主服务器的情况:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:aa:a1:e4 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.103/24 brd 255.255.255.255 scope global eth0
从服务器的情况:
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:2b:94:3b brd ff:ff:ff:ff:ff:ff
inet 192.168.1.101/24 brd 255.255.255.255 scope global eth0
inet 192.168.1.110/32 scope global eth0
由此可见vip已经指向了从服务器。