Nginx+keepalived 脚本安装主从双机热备自动切换解决方案

Nginx+keepalived 脚本安装主从双机热备自动切换解决方案 Nginx+keepalived 脚本安装主从双机热备自动切换解决方案

2013-07-02 19:24:13

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://kling.blog.51cto.com/3320545/1240359

Nginx+keepalived主从双机热备自动切换解决方案

测试环境如下:

系统:Ceentos 6.4 64位

主nginx服务器:192.168.122.5

备nginx服务器:192.168.122.6

VIP:192.168.122.15

一、Nginx+keepalived安装—脚本安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/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 wgetmake
|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 ] && wgetftp://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
配置

1
2
3
[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

Nginx+keepalived 脚本安装主从双机热备自动切换解决方案

1
2
3
[root@node5 conf]# ../sbin/nginx -s reload
[root@node5 conf]# curl http://192.168.122.5
192.168.122.5

三、主Keepalived配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@node6 conf]# vim /etc/keepalived/keepalived.conf
! Configuration Filefor
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配置

1
2
3
[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

Nginx+keepalived 脚本安装主从双机热备自动切换解决方案

1
2
3
[root@node6 conf]# ../sbin/nginx -s reload
[root@node6 conf]# curl http://192.168.122.6
192.168.122.6

五、备keepalived配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@node6 conf]# vim /etc/keepalived/keepalived.conf
! Configuration Filefor
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服务器上添加检测脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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

本文出自 “&思远晨曦” 博客,请务必保留此出处http://kling.blog.51cto.com/3320545/1240359

注:

1.这篇文章是keepalived单主高可用配置

2.keepalived没有解决自身故障的问题,更多是针对操作系统和网络故障来进行vip迁移

3.keepalived没有和nginx联动,也就是说nginx故障的情况下,keepalived不会知道,也就不会迁移vip,这个问题的解决方法:将监控脚步长期在后台持续运行(while无限循环,sleep 1)

脚步改造如下:

#!/bin/bash

# description:

# 定时查看Nginx是否存在,如果不存在则启动Nginx

# 如果启动失败,则停止keepalived

while [ a==a ]

do

status=`ps -C nginx --no-header |wc -l`

if [ $status -eq 0 ];then

/usr/local/nginx/sbin/nginx > /dev/null 2>&1

sleep 3

if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then

killall keepalived > /dev/null 2>&1

fi

fi

sleep 1

done

上一篇:ubutu16.04 安装Tenda u12无线网卡驱动


下一篇:Day04.a(对象类型的转换,多态)