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

Nginx+Keepalived负载均衡高可用(双机热备)

时间:2015-04-14 16:50:58      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

Nginx+Keepalived负载均衡高可用(双机热备)
1.1 Nginx安装及配置
1.2 Keepalived安装及配置
1.3 WebServer安装
1.4 测试Nginx+Keepalived

环境如下:
CentOS 6.4_64K
eepalived-1.2.12
Nginx-1.4.4
vip:192.168.10.50
master:192.168.10.11
backup:192.168.10.12
webserver1:192.168.10.13
webserver2:192.168.10.14

1.1 Nginx 安装及配置
a.在192.168.10.11、192.168.10.12上,分别安装Nginx(可参考Nginx安装文档)
b.修改192.168.10.11、192.168.10.12上的Nginx配置文件如下所示:

user wwwwww;
worker_processes 8;
pid/usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events{
use epoll;
worker_connections 51200;
}
http{
include mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr-$remote_user[$time_local]"$request"‘
‘$status$body_bytes_sent"$http_referer"‘
‘"$http_user_agent""$http_x_forwarded_for"‘;
access_log /usr/local/nginx/logs/access.log main;
sendfile on;
keepalive_timeout 65;
client_header_buffer_size 32k;
large_client_header_buffers 432k;
client_max_body_size 10m;
gzip on;
gzip_min_length 1k;
gzip_buffers 416k;
gzip_http_version 1.1;
gzip_comp_level 5;
gzip_disable "MSIE[1-6]\.";
gzip_vary on;
gzip_types text/plainapplication/x-javascripttext/cssapplication/xml;
upstreambackend{
#ip_hash; #测试的时候,先注释掉
server192.168.10.13:80;
server192.168.10.14:80;
}
server{
listen 80;
server_name localhost;
location/{
proxy_redirect off;
proxy_set_header Host$host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;
proxy_pass http://backend;
}
}
}


1.2 Keepalived安装及配置
a.在192.168.10.11、192.168.10.12上,分别安装Keepalived(可参考Keepalived安装文档)。
b.修改192.168.10.11上的/etc/keepalived/keepalived.conf 文件,如下所示:
global_defs{
notification_email{
1244918797@qq.com
}
notification_email_fromtest@baba.io
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_idLVS_DEVEL
}
vrrp_instanceVI_1{
state BACKUP
interface eth0
virtual_router_id 100
priority 200
advert_int 1
nopreempt
authentication{
auth_type PASS
auth_pass 123456
}
virtual_ipaddress{
192.168.10.50
}
}
c.修改192.168.10.12上的/etc/keepalived/keepalived.conf文件,如下所示:
global_defs{
notification_email{
1244918797@qq.com
}
notification_email_fromtest@baba.io
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instanceVI_1{
state BACKUP
interface eth0
virtual_router_id 100
priority 200
advert_int 1
nopreempt
authentication{
auth_type PASS
auth_pass 123456
}
virtual_ipaddress{
192.168.10.50
}
}
d. 在192.168.10.11、192.168.10.12上,编写SHELL脚本,监测Nginx是否正常,如果Nginx
挂掉了,则停掉Keepalived
[root@localhost~]#vim/root/shell/nginx_check.sh
#!/bin/bash
while :
do
nginxcheck=`ps-Cnginx--no-header|wc-l`
keepalivedcheck=`ps-Ckeepalived--no-header|wc-l`
if[$nginxcheck-eq0];then
if[$keepalivedcheck-ne0];then
/etc/init.d/keepalivedstop
else
echo "keepalivedisstoped"
fi
elif[$keepalivedcheck-eq0];then
/etc/init.d/keepalivedstart
fi
sleep 5
done

e.设置开机自动执行
[root@localhost~]# echo "nohupsh/root/shell/nginx_check.sh&" >> /etc/rc.local

1.3 WebServer安装
为了方便演示,WebServer上只安装Apache(也可以安装Tomcat、Resin等),来提供Web
服务。
a.在192.168.10.13上安装Apache,并设置首页内容为自己的IP地址
[root@localhost~]# yum install httpd
[root@localhost~]# echo "<h1>192.168.10.13</h1>" > /var/www/html/index.html
[root@localhost~]# service httpd start
b.在192.168.10.14上安装Apache,并设置首页内容为自己的IP地址
[root@localhost~]# yum install httpd
[root@localhost~]# echo "<h1>192.168.10.14</h1>" > /var/www/html/index.html
[root@localhost~]# service httpd start


1.4 测试Nginx+Keepalived
完成上面步骤后,集群环境就搭建好了,下面就测试集群环境是否正常。
a.在192.168.10.11、192.168.10.12上,分别启动Nginx并开启监测
[root@localhost~]# /usr/local/nginx/sbin/nginx
[root@localhost~]# nohupsh/root/shell/nginx_check.sh&
b.测试方法如下:
1)打开浏览器,输入192.168.10.50后,是否轮回显示“192.163.10.13”或“192.163.10.14”
2)关闭192.168.10.11上的Nginx,Web服务是否还正常
3)关闭192.168.10.12上的Nginx,启动192.168.10.11上的Nginx,Web服务是否还正常
4)关闭192.168.10.13上的Apahce,Web服务是否还正常

Nginx+Keepalived负载均衡高可用(双机热备)

标签:

原文地址:http://blog.csdn.net/pa5201314/article/details/45042187

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