测试环境如下:

系统: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)
fori 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
cdpcre-8.33
./configure
make&& makeinstall
echo$? || [ $? != 0] || echo" installation pcrefailed"|| exit1
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
cdnginx-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&& makeinstall
echo$? || [ $? != 0] || echo" installationnginxfailed"|| exit1
# -------------------------------------------------------- #
## 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
cdkeepalived-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&& makeinstall
echo$? || [ $? != 0] || print " installation keepalivedfailed"|| exit1
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主从双机热备自动切换解决方案_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 File forkeepalived
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主从双机热备自动切换解决方案_服务器_02

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 File forkeepalived
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
}
}


六、分别在2nginx服务器上添加检测脚本:

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 -eq0 ];then
/usr/local/nginx/sbin/nginx
sleep3
if[ `ps-C nginx --no-header |wc-l` -eq0 ];then
killall keepalived
fi
fi
[root@node6 conf]#chmod +x /etc/keepalived/chk_nginx.keepalived.sh


七、测试

分别在2nginx上启动nginxkeepalived服务,然后分别用ip a 查看ip

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