Nginx+Keepalived负载均衡高可用(双机热备)
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;}</blockquote>server{listen 80;server_name localhost;location/{proxy_redirect off;</blockquote>proxy_set_header Host$host;</blockquote>proxy_set_header X-Real-IP $remote_addr;</blockquote>proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;</blockquote>proxy_pass http://backend;</blockquote>}}</blockquote> }
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</blockquote> }
vrrp_instanceVI_1{
state BACKUP
interface eth0
virtual_router_id 100
priority 200
advert_int 1
nopreempt
authentication{auth_type PASSauth_pass 123456}
virtual_ipaddress{192.168.10.50}</blockquote> }
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</blockquote> }
vrrp_instanceVI_1{
state BACKUP
interface eth0
virtual_router_id 100
priority 200
advert_int 1
nopreempt
authentication{auth_type PASSauth_pass 123456}
virtual_ipaddress{192.168.10.50}</blockquote> }
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];thenif[$keepalivedcheck-ne0];then/etc/init.d/keepalivedstop</blockquote>elseecho "keepalivedisstoped"</blockquote>fielif[$keepalivedcheck-eq0];then/etc/init.d/keepalivedstartfi
sleep 5</blockquote> 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服务是否还正常
来自:http://blog.csdn.net/pa5201314/article/details/45042187
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!