Nginx+Keeplived+Tomcat搭建高可用/负载均衡的web服务器集群

一、集群规划

服务器角色 主机名 IP地址/VIP 软件
Nginx Master NK1 20.0.20.101/20.0.20.100 Nginx/Keepalived
Nginx Backup NK2 20.0.20.102/20.0.20.100 Nginx/Keepalived
Web Server NK3 20.0.20.103 Tomcat
Web Server NK4 20.0.20.104 Toncat

#关闭selinux和firewall

#软件版本为:apache-tomcat-7.0.85.tar.gz  jdk-8u151-linux-x64.tar.gz

二、安装配置Tomcat

1.安装JDK并配置环境变量

2.部署Tomcat

[[email protected] ~]# tar zxf apache-tomcat-7.0.85.tar.gz
[[email protected] ~]# mv apache-tomcat-7.0.85 /usr/local/tomcat

3.配置server.xml

[[email protected] ~]# cat /usr/local/tomcat/conf/server.xml
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JasperListener" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">    #NK4为tomcat2
        <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                 channelSendOptions="8">
          <Manager className="org.apache.catalina.ha.session.DeltaManager"    
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"
                   mapSendOptions="6"/>
          <Channel className="org.apache.catalina.tribes.group.GroupChannel">
            <Membership className="org.apache.catalina.tribes.membership.McastService"
                        address="228.0.0.4"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="20.0.20.103"    #填写本机的IP地址
                      port="4000"
		      autoBind="100"
                      selectorTimeout="5000"
                      maxThreads="6"/>
            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
            </Sender>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
          </Channel>
          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=""/>
          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
                    tempDir="/tmp/war-temp/"
                    deployDir="/tmp/war-deploy/"
                    watchDir="/tmp/war-listen/"
                    watchEnabled="false"/>
          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
  </Service>
</Server>

4.在web.xml里增加<distributable/>

[[email protected] ~]# vim /usr/local/tomcat/conf/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0">
    <distributable/>
......
......
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

5.建立测试页面

[[email protected] ~]# mkdir /usr/local/tomcat/webapps/test
[[email protected] ~]# cat /usr/local/tomcat/webapps/test/index.jsp 
<%@page language="java"%>  
<html>  
<body>  
        <h1><font color="red">Session serviced by tomcat</font></h1>  
        <table aligh="center" border="1">  
                <tr>  
                        <td>Session ID</td>  
                        <td><%=session.getId() %>-----NK3</td>  
                        <% session.setAttribute("abc","abc");%>  
                </tr>  
                <tr>  
                        <td>Created on</td>  
                        <td><%= session.getCreationTime() %></td>  
                </tr>  
        </table>  
</body>  
<html>

6.复制web.xml到测试文件夹

[[email protected] ~]# mkdir /usr/local/tomcat/webapps/test/WEB-INF
[[email protected] ~]# cp /usr/local/tomcat/conf/web.xml /usr/local/tomcat/webapps/test/WEB-INF/

7.启动tomcat

[[email protected] ~]# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

#以上操作在NK4上同步执行,除了配置文件server.xml稍有区别,其他都一样

8.浏览测试页面

三、配置Nginx

1.获取Nginx yum源并安装

[[email protected] ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[[email protected] ~]# yum install -y nginx

2.修改配置文件(NK1和NK2相同配置)

[[email protected] ~]# cat /etc/nginx/nginx.conf 
user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;
    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    on;
    keepalive_timeout  65;
    server_tokens   off;
    gzip  on;
    client_body_buffer_size 512k;
    proxy_connect_timeout   5;
    proxy_send_timeout      60;
    proxy_read_timeout      5;
    proxy_buffer_size       16k;
    proxy_buffers           4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;

    upstream test{
        server 20.0.20.103:8080 weight=10;
        server 20.0.20.104:8080 weight=10;
	}	

    server {
        listen   80;
        server_name  20.0.20.101;
        charset utf-8;
        location / {
                proxy_pass	http://test;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header Host $host;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Proto $scheme;
        	}
	}

    include /etc/nginx/conf.d/*.conf;
}

3.启动nginx

[[email protected] ~]# systemctl start nginx
[[email protected] ~]# systemctl enable nginx

4.测试

四、配置keepalived

1.安装keepalived

[[email protected] ~]# yum -y install keepalived

2.编辑nginx检测脚本

[[email protected] ~]# cat /usr/local/keepalived/chknginx.sh
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
    systemctl restart nginx
    sleep 2
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" = "0" ]; then
    systemctl stop keepalived
    fi
fi
[[email protected] ~]# chmod +x /usr/local/keepalived/chknginx.sh

3.修改配置文件

[[email protected] ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
    notification_email {
    }
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}

vrrp_script check_nginx {
    script "/usr/local/keepalived/chknginx.sh"    #nginx检测脚本
    interval 3
    weight -2
}

vrrp_instance VI_1 {
    state MASTER    #NK2为BACKUP
    interface ens192
    virtual_router_id 151
    priority 100    #NK2为99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2222
    }

    track_script {
        check_nginx    #检测脚本名
    }

    virtual_ipaddress {
        20.0.20.100
    }
}

4.在网卡上添加一个IP地址

#NK2和NK1配置类似

5.启动keepalived

[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# systemctl enable keepalived

6.查看IP是否绑定

[[email protected] ~]# ip add |grep "inet 20"
    inet 20.0.20.101/16 brd 20.0.255.255 scope global ens192
    inet 20.0.20.100/32 scope global ens192
    inet 20.0.20.100/16 brd 20.0.255.255 scope global secondary ens192
    
[[email protected] ~]# ip add |grep "inet 20"
    inet 20.0.20.102/16 brd 20.0.255.255 scope global ens192
    inet 20.0.20.100/16 brd 20.0.255.255 scope global secondary ens192

7.使用虚拟IP浏览测试页面

四、故障测试

1.keepalived测试

终止掉master后切换到了backup

2.nginx测试

在NK1上终止nginx后,会通过脚本自动启动nginx

3.tomcat测试

终止NK3上的tomcat

原文地址:http://blog.51cto.com/lullaby/2094663

时间: 2024-10-13 18:01:08

Nginx+Keeplived+Tomcat搭建高可用/负载均衡的web服务器集群的相关文章

【Linux运维-集群技术进阶】Nginx+Keepalived+Tomcat搭建高可用/负载均衡/动静分离的Webserver集群

额.博客名字有点长.. . 前言 最终到这篇文章了,心情是有点激动的. 由于这篇文章会集中曾经博客讲到的全部Nginx功能点.包含主要的负载均衡,还有动静分离技术再加上这篇文章的重点.通过Keepalived实现的HA(High Available).为什么要实现高可用呢?曾经在搭建的时候仅仅用了一台Nginxserver,这种话假设Nginxserver宕机了,那么整个站点就会挂掉.所以要实现Nginx的高可用,一台挂掉还会有还有一台顶上去.从而保证站点能够持续的提供服务. 关于负载均衡和动静

Nginx+Keepalived搭建高可用负载均衡集群

Nginx+Keepalived搭建高可用负载均衡集群   一. 环境说明 前端双Nginx+keepalived,nginx反向代理到后端的tomcat集群实现负载均衡,Keepalived实现集群高可用. 操作系统: Centos 6.6_X64 Nginx版本: nginx-1.9.5 Keepalived版本:keepalived-1.2.13 结构: Keepalived+nginx-MASTER:10.6.1.210         Keepalived+nginx-BACKUP:

Nginx+Keepalived(双机热备)搭建高可用负载均衡环境(HA)

原文:https://my.oschina.net/xshuai/blog/917097 摘要: Nginx+Keepalived搭建高可用负载均衡环境(HA) http://blog.csdn.net/xyang81/article/details/52554398可以看更多介绍 Keepalived的介绍可以百度一堆一堆的资料.一定要看看哦. 1.基于上一篇博客总结,再次安装一个虚拟机当backup服务器,这个服务器只安装Keepalived+Nginx即可 2.Master还是上一篇博文的

使用Nginx1.9.9+Keepalived1.2.x搭建高可用负载均衡集群

一 简介以及原理介绍 (1)Nginx概念介绍: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开发.其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度.京东.新浪.网易.腾讯.淘宝等 (2)Keepalived概念介绍: Keepalived的作用是检测服务器的状态,如果有一台we

Keepalived+Nginx实现双主高可用负载均衡

Keepalived+Nginx实现双主高可用负载均衡 一.部署Nginx+keepalived高可用有两种配置方案: 1.Nginx+keepalived 主从配置 前端使用两台机器,使用一个VIP,即其中一台为主负责全部请求,一台为备,只用在主出现故障时,才会替代主处理请求,平常处于闲置状态,此方案不够理想.  2.Nginx+keepalived 双主配置 前端使用2台机器,使用两个VIP,互为主备,不考虑其他情况时均衡处理请求,当其中一台机器出现故障时,另一台负责处理对两台机器的请求,此

HAProxy负载均衡与keepalived搭建高可用负载均衡web(Nginx/PHP/Tomcat)集群

HAProxy简介 HAProxy是基于TCP四层和HTTP七层的开源的第三方应用负载均衡软件.具有高可靠性.高稳定性.高并发处理能力.透明代理和支持ACL功能等特点.HAProxy是一个功能强大且优秀的负载均衡集群解决方案. HAProxy负载均衡算法 Haproxy的负载均衡算法在backend模块中通过balance命令来定义,常见的算法有: roundrobin: 轮叫调度算法,可以通过定义权值来分配后端服务器 static-rr : 静态的基于权重的轮叫调度算法 source:    

三、负载均衡与web服务集群搭建

一.负载均衡LVS基本介绍 LB集群的架构和原理很简单,就是当用户的请求过来时,会直接分发到Director Server上,然后它把用户的请求根据设置好的调度算法,智能均衡地分发到后端真正服务器(real server)上.为了避免不同机器上用户请求得到的数据不一样,需要用到了共享存储,这样保证所有用户请求的数据是一样的. LVS是 Linux Virtual Server 的简称,也就是Linux虚拟服务器.这是一个由章文嵩博士发起的一个开源项目,它的官方网站是http://www.linu

公司nginx keepalived tomcat cxf 搭建高可用负载均衡实战系列1- keepalived安装配置

1,ip说明 vip 10.50.13.67 server1 10.50.13.68 server2 10.50.13.140 2,keepalived安装 keepalived通常与lvs或者nginx结合使用保证集群的高可用,keepalived的master会绑定一个vip用来对外服务并定期向backup发送消息,当backup接收不到消息时则会判定master已经挂掉,backup将升为master并且绑定vip继续对外提供服务,从而保证高可用.下面介绍keepalived的安装 安装依

公司nginx keepalived tomcat cxf 搭建高可用负载均衡实战系列1- keepalived的安装配置

1,ip说明 vip 10.50.13.67 server1 10.50.13.68 server2 10.50.13.140 2,keepalived安装 keepalived通常与lvs或者nginx结合使用保证集群的高可用,keepalived的master会绑定一个vip用来对外服务并定期向backup发送消息,当backup接收不到消息时则会判定master已经挂掉,backup将升为master并且绑定vip继续对外提供服务,从而保证高可用.下面介绍keepalived的安装 安装依