nginx,tomcat,redis,keepalived高可用实现

来源:互联网 发布:网络维护要求 编辑:程序博客网 时间:2024/05/29 16:34

1.安装nginx,tomcat,redis。命令都为tar -zxvf ***,安装redis时,在redis根目录后要加 make 命令。安装keepalived,命令:yum -y keepalived

2.修改nginx的nginx.conf:

    upstream nginx {
        server 192.168.0.168:8080;
        server 192.168.0.168:8090;


        check interval=3000 rise=2 fall=5 timeout=1000 type=http;
        check_http_send "HEAD / HTTP/1.0\r\n\r\n";
        check_http_expect_alive http_2xx http_3xx;
    }


    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://nginx;
        }

        location /status {
            check_status;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        check interval=3000 rise=2 fall=5 timeout=1000 type=http;
        check_http_send "HEAD / HTTP/1.0\r\n\r\n";
        check_http_expect_alive http_2xx http_3xx;
    }

3.把jar包复制到tomcat中的lib文件夹:

   commons-pool2-2.4.2.jar

   jedis-2.8.0.jar

   tomcat-redis-session-manager1.2.jar

   修改另一个tomcat的端口号,保证正常启动两个tomcat

   在conf目录下修改context.xml文件,在<context>里面加入:

   <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />

   <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="192.168.0.168"
         port="6379"
         database="0"
         maxInactiveInterval="60" />

   修改tomcat中webapps/ROOT/index.jsp

   <%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>

   <html lang="en">

   SessionID:<%=session.getId()%>

   </br>

   SessionIP:<%=request.getServerName()%>

   </br>

   <h1>tomcat1</h1>

   </html>

4.修改redis的配置文件redis.conf

   将bind127.0.0.1修改为本机地址否则只能本机访问

5.修改主nginx/etc/keepalived/keepalived.conf文件

  ! Configuration File for keepalived

  #全局配置
  global_defs {
   notification_email {  #指定keepalived在发生切换时需要发送email到的对象,一行一个
     XXX@XXX.com
   }
   notification_email_from XXX@XXX.com                  #指定发件人
   #smtp_server XXX.smtp.com                         #指定smtp服务器地址
   #smtp_connect_timeout 30                          #指定smtp连接超时时间
   router_id LVS_DEVEL                             #运行keepalived机器的一个标识
  }


  vrrp_instance VI_1 { 
    state MASTER           #标示状态为MASTER 备份机为BACKUP
    interface eth0         #设置实例绑定的网卡
    virtual_router_id 51   #同一实例下virtual_router_id必须相同
    priority 100           #MASTER权重要高于BACKUP 比如BACKUP为99  
    advert_int 1           #MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
    authentication {       #设置认证
        auth_type PASS     #主从服务器验证方式
        auth_pass 8888
    }
    virtual_ipaddress {    #设置vip
        192.168.0.180       #可以多个虚拟IP,换行即可
    }
  }

6.依次重启即可运行