首页 > 代码库 > Nginx+Keepalived实现负载均衡

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        http://birdman1.sosoav.com;

            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: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> 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,省略配置

 

测试:访问http://birdman1.sosoav.com

由于配置client1client2的内容不同(便于区分),打开http://birdman1.7road.com后显示内容为

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: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> 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: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> 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://birdman1.sosoav.com 仍能访问。哈哈,成功了


本文出自 “我是一只小小鸟” 博客,请务必保留此出处http://2242558.blog.51cto.com/2232558/1545338

Nginx+Keepalived实现负载均衡