您的位置:首页 > 运维架构 > Tomcat

Nginx +keepalived+tomcat 实现高可用+负载均衡

2016-12-20 14:00 447 查看
参考:http://litaotao.blog.51cto.com/6224470/1302100搭建环境:192.168.127.141 (master nginx+keepalived+tomcat)192.168.127.130 (backup nginx+keepalived+tomcat )vip 192.168.127.100 (实际环境中这个ip需要是个公网地址供客户端访问)实现的功能有:1)、Master服务器没挂,则Master占有vip且nginx运行在Master上2)、Master服务器挂了,则backup抢占vip且在backup上运行nginx服务3)、如果master服务器上的nginx服务挂了,则vip资源转移到backup服务器上4)、检测后端服务器的健康状态master:一 安装nginx和keepalived: 1 安装keepalived和编译安装nginx
yum -y install keepalived
wget http://nginx.org/download/nginx-1.6.2.tar.gz [root@ ~]#tar xf nginx-1.6.2.tar.gz
[root@ ~]#yum -y groupinstall "Development tools" "Server  Platform Development"
[root@ ~]#yum -y install pcre-devel
[root@ ~]# cd nginx-1.6.2
[root@ nginx]# groupadd nginx
[root@ nginx]# useradd -r -g nginx nginx
[root@ nginx]#./configure \
--prefix=/usr\
--sbin-path=/usr/sbin/nginx\
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid  \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/\
--http-proxy-temp-path=/var/tmp/nginx/proxy/\
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/\
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi\
--http-scgi-temp-path=/var/tmp/nginx/scgi\
--with-pcre
make && make install
2 编辑nginx的启动脚本
vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
3 修改keepalived.conf配置文件
global_defs {
notification_email {
root@localhost
}
notification_email_from admin@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LTT
}
vrrp_script chk_nginx {  #检测nginx服务是否在运行有很多方式,比如进程,用脚本检测等等
script "killall -0 nginx"  #用shell命令检查nginx服务是否存在
interval 1  #时间间隔为1秒检测一次
weight -2   #当nginx的服务不存在了,就把当前的权重-2
fall 2      #测试失败的次数
rise 1      #测试成功的次数
}
vrrp_instance IN_1 {
state MASTER
interface eth0
virtual_router_id 22
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass aaaa
}
virtual_ipaddress {
192.168.127.100
}
track_script {
chk_nginx   #引用上面的vrrp_script定义的脚本名称
}
}

4 启动keepalive和nginx服务/etc/init.d/nginx start/etc/init.d/keepalived start
Backup:1、安装keepalived和编译安装nginx同master一样(第一步和第二步)3 修改keepalived配置文件 先从master上面拷贝这个配置文件然后更改
vim keepalived.conf   #此配置文件是从Master服务器上copy过来,只需小小改动
state BACKUP
#把这里原先的MASTER改成BACKUP
priority 99
#把这里原先的100改成99

4 开启服务 /etc/init.d/nginx start/etc/init.d/keepalived start#######################################################################################两台服务器安装tomcat:详细过程见:http://825536458.blog.51cto.com/4417836/1831678
二 在master 和backup 两个服务器上面配置nginx 负载均衡
BACKUP:
tcp_nopush     on;
keepalive_timeout 60;
tcp_nodelay on;
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;
gzip_min_length 1k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;
# include vhosts/*.conf;
upstream web_server {
server 192.168.127.130:8080 weight=1;
server 192.168.127.141:8080 weight=1;
}
server
{
listen 80;
server_name 192.168.127.130;
index index.html index.htm;
root /usr/local/nginx/html/ROOT;
location  / {
proxy_pass http://web_server; proxy_set_header Host  $host;
proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP  $remote_addr;
}
}
}
MASTER:
tcp_nopush     on;
keepalive_timeout 60;
tcp_nodelay on;
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;
gzip_min_length 1k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;
# include vhosts/*.conf;
upstream web_server {
server 192.168.127.130:8080 weight=1;
server 192.168.127.141:8080 weight=1;
}
server
{
listen 80;
server_name 192.168.127.141;
index index.html index.htm;
root /usr/local/nginx/html/ROOT;
location  / {
proxy_pass http://web_server; proxy_set_header Host  $host;
proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP  $remote_addr;
}
}
}
然后两边分别重启nginx服务三 测试1 把master上面的nginx服务停掉,看客户端能否正常访问网站并且查看vip是否会移动到backup上面2 把master 上面的keepalived 服务停掉 看客户端能否正常访问网站并且查看vip是否会移动到backup上面3 把其中一个web服务停掉,看是否还能正常访问web
用浏览器访问http://192.168.127.100验证即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  服务器 客户端 检测