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.27
主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 {
   xuezm@test.com
}
   notification_email_from xuezm@test.com
   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
interface eth0
virtual_router_id 52
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass yaoshi.com
}
virtual_ipaddress {
192.168.11.209
}
}

主Nginx之二(192.168.11.27)的keepalivd.conf配置文件如下:
[root@localhost keepalived]# cat keepalived.conf 
!Configuration File for keepalived
global_defs {
   notification_email {
   xuezm@test.com
}
   notification_email_from xuezm@test.com
   smtp_server mail.test.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass yaoshi
}
virtual_ipaddress {
192.168.11.208 
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass yaoshi
}
virtual_ipaddress {
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进程的高可用。