最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

nginx+keepalived实现高可用负载均衡方案

来源:动视网 责编:小OO 时间:2025-09-23 23:54:12
文档

nginx+keepalived实现高可用负载均衡方案

学习文档目录1.引言32.环境说明33.Nginx安装配置34.Keepalived安装配置55.验证91.引言本学习文档主要介绍了采用Nginx负载均衡,通过keepalived实现Nginx双机互备,保证实现的WEB服务高可用方案。2.环境说明主nginx负载均衡器:172.20.52.20端口81(CentOSrelease5.8)副nginx负载均衡器:172.20.52.21端口81(CentOSrelease5.8)Tomcat1:172.20.52.19端口3030Tomcat2
推荐度:
导读学习文档目录1.引言32.环境说明33.Nginx安装配置34.Keepalived安装配置55.验证91.引言本学习文档主要介绍了采用Nginx负载均衡,通过keepalived实现Nginx双机互备,保证实现的WEB服务高可用方案。2.环境说明主nginx负载均衡器:172.20.52.20端口81(CentOSrelease5.8)副nginx负载均衡器:172.20.52.21端口81(CentOSrelease5.8)Tomcat1:172.20.52.19端口3030Tomcat2
学习文档

   

目 录

1.    引言    3

2.    环境说明    3

3.    Nginx安装配置    3

4.  Keepalived安装配置    5

5.    验证    9

1.引言

本学习文档主要介绍了采用Nginx负载均衡,通过keepalived实现Nginx双机互备,保证实现的WEB服务高可用方案。

2.环境说明

主nginx负载均衡器:172.20.52.20 端口81(CentOS release 5.8)

副nginx负载均衡器:172.20.52.21 端口81(CentOS release 5.8)

Tomcat1: 172.20.52.19端口3030

Tomcat2: 172.20.52.20端口4040

VIP:172.20.52.22

软件:keepalived- 1.2.12  nginx-1.4.4

说明:keepalived 是一个基于VRRP协议来实现的WEB服务高可用方案,可以利用其来避免单点故障。一个WEB服务至少会有2台服务器运行Keepalived,一台为主服务器(MASTER),一台为备份服务器(BACKUP),但是对外表现为一个虚拟IP,主服务器会发送特定的消息给备份服务器,当备份服务器收不到这个消息的时候,即主服务器宕机的时候,备份服务器就会接管虚拟IP,继续提供服务,从而保证了高可用性。

3.Nginx安装配置

1.安装Nginx

  获取Nginx稳定版,把 Nginx 安装到 /usr/local/nginx 目录下(两台机器都安装)的详细步骤:

    yum –y install gcc openssl-devel pcre-devel zlib-devel(安装相关组件)

    tar zxvf nginx-1.4.4.tar.gz

cd nginx-1.4.4

./configure 

--prefix=/usr/local/nginx 

--with-http_ssl_module 

--with-http_flv_module 

--with-http_gzip_static_module 

--with-http_stub_status_module 

make && make install

2.分别在两台服务器编写配置文件

vim /usr/local/nginx/conf/nginx.conf

#user  nobody;

worker_processes  1;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    #tcp_nopush     on;

keepalive_timeout  65;

   

    upstream cart {

     server 172.20.52.19:3030 weight=1;

     server 172.20.52.20:4040 weight=1;

    #ip_hash;       #在没有做共享session的情况下ip_hash可以解决session问题

}

    server {

        listen       81;

        server_name  172.20.52.20; #另外一台填写另外IP

        charset utf-8;

       

        location /cart {

            root   html;

            index  index.html index.htm;

            proxy_next_upstream error timeout http_500 http_502 http_504;

            proxy_read_timeout 10s;

            proxy_pass    http://cart;

            proxy_set_header   Host             $host:81; #没用默认80端口需要加入

            proxy_set_header   X-Real-IP        $remote_addr;

            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

        

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

    }

}

3.验证配置文件正确性

/usr/local/nginx/sbin/nginx –t

显示以下信息为正确的

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

configuration file /usr/local/nginx/conf/nginx.conf test is successful

4.启动

/usr/local/nginx/sbin/nginx

4.Keepalived安装配置

1.安装(两台nginx机器都安装)

#安装 popt

yum -y install popt popt-devel

tar zxvf keepalived- 1.2.12.tar.gz

cd keepalived- 1.2.12

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

make && make install

 

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

chkconfig --add keepalived

#设置开机启动

chkconfig keepalived on

#启动keepalive服务

/etc/init.d/keepalived start

service keepalived restart

2.配置

cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak

MASTER

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

   notification_email {

     jiyulong@capinfo.com.cn

     gufanbiao@capinfo.com.cn

   }

   notification_email_from jiyulong@capinfo.com.cn

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script Monitor_Nginx {

     script "/root/monitor/monitor_nginx.sh"

     interval 2

     weight 2

}

vrrp_instance VI_1 {

    state MASTER  #(主机为MASTER,备用机为BACKUP)

    interface eth0  #(HA监测网络接口)

    virtual_router_id 51 #(主、备机的virtual_router_id必须相同)

    priority 100 #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)

    advert_int 1 #(VRRP Multicast广播周期秒数)

    authentication {

        auth_type PASS #(VRRP认证方式)

        auth_pass 1111 #(密码)

    }

   track_script {

        Monitor_Nginx  #(调用nginx进程检测脚本)

    }

    virtual_ipaddress {

        172.20.52.22  #(VRRP HA虚拟地址)

    }

}

BACKUP方面只需要修改state为BACKUP , priority比MASTER稍低即可

3.监控脚本

vim /root/monitor_nginx.sh

当检测到nginx进程不存在的时候,就干掉所有的keepalived,这时候,请求将会由keepalived的backup接管!!

vim /opt/nginx_pid.sh 

#!/bin/bash

# varsion 0.0.2

# 根据一网友说这样做不科学,如果nginx服务起来了,但是我把keepalived 杀掉了,我的理由是,如果nginx死掉了,我觉得就很难在起来,再有就是nagios 当然要给你报警了啊。不过这位同学说的有道理,所以就稍加改了一下脚本

## 查看是否有nginx进程 把值赋给变量A 

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

if [ $A -eq 0 ];then              ## 如果没有进程值得为 零

                /usr/local/nginx/sbin/nginx

                sleep 3

                if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then

                       killall keepalived         ## 则结束 keepalived 进程

                fi

fi

运行chmod +x /root/monitor_nginx.sh赋权限

注意:运行monitor_nginx.sh脚本时出现了这错误/bin/bash^M: bad interpreter: 没有那个文件或目录。

原因:linux和windows之间的不完全兼容。。

具体细节不管,如果验证:

vim XXX.sh

:set ff?

如果出现fileforma=dos那么就基本可以确定是这个问题了。

:set fileformat=unix

:wq

OK了。。。。

4.启动

172.20.52.20  172.20.52.21都重新启动keepalived:

service keepalived restart

这里请注意,当keepalived启动后,我们可以用命令:

ip add show eth0 来看我们的eth0网卡确实被添加了虚拟IP,如图

注:给大家提供加虚IP的方法

eg ifconfig eth0:0 166.111.69.100 netmask 255.255.255.0 up

ip add show eth0 来看我们的eth0网卡确实被添加了虚拟IP

5.验证

1.访问VIP看是否能够正常访问后端的tomcat

2.停止其中一个tomcat看是否能将访问转到另一台上

3.停止两台nginx上任何一个nginx进程看监控进程脚本是否会自动启动nginx

4.停止任何一台nginx上的keepalived进程看另一台是否接管vip

比如停止Master上的keepalived,例如如下killall keepalived,查看BACKUP机器是否已经接管,如果BACKUP接管后,BACKUP机器日志会是出下情况

日志路径:tail  /var/log/messages

Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.

Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.20.52.22

MASTER机器上日志会显示

Keepalived_vrrp: Terminating VRRP child process on signal

Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.

现在把MASTER上的Keepalived重新启动,会看到MASTER重新接管VIP,并对外提供服务,BACKUP仍旧回到BACKUP STATE,如果不是这种情况,请检查配置文件和步骤。

现在的BACKUP日志如下:

Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert

Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE

Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.

Master日志如下:

Keepalived_vrrp: VRRP_Script(Monitor_Nginx) succeeded

Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE

Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.

Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.20.52.22

文档

nginx+keepalived实现高可用负载均衡方案

学习文档目录1.引言32.环境说明33.Nginx安装配置34.Keepalived安装配置55.验证91.引言本学习文档主要介绍了采用Nginx负载均衡,通过keepalived实现Nginx双机互备,保证实现的WEB服务高可用方案。2.环境说明主nginx负载均衡器:172.20.52.20端口81(CentOSrelease5.8)副nginx负载均衡器:172.20.52.21端口81(CentOSrelease5.8)Tomcat1:172.20.52.19端口3030Tomcat2
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top