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

释放双眼,带上耳机,听听看~!

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
1--------------------- #
2## Nginx_install
3# -------------------------------------------------------- #
4# Nginx installation
5#CURRENT_PATH=$(pwd)
6for 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}')
7do
8yum -y install $i
9done
10[ -d /root/software ]
11[ "$?" != 0 ] && mkdir /root/software
12cd /root/software
13[ !-e pcre-8.33.tar.gz ] && wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
14tar -zxvf pcre-8.33.tar.gz
15cd pcre-8.33
16./configure
17make && make install
18echo $? || [ $? != 0] || echo" installation pcrefailed" || exit 1
19cd /root/software
20[ ! -e nginx-1.2.9.tar.gz ] && wget http://nginx.org/download/nginx-1.2.9.tar.gz
21tar -zxvf nginx-1.2.9.tar.gz
22cd nginx-1.2.9
23./configure--prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module--with-http_gzip_static_module
24make && make install
25echo $? || [ $? != 0] || echo" installationnginxfailed" || exit 1
26# -------------------------------------------------------- #
27## Keepalived_intsall
28# -------------------------------------------------------- #
29# Keepalived installation
30cd /root/softwarae
31[ ! -e keepalived-1.2.4.tar.gz ] &&wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gz
32tar -zxvf keepalived-1.2.4.tar.gz
33cd keepalived-1.2.4
34ln -s /usr/src/kernels/$(uname -r) /usr/src/kernels/linux
35./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
36make && make install
37echo $? || [ $? != 0] || print " installation keepalivedfailed" || exit 1
38chkconfig --add keepalived
39chkconfig --level 345 keepalived on
40
41

二、主Nginx 配置


1
2
3
4
5
6
1[root@node5 conf]# mkdir -p /var/www/html
2[root@node5 conf]# cat "192.168.122.5" > /var/www/html/index.html
3[root@node5 conf]# vim nginx.conf
4
5
6

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


1
2
3
4
5
6
1[root@node5 conf]# ../sbin/nginx -s reload
2[root@node5 conf]# curl http://192.168.122.5
3192.168.122.5
4
5
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
35
36
1[root@node6 conf]# vim /etc/keepalived/keepalived.conf
2! Configuration File for keepalived
3global_defs {
4notification_email {
5404060@qq.com
6138162@139.com
7}
8notification_email_from 404060@qq.com
9smtp_server 127.0.0.1
10smtp_connect_timeout 30
11router_id LVS_DEVEL
12}
13vrrp_script chk_nginx {
14script "/etc/keepalived/chk_nginx.keepalived.sh"
15interval 2
16weight 2
17}
18vrrp_instance VI_1 {
19state MASTER
20interface eth0
21virtual_router_id 51
22priority 200
23advert_int 1
24authentication {
25auth_type PASS
26auth_pass kuangling
27}
28track_script {
29chk_nginx.keepalived
30}
31virtual_ipaddress {
32192.168.122.15
33}
34}
35
36

四、备nginx配置


1
2
3
4
5
1[root@node6 conf]# mkdir -p /var/www/html
2[root@node6 conf]# cat "192.168.122.6" > /var/www/html/index.html
3[root@node6 conf]# vim nginx.conf
4
5

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


1
2
3
4
5
1[root@node6 conf]# ../sbin/nginx -s reload
2[root@node6 conf]# curl http://192.168.122.6
3192.168.122.6
4
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
35
36
1[root@node6 conf]# vim /etc/keepalived/keepalived.conf
2! Configuration File for keepalived
3global_defs {
4notification_email {
5404060@qq.com
6138162@139.com
7}
8notification_email_from 404060@qq.com
9smtp_server 127.0.0.1
10smtp_connect_timeout 30
11router_id LVS_DEVEL
12}
13vrrp_script chk_nginx {
14script "/etc/keepalived/chk_nginx.keepalived.sh"
15interval 2
16weight 2
17}
18vrrp_instance VI_1 {
19state BACKUP
20interface eth0
21virtual_router_id 51
22priority 150
23advert_int 1
24authentication {
25auth_type PASS
26auth_pass kuangling
27}
28track_script {
29chk_nginx.keepalived
30}
31virtual_ipaddress {
32192.168.122.15
33}
34}
35
36

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1[root@node6 conf]# vim /etc/keepalived/chk_nginx.keepalived.sh
2#!/bin/bash
3# description:
4# 定时查看Nginx是否存在,如果不存在则启动Nginx
5# 如果启动失败,则停止keepalived
6status=`ps -C nginx --no-header |wc -l`
7if [ $status -eq 0 ];then
8/usr/local/nginx/sbin/nginx
9sleep 3
10if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
11killall keepalived
12fi
13fi
14[root@node6 conf]#chmod +x /etc/keepalived/chk_nginx.keepalived.sh
15
16

七、测试

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

给TA打赏
共{{data.count}}人
人已打赏
安全技术安全运维

Windows服务器如何发现被黑

2018-5-20 12:24:31

安全技术

详解Node.js API系列 Crypto加密模块

2021-12-21 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索