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

Nginx + tomcat + keepalived 负载均衡

2015-01-30 14:54 281 查看
线上环境:
server118 IP:172.16.8.118 keepalived_MASTER,nginx(端口8080),tomcat(端口8087)

server135 IP: 172.16.8.135 keepalived_BACKUP,nginx(端口8080),tomcat(端口8087)
VIP :172.16.8.254
目的:两台服务器跑的相同的tomcat,连接到同一台数据库服务器。使用虚拟IP(254)nat转换到外网。如果一台服务器宕机或者tomcat服务出问题,可以自动切换到另外一台,保证线上服务不停止。

一、keepalived安装
下载地址:http://www.keepalived.org/download.html
[root@spider118 ]# tar -zxvf keepalived-1.2.2.tar.gz
[root@spider118 ]# cd keepalived-1.2.2
[root@spider118 keepalived]# ./configure --prefix=/usr/local/keepalived
整理管理文件:

[root@spider118 ]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@spider118 ]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@spider118 ]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
1.1 keepalived的配置文件默认在/etc/keepalived/
serever118 keepalived配置:
[root@spider118 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
router_id LVS_DEVEL
}
vrrp_script Monitor_Nginx {
script "/vol/script/monitor_nginx.sh"     #根据自己的实际路径放置monitor_nginx.sh
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER                #备用服务器是 BACKUP
interface eth1                      #我这使用的eth1的IP地址,请根据实际情况修改
virtual_router_id 51                #主备相同
priority 100                        #优先级,值越大优先级越高
advert_int 1
authentication {
auth_type PASS
auth_pass 123456             #主备相同
}
track_script {
Monitor_Nginx
}
virtual_ipaddress {
172.16.8.254                 #VIP
}
}
server135 keepalived配置:
[root@server135 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
router_id LVS_DEVEL
}
vrrp_script Monitor_Nginx {
script "/vol/script/monitor_nginx.sh" #根据自己的实际路径放置monitor_nginx.sh
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP                    #BACKUP
interface eth1                  #我这使用的eth1的IP地址,请根据实际情况修改
virtual_router_id 51
priority 99                     #优先权低于master的100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
track_script {
Monitor_Nginx
}
virtual_ipaddress {
172.16.8.254
}
}
1.2 keepalived中脚本monitor_nginx.sh的内容及目的:

起到监控nginx程序的功能,如果此服务器的nginx程序宕掉会自动结束keepalived程序,VIP(172.16.8.254)将映射到backup的服务器

[root@server135 keepalived]# cat /vol/script/monitor_nginx.sh
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
/etc/init.d/nginx start
sleep 5
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
killall keepalived
fi
fi
通过查看日志:tail -f /var/log/messages
server118上:
Jan 30 11:12:38 server135 avahi-daemon[2345]:Registering new address record for 172.16.8.254 on eth1.
server135上:
Jan 30 11:12:38 server135 avahi-daemon[2345]:Withdrawing address record for 172.16.8.254 on eth1.
或者使用 #ip addr命令查看254 ip地址情况。
[root@spider118 conf]# ip addr
。。。。(省略)。。。。
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 60:eb:69:21:60:c1 brd ff:ff:ff:ff:ff:ff
inet 172.16.8.118/24 brd 172.16.8.255 scope global eth1
inet 172.16.8.254/32 scope global eth1                #有254
inet6 fe80::62eb:69ff:fe21:60c1/64 scope link
valid_lft forever preferred_lft forever
[root@server135 nginx]# ip addr                           #无354
。。。。(省略)。。。。
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether d4:85:64:58:4f:9d brd ff:ff:ff:ff:ff:ff
inet 172.16.8.135/24 brd 172.16.8.255 scope global eth1
inet6 fe80::d685:64ff:fe58:4f9d/64 scope link
valid_lft forever preferred_lft forever
二、nginx安装配置:
下载地址:http://nginx.org/download/
由于我的机器装有epel源,所以直接用yum安装的。

[root@server118 ]# yum install nginx -y
yum安装nginx配置文件在:/etc/nginx/nginx.conf,编译安装在/usr/local/nginx/conf/nginx.conf
附上nginx详细编译安装组件(按需安装,不需要安装全部)

./configure
--user=www --group=www
--prefix=/usr/local/nginx
--with-http_stub_status_module
--with-http_ssl_module
--with-http_sub_module
--with-md5=/usr/lib
--with-sha1=/usr/lib
--with-http_gzip_static_module
认安装的路径是/usr/local/nginx
--with-http_stub_status_module #nginx状态
--with-http_ssl_module #支持HTTPS
--with-http_sub_module #支持URL重定向
--with-pcre=/usr/include/pcre/ (启用正规表达式)
--with-md5=/usr/lib #md5是一种加密算法
--with-http_memcached_module (启用memcache缓存)
--with-http_rewrite_module (启用支持url重写)
--with-sha1=/usr/lib #安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准(Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signature Algorithm DSA)对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。当接收到消息的时候,这个消息摘要可以用来验证数据的完整性。在传输的过程中,数据很可能会发生变化,那么这时候就会产生不同的消息摘要
--with-http_gzip_static_module #这个模块在一个预压缩文件传送到开启Gzip压缩的客户端之前检查是否已经存在以“.gz”结尾的压缩文件,这样可以防止文件被重复压缩
然后make; make install
2.1 nginx.conf配置文件内容:
[root@spider118 conf]# cat nginx.conf
# user  www www;
worker_processes 2;                #建议进程数和cpu个数相同
pid        logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include       mime.types;
default_type  application/octet-stream;
keepalive_timeout 120;
server_tokens off;
send_timeout 60;
tcp_nodelay on;

upstream tomcats{                     #tomcat集群
server 172.16.8.118:8087 weight=1;    #weight值越大,优先级越高
server 172.16.8.135:8087 weight=1;
ip_hash;               #在没有做共享session的情况下ip_hash可以解决session问题
}
server
{
listen  8080;
server_name tomcats;

location /tyfo {
proxy_pass        http://tomcats/tyfo; proxy_set_header   Host             $host:$server_port;   #修改了nginx端口号要加上port
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
proxy_connect_timeout        2;        #client连接集群超时时间
# proxy_read_timeout          60;
# proxy_send_timeout          100;
}
log_format ssl_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/ssl_access.log  ssl_access_log;
location  /nginxstatus {
stub_status on;             #Nginx 状态监控配置
access_log off;
}
}
}
说明:
ip_hash #启用后client会分配一个固定的tomcat服务器直到连接中断。但并没有session同步,可以参考我的另外一篇tomcat集群解决session同步:http://pynliu.blog.51cto.com/5027391/1576333。我还没测试,有问题可以给我留言。
proxy_set_header Host $host:$server_port; #默认nginx端口80,这里因为修改了nginx端口号所以加上":$server_port",不然在连接服务器时会跳转到80端口导致出错。
proxy_connect_timeout 2; #client在连接nginx服务器时会被nginx反向代理到upstream tomcats集群中的一台服务器上,如果2秒内此服务器没有响应会重新连接集群中的另外一台服务器。起到冗余的功能。
proxy_read_timeout 60; #client端从集群服务器读取超时时间
proxy_send_timeout 100; #这个时间代表tomcats集群中的某台服务器发送数据给client的时间,如果超时同样会给client更换服务器,这里我注释掉了,原因是web的某一模块在从数据库中读取数据时有可能超时,但不影响正常使用。
location /nginxstatus #http://$IP/nginxstatus查看链接状态

server135 nginx.conf配置相同,这里就不贴出了。

三、测试
内网浏览器打开:http://172.16.8..254:8080/tyfo

通过查看tomcat日志可以看出当前链接的是哪台服务器,再把这台tomcat断掉可以发现自动跳转到另外一台服务器。这里是线上环境我就不再贴出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息