Chinaunix首页 | 论坛 | 博客
  • 博客访问: 179406
  • 博文数量: 42
  • 博客积分: 315
  • 博客等级: 二等列兵
  • 技术积分: 461
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-16 11:55
文章分类

全部博文(42)

文章存档

2013年(29)

2012年(7)

2011年(6)

我的朋友

分类: LINUX

2012-01-11 15:27:27

                             Nginx+ keepalived 主备负载均衡

 

OS:Centos 5.7

kernel:2.6.18-274.el5

主:10.10.8.69

备:10.10.8.70

VIP:10.10.8.71

 

client1:10.10.8.72

client2:10.10.8.73

 

 

一,安装gcc等环境

二,下载软件,nginx pcre

nginx:nginx-1.1.11.tar.gz

pcre:pcre-8.21.tar.gz

keepalived-1.1.19.tar.gz

三,安装pcre

解压

tar zxvf pcre-8.21.tar.gz

cd pcre-8.21

编译前的配置:

./configure

配置:

make

安装:

make install

 

四:安装nginx

 

解压:

tar zxv nginx-1.1.11.tar.gz

cd nginx-1.1.11

编译前的配置

./configure --user=www --group=www --prefix=/usr/local/nginx \

--with-http_stub_status_module \

--with-http_ssl_module

make&&make install

配置Nginx

 

vi /usr/local/nginx/conf/nginx.conf

 

user  www;

worker_processes  8;

error_log  logs/error.log;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;    sendfile        on;

    keepalive_timeout  65;

#gzip  on;

    upstream  birdman1.7road.com   {

              server   10.10.8.72:80;

              server   10.10.8.73:80;

    }

    server {

        listen       80;

 server_name  birdman1.7road.com;

        charset utf-8;

        location / {

            root   html;

 index  index.html index.htm;

            proxy_pass       

            proxy_set_header  X-Real-IP  $remote_addr;

            client_max_body_size  100m;

        }

        location ~ ^/(WEB-INF)/ {

     deny all;

        }        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }    }

}

 

注:10.10.8.7210.10.8.73apache后端服务器。

 

 

五:开启转发功能:

sed -i 's#net.ipv4.ip_forward = 0#net.ipv4.ip_forward = 1#' /etc/sysctl.conf

sysctl -p

 

 

 

六,安装keepalived

tar zxvf keepalived-1.1.19.tar.gz

cd keepalived-1.1.19

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

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/

 

1)主服务器配置:

 

vi /etc/keepalived/keepalived.conf

 

#################################[MASTER]##############################################

! Configuration File for keepalived

global_defs {

    notification_email {

    64125568@qq.com

         }

    notification_email_from birdsdeng@163.com

    smtp_server 127.0.0.1

    smtp_connect_timeout 30

    router_id LVS_DEVEL

 }

 

 vrrp_instance VI_1 {

     state MASTER

     interface eth0

     virtual_router_id 51

     #define nginx-master-IP

     mcast_src_ip 10.10.8.69 #本机IP

     priority 100

     advert_int 1

     authentication {

     auth_type PASS

     auth_pass jesse

     }

 

     #define vip

     virtual_ipaddress {

             10.10.8.71   #VIP IP10.10.8.71

                       }

               }

####################################################################################

 

注:设置masterpriority100

 

2)备服务器上配置:

###################################[BACKUP]###########################################

! Configuration File for keepalived

global_defs {

    notification_email {

    64125568@qq.com

       }

       notification_email_from birdsdeng@163.com

       smtp_server 127.0.0.1

       smtp_connect_timeout 30

       router_id LVS_DEVEL

      }

 

vrrp_instance VI_1 {

     state BACKUP

     interface eth0

     virtual_router_id 51

     #define nginx-backup-ip

     mcast_src_ip 10.10.8.70 #本机IP

     priority 99

     advert_int 1

     authentication {

     auth_type PASS

     auth_pass jesse

    }

virtual_ipaddress {

         10.10.8.71 #VIP ip

     }

 }

###################################[BACKUP]###########################################

 

注:设置backuppriority99

 

七,启动keepalived

启动keepalived和加入开机自动启动

/etc/init.d/keepalived start

echo "/etc/init.d/keepalived start" >>/etc/rc.local

 

 

命令检查vrrp是否启动

[root@birdman1 soft]# ip add

1: lo: mtu 16436 qdisc noqueue

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000

    link/ether 00:0c:29:04:d9:b0 brd ff:ff:ff:ff:ff:ff

    inet 10.10.8.69/24 brd 10.10.8.255 scope global eth0

    inet 10.10.8.71/32 scope global eth0

 

 

九:监控nginx:此在

vi /shell/nginx_pid.sh

###################################################

#!/bin/bash

while :

    do

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

 

 if [ $nginxpid -eq 0 ]

   then

   /usr/local/nginx/sbin/nginx

   sleep 5

 if [ $nginxpid -eq 0 ]

    then

    /etc/init.d/keepalived stop

    fi

    fi

    sleep 5

done

###################################################

nohup /bin/bash /shell/nginx_pid.sh &

 

如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,

那么可以使用nohup命令。该命令可以在你退出root帐户之后继续运行相应的进程.

 

10.10.8.7210.10.8.73为后端apache,省略配置

 

测试:访问

由于配置client1client2的内容不同(便于区分),打开后显示内容为

10.10.8.7210.10.8.73

 

假设10.10.8.69(主)的nginx挂掉:

1,停掉主上得nginx

[root@birdman1 ~]# killall nginx

 

backup上查看:

[root@eklfj-2edbdf6c2 ~]# ip add
1: lo: mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:22:31:e3 brd ff:ff:ff:ff:ff:ff
    inet 10.10.8.70/24 brd 10.10.8.255 scope global eth0
[root@eklfj-2edbdf6c2 ~]# ip add
1: lo: mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:22:31:e3 brd ff:ff:ff:ff:ff:ff
    inet 10.10.8.70/24 brd 10.10.8.255 scope global eth0
    inet 10.10.8.71/32 scope global eth0

以上可见,backup服务器上多了IP10.10.8.71

 

然后再次访问 仍能访问。哈哈,成功了。

 

参考:http://blog.163.com/jesse_linux/blog/static/171647142201111206556443/

阅读(1955) | 评论(0) | 转发(0) |
0

上一篇:web压力测试

下一篇:可用网址

给主人留下些什么吧!~~