集群搭建tomcat+keepalived+nginx+memcached

集群搭建tomcat+keepalived+nginx+memcached

一、基础知识

1、linux基本命令

ps -aux | grep tomcat

ps -aux | grep java

ps -aux | grep pid

netstat  -anp |grep  portno即:netstat –apn | grep 8080

centos版本差异命令有区别,如启动服务:

Centos7:systemctlrestart iptables.service

Centos6:service iptablesrestart

二、架构图


集群搭建tomcat+keepalived+nginx+memcached_第1张图片

说明:


集群搭建tomcat+keepalived+nginx+memcached_第2张图片

三、安装配置

1、安装nginx

1.1、需要编译程序依赖包

yum install gcc gcc-c++ make automake autoconflibtool pcre pcre-devel zlib zlib-devel openssl openssl-devel

1.2、安装配置

192.168.117.131和192.168.117.132机器上安装nginx

下载nginx包:wgethttp://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

建立nginx的yum仓库:rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm

安装:yum install nginx

查看:whereis nginx

检查:/usr/sbin/nginx –t检查安装是否成功

启动:systemctl start nginx或/usr/sbin/nginx start

停止:systemctl stop nginx

重启:/usr/sbin/nginx -s reload

分别修改两台机器的/usr/share/nginx/html/index.html以区分效果

加入启动项:

vi /etc/rc.local增加一行:/usr/sbin/nginx

若开启防火墙需要开放端口:

vi /etc/sysconfig/iptables

添加内容:-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

重启防火墙:service iptables restart或systemctl restartiptables.service

配置:(需要先将130和133tomcat配置好)

192.168.117.131增加文件:vi /etc/nginx/conf.d/131.conf

upstream my_server {

server 192.168.117.130:8080 weight=3;

server 192.168.117.133:8080 weight=3;

#keepalive 2000;

}

server {

listen80;

server_name192.168.117.131;

client_max_body_size 1024M;

location / {

proxy_pass http://my_server/;

proxy_set_header Host $host:$server_port;

}

}

192.168.117.132增加文件:vi /etc/nginx/conf.d/132.conf

upstream my_server {

server 192.168.117.133:8080 weight=3;

server 192.168.117.130:8080 weight=3;

#keepalive 2000;

}

server {

listen80;

server_name192.168.117.132;

client_max_body_size 1024M;

location / {

proxy_pass http://my_server/;

proxy_set_header Host $host:$server_port;

}

}

2、安装配置Keepalived

2.1、安装配置

安装:yuminstall keepalived

检查版本:keepalived –v

查看:whereis keepalived

配置:

(1)新建文件:vi /etc/keepalived/nginx_check.sh

#!/bin/bash

A=ps -C nginx –no-header |wc -lif [ $A -eq 0 ];then

/usr/sbin/nginx

sleep 2

if [ps -C nginx--no-header |wc -l-eq 0 ];then

killall keepalived

fi

fi

(2)编辑keepalived.conf:vi /etc/keepalived/keepalived.conf

192.168.117.131如下:

! Configuration Filefor keepalived

global_defs {

router_idnode-01

}

vrrp_script chk_nginx {

script"/etc/keepalived/nginx_check.sh"

interval 2

weight -20

}

vrrp_instance VI_1 {

stateMASTER

interfaceens33

virtual_router_id 146

mcast_src_ip192.168.117.131

priority100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

chk_nginx

}

virtual_ipaddress {

192.168.117.150

}

}

192.168.117.132如下:

! Configuration File for keepalived

global_defs {

router_idnode-02

}

vrrp_script chk_nginx {

script"/etc/keepalived/nginx_check.sh"

interval 2

weight -20

}

vrrp_instance VI_1 {

stateBACKUP

interfaceens33 #网卡

virtual_router_id 146

mcast_src_ip192.168.117.132

priority 90

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

chk_nginx

}

virtual_ipaddress {

192.168.117.150

}

}

interfaceens33为网卡

3、安装配置memcached

3.1、安装配置

安装:yum -y install memcached

重启:service memcached restart

启动:service memcached start

开机启动:service memcached enable

状态:service memcached status

关闭:service memcached stop

查看状态:memcached-tool127.0.0.1:11211 stats

4、安装配置tomcat

进入目录:cd /home/wangyong

创建目录:mkdir test

上传:apache-tomcat-8.5.20.tar.gz到/home/wangyong/test/

解压:tar –zxvf apache-tomcat-8.5.20.tar.gz

重命名:mv apache-tomcat-8.5.20 tomcat-8.5.20

创建lib目录:mkdir memcachedlib

进入目录:cd /home/wangyong/memcachedlib下载以下依赖包

#下载memcached-session-manager jars to tomcat

wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/1.9.5/memcached-session-manager-1.9.5.jar

wget

http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc8/1.9.5/memcached-session-manager-tc8-1.9.5.jar

#使用memcached的时候要用到的jar文件

wget

http://repo1.maven.org/maven2/net/spy/spymemcached/2.11.1/spymemcached-2.11.1.jar

#使用javolution-serializer的时候用到的jar文件下载

wget

http://repo1.maven.org/maven2/de/javakaffee/msm/msm-javolution-serializer/1.9.5/msm-javolution-serializer-1.9.5.jar

wget http://www.java2s.com/Code/JarDownload/javolution/javolution-5.5.1.jar.zip

unzip

javolution-5.5.1.jar.zip

#下面是使用kryo-serializer的时候用到的jar文件下载

wget

http://repo1.maven.org/maven2/de/javakaffee/msm/msm-kryo-serializer/1.9.5/msm-kryo-serializer-1.9.5.jar

wget

http://repo1.maven.org/maven2/de/javakaffee/kryo-serializers/0.9/kryo-serializers-0.9.jar

#(相配合的kryo只能用3.+的版本。)

wget

http://repo1.maven.org/maven2/com/esotericsoftware/kryo/3.0.3/kryo-3.0.3.jar

wget

http://repo1.maven.org/maven2/com/esotericsoftware/minlog/1.3.0/minlog-1.3.0.jar

wget

http://repo1.maven.org/maven2/com/esotericsoftware/reflectasm/1.11.3/reflectasm-1.11.3.jar

wget http://repo1.maven.org/maven2/org/ow2/asm/asm/5.1/asm-5.1.jar

将所下载的包移动到tomcat的lib目录下

mv *.jar /home/wangyong/test/tomcat-8.5.20/lib/

配置tomcat的context.xml

cd /home/wangyong/test/tomcat-8.5.20/conf

vi context.xml

增加以下内容:

sticky="false"

memcachedNodes="n1:192.168.117.130:11211 n2:192.168.117.133:11211"

failoverNodes=""

requestUriIgnorePattern=".*\.(png|gif|jpg|css|js|ico)$"

sessionBackupAsync="false"

sessionBackupTimeout="500"

transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"

/>

四、运行调试

首先要把memcached的服务启动,然后是nginx服务,最后启动tomcat所在的每个子节点的服务

五、参考文章

1、Keepalived+Nginx实现高可用Web负载均衡

http://www.jianshu.com/p/da26df4f7d60

2、nginx代理配置

http://www.cnblogs.com/fanzhidongyzby/p/5194895.html

3、nginx+tomcat7集群,负载均衡和session共享

http://blog.csdn.net/remote_roamer/article/details/51133790

你可能感兴趣的:(集群搭建tomcat+keepalived+nginx+memcached)