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; #測試的時候,先注釋掉 </blockquote> <blockquote> server192.168.10.13:80; </blockquote> <blockquote> server192.168.10.14:80; </blockquote>
}</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 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!