nginx+keepalive的负载均衡的高可用

一、环境

Centos 6.7  nginx1.6.3 

web01ip:172.16.1.7(内网)

web02ip:172.16.1.8(内网)

web搭建三个网站:blog、bbs、www

主的lb01ip:10.0.0.5/24(外网)

备的lb02ip:10.0.0.6/24(外网)

vip:10.0.0.4/24


二、负载均衡的配置

主配置文件:nginx.conf

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    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  /app/logs/access.log  main;

    error_log   /app/logs/error.log;

    upstream blog_server_pools {

    ip_hash;

    server 172.16.1.7:80;

    server 172.16.1.8:80;

}

    upstream bbs_server_pools {

    ip_hash;

    server 172.16.1.7:80;

    server 172.16.1.8:80;

}

    upstream www_server_pools {

    ip_hash;

    server 172.16.1.7:80;

    server 172.16.1.8:80;

}

    include extra/lb_blog.conf;

    include extra/lb_bbs.conf;

    include extra/lb_www.conf;

}

server {

        listen       80;

        server_name  blog.etiantian.org;

        location / {

          proxy_pass http://blog_server_pools;

          proxy_set_header Host      $host;

          proxy_set_header X-Forwarded-For $remote_addr;

         }

}

~

分支配置         

[root@lb01 extra]# cat lb_blog.conf 

server {

        listen       80;

        server_name  blog.etiantian.org;

        location / {

          proxy_pass http://blog_server_pools;

          proxy_set_header Host      $host;

          proxy_set_header X-Forwarded-For $remote_addr;  

         }

}

其他两个分配置同上


三、keepalive安装配置

1.yum -y insatll keepalive

2.配置keepalived.conf

[root@lb01 keepalived]# cat keepalived.conf

global_defs {

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_01

}


vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 150

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        10.0.0.4/24

    }

}


[root@lb02 keepalived]# cat keepalived.conf

global_defs {

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_02

}


vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        10.0.0.4/24

    }

}


3.主备两台服务开启

/application/nginx/sbin/nginx

/etc/init.d/keepalive start


四、测试

首先在两台负载均衡器上查看ip

[root@lb01 ~]# ip addr|grep 10.0.0.4

    inet 10.0.0.4/24 scope global secondary eth0

[root@lb02 ~]# ip addr|grep 10.0.0.4

然后把主负载均衡关闭

[root@lb01 ~]# /etc/init.d/keepalived stop

Stopping keepalived:                                       [  OK  ]

最后查看备份负载均衡上的IP

[root@lb02 ~]# ip addr|grep 10.0.0.4

    inet 10.0.0.4/24 scope global secondary eth0

发现虚拟IP已经跳转过来。


五、知识拓展

弄个监控脚本来监测nginx的服务,一旦主机的nginx停止掉,会马上停止主机的keepalived,然后切换到备机。

 vim check_nginx.sh  

###############监测nginx服务切换备机#############

#############write by chunchun at 20151203#########

#!/bin/bash

#

#!/bin/bashi

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


if [ $nginx -eq 0 ]

then

        /application/nginx/sbin/nginx  >> /dev/null

        echo "nginx 已经正常重启工作中"

        sleep 3

else

        echo "nginx 正常运行中..."


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


        if [ $keepalived -eq 0 ]

        then

                /etc/init.d/keepalived stop >> /dev/null

                echo "已经正常停止keepalived服务"

        else

                echo "keepalived 仍处于运行状态..."

        fi

fi

让脚本每隔多久监测一次

15 * * * * /server/scripts/check_nginx.sh &> /dev/null


实现Nginx+Keepalived中Nginx进程的高可用

[root@lb01 scripts]# crontab -e

############nginx+keepalive监控nginx服务高可用##########

*/15 * * * * sh /server/scripts/check_nginx.sh >/dev/null 2>&1

你可能感兴趣的:(nginx,高可用,负载均衡,KeepAlive)