专注JavaWeb开发 首页制式『默认式 OR 时间轴 网站地图|RSS订阅|设为首页|加入收藏
当前位置:首页 >服务器 > 正文

CentOS基于Keepalived-1.2.23, Nginx-1.8.0, Tomcat7.0 搭建负载均衡器

作者: 紫寒1120 发布时间: 2016-08-03 浏览次数:

CentOS基于Keepalived-1.2.23, Nginx-1.8.0, Tomcat7.0 搭建负载均衡器

两台测试服务器  master:192.168.89.128

  Backup:192.168.89.129

 

Linux下安装Nginx 请参照地址

Redhat 下安装地址为:http://www.javaweb1024.com/server/2015/07/07/796.html

CentOS 下安装地址为:http://www.javaweb1024.com/server/2015/07/31/817.html

Nginx默认安装,其有效的配置文件应该在 /usr/local/nginx/conf/nginx.conf 而不是你的安装目录下面 (安装目录)/conf/nginx.conf. PS 如果找不到nginx.conf在哪里,find / -name nginx.conf 找出所有文件,然后修改.

 

Nginx.conf(Masterbackup的配置相同)

请先建立2个文本 /home/zihan/log/error.log

 /home/zihan/log/nginx.pid

用于存放Nginx的日志信息 不做iphash

worker_processes  4;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

error_log  /home/zihan/log/error.log  info;

 

pid        /home/zihan/log/nginx.pid;

events {

use epoll;

worker_connections  1024;

}

http {

include       mime.types;

default_type  application/octet-stream;

#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  off;

#access_log  /var/log/nginx/access.log;

client_header_timeout  3m;

client_body_timeout    3m;

send_timeout           3m;

client_header_buffer_size    1k;

large_client_header_buffers  4 4k;

sendfile        on;

tcp_nopush      on;

tcp_nodelay     on;

fastcgi_buffer_size         512k;

fastcgi_buffers             6 512k;

fastcgi_busy_buffers_size   512k;

fastcgi_temp_file_write_size        512k;

fastcgi_intercept_errors    on;

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

#keepalive_timeout  75 20;

#include    gzip.conf;

upstream tomcatservers {

#ip_hash;

server 192.168.89.128:8080  max_fails=3 fail_timeout=60s;

server 192.168.89.129:8090  max_fails=3 fail_timeout=60s;

}

server {

listen       80;

  location ~ \/cms/.(gif|jpg|jpeg|png|ico|rar|zip|txt|flv|swf|doc|ppt|xls|pdf)$

{  

  root    /home/tomcat/webapps/cms/;

}

 

location / {

proxy_connect_timeout   3;

proxy_send_timeout      60;

proxy_read_timeout      60;

proxy_pass http://tomcatservers/;

proxy_buffer_size 4k;

proxy_buffers 64 32k;

client_max_body_size 500m;

client_body_buffer_size 1024k;

proxy_set_header Host $host:80;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header REMOTE-HOST $remote_addr;

}

}

}

 

Keepalived-1.2.23安装

tar -zxvf keepalived-1.2.23.tar.gz

cd keepalived-1.2.23

./configure

make

make install

如果出错请参考网址:http://www.javaweb1024.com/info/973.jspx

不用复制文件,我也没有做启动服务.

global_defs {

 router_id LVS_DEVEL

}

vrrp_script Monitor_Nginx {

 script "/home/zihan/keepalived-1.2.23/etc/keepalived/monitor_nginx.sh" #据自己的实际路径放置monitor_nginx.sh    

 interval 2

 weight 2

}

vrrp_instance VI_1 {

 state MASTER #备用机为backUP

 interface eth4 #请使用ifconfig 确定是 eth0 还是eth4 等等 不然会创建#virtual_ipaddress失败

 virtual_router_id 51

 priority 100 #备用机小于此数字

 advert_int 1

 authentication {

 auth_type PASS

 auth_pass 1234

}

 track_script {

 Monitor_Nginx

}

 virtual_ipaddress {

 192.168.89.86 #可以写多个,但是注意与主机IP一致

 }

}

monitor_nginx.sh 配置为

!/bin/bash

if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]

then

 /home/zihan/nginx-1.8.0/objs/nginx

 sleep 5

 if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]

 then

 killall keepalived

 fi

fi

启动命令为 

 ./keepalived -f /home/zihan/keepalived-1.2.23/etc/keepalived/keepalived.conf 

测试步骤

1.    访问VIP看是否能够正常访问后端的tomcat
2.    停止其中一个tomcat看是否能将访问转到另一台上
3.    停止两台nginx上任何一个nginx进程看监控进程脚本是否会自动启动nginx PS: 一般情况下,创建的nginx监控脚本没有sh权限 需要命令:cd 到相应目录下执行 chmod u+x *.sh
4.    停止任何一台nginx上的keepalived进程看另一台是否接管vip

 

 



QQ群: 武汉地区(142173356)验证消息JavaWeb1024

网友共有【】评论,点击查看



x 微信关注我们 随时获取最新网站资讯