设为首页 收藏本站
查看: 1418|回复: 0

[经验分享] Nginx + tomcat + keepalived 负载均衡

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-2-2 08:55:42 | 显示全部楼层 |阅读模式
    Nginx + tomcat + keepalived 负载均衡

线上环境:

    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
1
2
3
   
[iyunv@spider118 ]# tar -zxvf keepalived-1.2.2.tar.gz
[iyunv@spider118 ]# cd keepalived-1.2.2
[iyunv@spider118 keepalived]# ./configure --prefix=/usr/local/keepalived

整理管理文件:
1
2
3
   
[iyunv@spider118 ]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[iyunv@spider118 ]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[iyunv@spider118 ]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

1.1 keepalived的配置文件默认在/etc/keepalived/

    serever118 keepalived配置:   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
   
[iyunv@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配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
   
[iyunv@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的服务器
1
2
3
4
5
6
7
8
9
10
11
   
[iyunv@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
1
2
   
server118上:
Jan 30 11:12:38 server135 avahi-daemon[2345]:Registering new address record for 172.16.8.254 on eth1.
1
2
   
server135上:
Jan 30 11:12:38 server135 avahi-daemon[2345]:Withdrawing address record for 172.16.8.254 on eth1.

  或者使用 #ip addr命令查看254 ip地址情况。
1
2
3
4
5
6
7
8
   
[iyunv@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
1
2
3
4
5
6
7
   
[iyunv@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安装的。
1
   
[iyunv@server118 ]# yum install nginx -y

yum安装nginx配置文件在:/etc/nginx/nginx.conf,编译安装在/usr/local/nginx/conf/nginx.conf

附上nginx详细编译安装组件(按需安装,不需要安装全部)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   
./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配置文件内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
   
[iyunv@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.iyunv.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断掉可以发现自动跳转到另外一台服务器。这里是线上环境我就不再贴出。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-41822-1-1.html 上篇帖子: Nginx安全加固配置手册 下篇帖子: 一个关于nginx网关504的问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表