tomcat+nginx+keepalived高可用实现详细部署

系统

两台Nginx:

CentOS6.5 x86_64

两台tomcat:

CentOS6.5 x86_64

拓扑

机器IP

安装软件

角色

虚拟ip

描述

192.168.15.132

Nginx、keepalived

Nginx主机

192.168.15.135

反向代理到tomcat1和tomcat2

192.168.15.133

Nginx、keepalived

Nginx备机

主机挂了切换虚拟ip 192.168.15.135

反向代理到tomcat1和tomcat2

192.168.15.128

Tomcat

Tomcat1

192.168.15.30

Tomcat

Tomcat2

IP地址

nginx(主LB):192.168.15.132

nginx(备LB):192.168.15.133

VIP地址:192.168.15.135

Real1的IP:192.168.15.128

Real2的IP:192.168.15.30

一.修改启动web服务器

在192.168.15.128和192.168.15.30上分别解压修改tomcat的index页面:

vi webapps/ROOT/index.jsp

修改页面html部分

<html>
    <head>
    </head>
    <body>
SessionID:<%=session.getId()%>
<br/>
SessionIP:<%=request.getServerName()%>
<br/>
<h1>tomcat1 page</h1>
</body>
</html>

这样可以通过访问index来判断访问是不是轮询的

二.安装配置nginx

1.安装GCC编译器等工具:

yum install -y gcc gcc-c++ autoconf automake libtool make openssl openssl-devel pcre pcre-devel

2.下载安装Nginx:

wget http://nginx.org/download/nginx-1.6.3.tar.gz

注:这里也可以下载tengine压缩包,比一般nginx多一些功能

tar -zxvf nginx-1.6.3.tar.gz 

cd nginx-1.6.3/  

./configure --prefix=/usr/local/nginx
--sbin-path=/usr/local/nginx/sbin/nginx
--conf-path=/usr/local/nginx/conf/nginx.conf
--pid-path=/usr/local/nginx/logs/nginx.pid \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \ 

make && make install 

注:查询"./configure --help"相关模块,按需求指定启用

3.配置Nginx.conf

配置文件,二个nginx负载均衡器的文件一样

user nobody;

worker_processes 1;

error_log /usr/local/nginx/logs/error.log notice;

pid /usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile 51200;

events {

use epoll;

worker_connections 51200;

}

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

server_names_hash_bucket_size 128;

client_header_buffer_size 32k;

large_client_header_buffers 4 32k;

client_max_body_size 8m;

sendfile on;

tcp_nopush on;

server_tokens off;

keepalive_timeout 60;

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

gzip on;

upstream backend

{

server 192.168.15.128;

server 192.168.15.130;

}

server {

listen 80;

server_name 192.168.15.135;

location / {

root html;

index index.php index.html index.htm;

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass http://backend;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

location /nginx_status {

stub_status on;

auth_basic "NginxStatus";

auth_basic_user_file /usr/local/nginx/htpasswd;

#allow 127.0.0.1;

#deny all;

}

location ~* \.(ini|docx|txt|doc|pdf)$ {

#禁止访问文档性文件

root /usr/share/nginx/html;

deny all;

}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|html|htm|css)$ {

root /home/image;

proxy_store on;

proxy_store_access user:rw group:rw all:rw;

proxy_temp_path /home/image;

if ( !-e $request_filename) {

proxy_pass http://backend;

}

}

}

}

这里面配置较多,其中比较有用的已经标红了,可以直接将此部分配置在默认的nginx的配置文件里面即可

三.安装及配置keepalived

1.安装keepalived

在两台nginx服务器张安装keepalived:

wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
tar -zxvf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
./configure --sysconf=/etc/  --with-kernel-dir=/usr/src/kernels/2.6.32-573.8.1.el6.x86_64
make && make install
ln -s /usr/local/sbin/keepalived  /sbin/  

#这一步很重要,不执行ln -s会报错“Starting keepalived: /bin/bash: keepalived: command not found”

service keepalived start

上述步骤也可以直接通过yum –y install keepalived 来代替,不同安装方式而已,不是重点

二台Nginx上keepalived.conf配置文件如下,配置完成后分别service keepalived start启动。检验keepalived配置是否成功

两台keepalived的配置如下:可以看出两者之间的区别仅仅是主备的权重不同,主为100备为66,其余一些有作用的配置已经用红色标注

主:

 global_defs {
   notification_email {
     test@163.com
   }
   notification_email_from keepalived@localhost 
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_MASTER
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2                           #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
   #state MASTER
    state BACKUP
    nopreempt                       
    #设置非抢占模式时,修改“state MASTER”为“state BACKUP”,添加“nopreempt“
    interface bond0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
}
track_script {
chk_http_port            #(调用检测脚本)
}
    virtual_ipaddress {
        192.168.15.135
    }
}

备:

 global_defs {
   notification_email {
     test@163.com
   }
   notification_email_from keepalived@localhost 
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_BACKUP
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2                           #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface bond0
    virtual_router_id 51
    priority 66
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
}
track_script {
chk_http_port            #(调用检测脚本)
}
    virtual_ipaddress {
        192.168.15.135
    }
}

以下是针对nginx状态进行检测的脚本,第一次nginx服务死掉时,会重新启动,如果Nginx服务无法正常启动,则杀掉keepalived进程

vim  /usr/local/src/check_nginx_pid.sh

check_nginx_pid.sh 文件内容:

#!/bin/bash
A=`ps -C nginx --no-header |wc -l`        
if [ $A -eq 0 ];then                            
      /usr/local/nginx/sbin/nginx        
      if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then  
              killall keepalived                    
      fi
fi 

开始nginx负载均衡测试,停掉其中一台的任何服务,不影响整个系统的运作。

四.测试

1、依次启动两个tomcat,启动两个nginx,启动两个keepalived,查看主机上是否有虚拟ip 192.168.15.135

2、通过虚拟ip访问nignx,看页面是不是轮询两个tomcat

3、将主机的nginx进程kill掉,看看vip是不是切换到备机器上了

4、访问虚拟ip是不是还能轮询访问tomcat

目的:搭建高可用、高性能的jsp集群 二、初始化系统 #init system 系统环境: CentOS 5.5(定制安装) 组件: Base Development Libraries Development Tools Editors Text-based Internet ./init_system.sh #此脚本参见http://kerry.blog.51cto.com/172631/555535 三、LVS+keeplived #关于LVS+keeplived的配置请参考我的另一篇博文《CentOS5.5环境下布署LVS+keepalived 》http://kerry.blog.51cto.com/172631/401253 四、nginxtomcat整合 #config web_1、web_2 #download software cd /opt wget http://labs.renren.com/apache-mirror/tomcat/tomcat-6/v6.0.32/bin/apache-tomcat-6.0.32.tar.gz wget http://download.oracle.com/otn-pub/java/jdk/6u25-b06/jdk-6u25-linux-i586.bin wget http://nginx.org/download/nginx-1.0.0.tar.gz wget http://ftp.exim.llorien.org/pcre/pcre-8.12.tar.gz wget http://apache.etoak.com//tomcat/tomcat-connectors/native/1.1.20/source/tomcat-native-1.1.20-src.tar.gz wget http://archive.apache.org/dist/apr/apr-1.4.2.tar.gz wget http://archive.apache.org/dist/apr/apr-util-1.3.9.tar.gz #================================ 1、安装tomcat、JDK ================================= mv apache-tomcat-6.0.32.tar.gz jdk-6u25-linux-i586.bin /usr/local/ tar -zxvf apache-tomcat-6.0.32.tar.gz mv apache-tomcat-6.0.32 tomcat chmod +x jdk-6u25-linux-i586.bin ./jdk-6u25-linux-i586.bin #apr 与 tomcat-native 提供更好的伸缩性、性能和集成到本地服务器技术,如果没有apr技术,启动tomcat 时出现如下提示 #INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/local/jdk1.6.0_25/jre/lib/i386/client:/usr/local/jdk1.6.0_25/jre/lib/i386:/usr/local/jdk1.6.0_25/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib rpm -e --nodeps apr-1.2.7-11.el5_3.1 rpm -e --nodeps apr-1.2.7-11.el5_3.1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

minmax329

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值