码迷,mamicode.com
首页 > 其他好文 > 详细

nginx+keepalived构建高可用服务

时间:2016-07-12 21:31:15      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

1.整体环境规划

技术分享

虚拟IP:10.0.4.248
主Nginx:10.0.4.249
备用Nginx:10.0.4.250

2.keepalived安装

#cd /usr/local/src
#wget http://www.keepalived.org/software/keepalived-1.2.22.tar.gz
#./configure --prefix=/usr/local/keepalived
#make & make install

 

3.master服务keepalived.conf配置

! Configuration File for keepalived

global_defs {

 router_id edu-proxy-01

}

vrrp_script chk_nginx {

 script "/etc/keepalived/nginx_check.sh"

 interval 2

 weight -20

}

vrrp_instance VI_1 {

 state MASTER

 interface eth0

 virtual_router_id 51

 mcast_src_ip 10.0.4.249

 priority 100

 nopreempt

 advert_int 1

 authentication {

 auth_type PASS

 auth_pass 1111

 }

 track_script {

 chk_nginx 

}

 virtual_ipaddress {

 10.0.4.248

 }

}

 

4.backerup服务keepalived.conf配置

! Configuration File for keepalived

global_defs {

 router_id edu-proxy-02

}

vrrp_script chk_nginx {

 script "/etc/keepalived/nginx_check.sh"

 interval 2

 weight -20

}

vrrp_instance VI_1 {

 state BACKUP

 interface eth0

 virtual_router_id 51

 mcast_src_ip 10.0.4.250

 priority 90

 advert_int 1

 authentication {

 auth_type PASS

 auth_pass 1111

 }

 track_script {

 chk_nginx 

}

 virtual_ipaddress {

 10.0.4.248

 }

}

 

5.nginx_check.sh脚本编写

 

#!/bin/bash 

A=`ps -C nginx –no-header |wc -l`

if [ $A -eq 0 ];then

 /usr/local/openrestry/nginx/sbin/nginx //Nginx安装路径

 sleep 2

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

 killall keepalived

 fi

fi

 

6.启动keepalived

/usr/local/keepalived/sbin/keepalived

[root@localhost sbin]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

 inet 127.0.0.1/8 scope host lo

 inet6 ::1/128 scope host 

valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

 link/ether 00:0c:29:c6:9b:91 brd ff:ff:ff:ff:ff:ff

 inet 10.0.4.249/24 brd 10.0.4.255 scope global eth0

 inet 10.0.4.248/32 scope global eth0

 inet6 fe80::20c:29ff:fec6:9b91/64 scope link 

valid_lft forever preferred_lft forever

 

 
备用

[root@localhost keepalived]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

 inet 127.0.0.1/8 scope host lo

 inet6 ::1/128 scope host 

valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

 link/ether 00:0c:29:75:6c:82 brd ff:ff:ff:ff:ff:ff

 inet 10.0.4.250/24 brd 10.0.4.255 scope global eth0

 inet 10.0.4.248/32 scope global eth0

 inet6 fe80::20c:29ff:fe75:6c82/64 scope link 

valid_lft forever preferred_lft forever

 

 

 

nginx+keepalived构建高可用服务

标签:

原文地址:http://www.cnblogs.com/sagech/p/5664818.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!