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
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  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 配置

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

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  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配置

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

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

 

六、分别在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 - 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

 

七、测试

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

 

 

 

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

 

 

nginx+keepalived实现双机热备的高可用

64人收藏此文章, 我要收藏发表于15天前(2013-09-12 23:11) , 已有 664次阅读 ,共 1个评论

这篇文章简单介绍利用keepalived软件,实现对nginx服务器的高可用,即实现故障自动切换。假设你已经安装好nginx,下面介绍keepalived的安装和使用。

keepalived安装

  1. yum install openssl-devel
  2. cd /tmp
  3. wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
  4. tar xzf keepalived-1.2.2.tar.gz
  5. cd keepalived-1.2.2
  6. ./configure
  7. make && make install
  8. cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
  9. cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
  10. chmod +x /etc/init.d/keepalived
  11. chkconfig --add keepalived
  12. chkconfig keepalived on
  13. mkdir /etc/keepalived
  14. 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进行配置:

  1. vi /etc/keepalived/keepalived.conf
  1. global_defs {
  2.    notification_email {
  3.      admin@centos.bz
  4.    }
  5.    notification_email_from keepalived@domain.com
  6.    smtp_server 127.0.0.1
  7.    smtp_connect_timeout 30
  8.    router_id LVS_DEVEL
  9. }
  10. vrrp_script chk_http_port {
  11.                 script "/opt/nginx_pid.sh"
  12.                 interval 2
  13.                 weight 2
  14. }
  15. vrrp_instance VI_1 {
  16.     state MASTER        ############ 辅机为 BACKUP
  17.     interface eth0
  18.     virtual_router_id 51
  19.     mcast_src_ip 192.168.1.103
  20.     priority 102                  ########### 权值要比 back 高
  21.     advert_int 1
  22.     authentication {
  23.         auth_type PASS
  24.         auth_pass 1111
  25.     }
  26. track_script { 
  27.         chk_http_port ### 执行监控的服务 
  28.         }
  29.     virtual_ipaddress {
  30.        192.168.1.110
  31.     }
  32. }

从服务器:

  1. global_defs {
  2.    notification_email {
  3.      admin@centos.bz
  4.    }
  5.    notification_email_from keepalived@domain.com
  6.    smtp_server 127.0.0.1
  7.    smtp_connect_timeout 30
  8.    router_id LVS_DEVEL
  9. }
  10. vrrp_script chk_http_port {
  11.                 script "/opt/nginx_pid.sh"
  12.                 interval 2
  13.                 weight 2
  14. }
  15. vrrp_instance VI_1 {
  16.     state BACKUP
  17.     interface eth0
  18.     virtual_router_id 51
  19.     mcast_src_ip 192.168.1.101
  20.     priority 101              ##########权值 要比 master 低。。
  21.     advert_int 1
  22.     authentication {
  23.         auth_type PASS
  24.         auth_pass 1111
  25.     }
  26. track_script { 
  27.         chk_http_port ### 执行监控的服务 
  28.         }
  29.     virtual_ipaddress {
  30.        192.168.1.110
  31.     }
  32. }

之后分别在主从服务器建立nginx的监控脚本:

  1. vi /opt/nginx_pid.sh
  1. #!/bin/bash
  2. A=`ps -C nginx --no-header |wc -l`               
  3. if [ $A -eq 0 ];then                                       
  4.                 /usr/local/nginx/sbin/nginx
  5.                 sleep 3
  6.                 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
  7.                        killall keepalived
  8.                 fi
  9. fi

然后分别启动主从服务器的keepalived:

  1. service keepalived start

keepalived的测试

我们在主服务器上执行命令ip a,显示如下:

  1. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
  2.     link/ether 00:0c:29:aa:a1:e4 brd ff:ff:ff:ff:ff:ff
  3.     inet 192.168.1.103/24 brd 255.255.255.255 scope global eth0
  4.     inet 192.168.1.110/32 scope global eth0

证明主服务器已经绑定了虚拟ip 192.168.1.110
在从服务器上执行命令ip a,显示如下:

  1. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
  2.     link/ether 00:0c:29:2b:94:3b brd ff:ff:ff:ff:ff:ff
  3.     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绑定情况:
主服务器的情况:

  1. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
  2.     link/ether 00:0c:29:aa:a1:e4 brd ff:ff:ff:ff:ff:ff
  3.     inet 192.168.1.103/24 brd 255.255.255.255 scope global eth0

从服务器的情况:

  1. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
  2.     link/ether 00:0c:29:2b:94:3b brd ff:ff:ff:ff:ff:ff
  3.     inet 192.168.1.101/24 brd 255.255.255.255 scope global eth0
  4.     inet 192.168.1.110/32 scope global eth0

由此可见vip已经指向了从服务器。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值