天天看點

Nginx+keepalived主主負載均衡伺服器

Nginx+keepalived主主負載均衡伺服器

測試實驗環境:

主Nginx之一:192.168.11.27

主Nginx之二:192.168.11.28

Web伺服器一:192.168.11.37

Web伺服器二:192.168.11.38

VIP位址一:192.168.11.208

VIP位址二:192.168.11.209

環境:

以下環境均使用的是centos 5.7 x86_64位系統

[root@localhost ~]# lsb_release  -a

LSB Version:    :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch

Distributor ID:    CentOS

Description:    CentOS release 5.7 (Final)

Release:    5.7

Codename:    Final

[root@localhost ~]# uname  -a

Linux localhost 2.6.18-274.el5 #1 SMP Fri Jul 22 04:43:29 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux

安裝nginx負載均衡伺服器

#添加運作nginx的使用者群組www 

groupadd www  

useradd -g www www  

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz 

tar zxvf pcre-7.8.tar.gz 

cd pcre-7.8/ 

./configure 

make && make install 

wget http://sysoev.ru/nginx/nginx-0.7.51.tar.gz 

tar zxvf nginx-0.7.51.tar.gz 

cd nginx-0.7.51/ 

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module 

make && make install

配置nginx負載均衡器的配置檔案vim /usr/local/nginx/conf/nginx.conf,此篇文章僅僅隻是我的某項目的配置文檔,純80轉發;

主Nginx之二:192.168.11.28  均需要安裝,相同即可

[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf

user www www;

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;

server_names_hash_bucket_size 128;

client_header_buffer_size 32k;

large_client_header_buffers 4 32k;

client_max_body_size 8m;

sendfile on;

tcp_nopush     on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

gzip on;

gzip_min_length 1k;

gzip_buffers     4 16k;

gzip_http_version 1.0;

gzip_comp_level 2;

gzip_types       text/plain application/x-javascript text/css application/xml;

gzip_vary on;

upstream backend

ip_hash;

server 192.168.11.37:80;

server 192.168.11.38:80;

server {

listen 80;

server_name www.test.com;

location / {

root /var/www/html ;

index index.php index.htm index.html;

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;

location /nginx {

access_log off;

auth_basic "NginxStatus";

#auth_basic_user_file /usr/local/nginx/htpasswd;

log_format access '$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 access;

test配置檔案  /usr/local/nginx/sbin/nginx -t

啟動 /usr/local/nginx/sbin/nginx

安裝keepalived,分别作web及nginx的HA

wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz 

tar zxvf keepalived-1.1.15.tar.gz 

cd keepalived-1.1.15 

./configure --prefix=/usr/local/keepalived 

make  

make install 

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ 

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ 

mkdir /etc/keepalived 

cd /etc/keepalived/

主nginx之一(192.168.11.27)機器keepalived.conf配置檔案如下:

[root@localhost keepalived]# cat keepalived.conf

!Configuration File for keepalived

global_defs {

   notification_email {

   [email protected]

   notification_email_from [email protected]

   smtp_server mail.test.com

   smtp_connect_timeout 30

   router_id LVS_DEVEL

vrrp_instance VI_1 {

state MASTER

interface eth0

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass yaoshi.com

virtual_ipaddress {

192.168.11.208

vrrp_instance VI_2 {

state BACKUP

virtual_router_id 52

priority 99

192.168.11.209

主Nginx之二(192.168.11.27)的keepalivd.conf配置檔案如下:

[root@localhost keepalived]# cat keepalived.conf 

auth_pass yaoshi

192.168.11.208 

192.168.11.209 

啟動服務  /etc/init.d/keepalived start

使用 ip a 檢視vip是否啟動

我們分别在二台主Nginx上執行,指令如下所示:

nohup sh /root/nginxpid.sh & 

複制代碼

腳本内容如下:

vi /root/nginxpid.sh

#!/bin/bash 

while  : 

do 

nginxpid=`ps -C nginx --no-header | wc -l` 

if [ $nginxpid -eq 0 ];then 

  /usr/local/nginx/sbin/nginx 

  sleep 1 

   if [ $nginxpid -eq 0 ];then 

   /etc/init.d/keepalived stop 

   fi 

fi 

sleep 1  

done

此腳本我是直接從生産伺服器上下載下傳的,大

家不要懷疑它會引起死循環和有效性的問題,稍為

解釋一下,這是一個無限循環的腳本,放在主Nginx機器

上(因為目前主要是由它提供服務),每隔1秒執行一次,用

ps -C 指令來收集nginx的PID值到底是否為0,如果是0的話(即Nginx

程序死掉了),嘗試啟動nginx程序;如果繼續為0,即nginx啟

動失改,則關閉本機的Keeplaived程序,VIP位址則會由

備機接管,當然了,整個網站就會由備機的Nginx

來提供服務了,這樣保證Nginx程序的高可用。