1. 准备好tomcat

最初的网络公司由于业务量较小,所以一般单机铺排,达成单点访问即可满足工作的供给,那也是最简便易行的安排方式,可是随着工作的不断扩充,系统的访问量慢慢的上涨,单机陈设的格局已不可能承载现有的业务量,需求展开服务集群化安插,本文主要介绍服务端汤姆cat多实例布署,以及怎么样保证web服务的高可用方案。

  
工作规律:两台Nginx通过Keepalived生成1个实例,二台Nginx的VIP互为备份,任何一台Nginx机器假如爆发硬件损坏,Keepalived会自动将它的VIP地址切换成另一台机械,不影响客户端的访问。

环境:CenOS 6.5
Nginx-Master:10.10.10.128
Nginx-Backup:10.10.10.129
Tomcat1:10.10.10.130
Tomcat2:10.10.10.131
VIP:10.10.10.100

第一台

  • Nginx 是三个高品质的 HTTP反向代理服务器
  • Keepalived
    是三个基于V揽胜KugaP协议来贯彻的LVS服务高可用方案,可以动用其来制止服务的单点故障
  • 汤姆cat 是贰个免费的绽开源代码的Web
    应用服务器,属于轻量级应用服务器。

IP 新闻列表:

① 、环境基础配置
一 、更换国内yum源
贰 、关闭防火墙、SELinux
叁 、时间共同

tar vxf apache-tomcat-7.0.54.tar.gz

正如图:为典型的汤姆cat服务多实例布署的架构图

名称     IP

VIP1      192.168.200.254
VIP2      192.168.200.253
Nginx1     192.168.200.202
Nginx2     192.168.200.203

① 、在Nginx二分之一上编写翻译安装nginx服务
第③搭建Nginx1,
[root@Nginx-1 ~]# yum -y install pcre-devel zlib-devel
[root@Nginx-1 ~]# useradd -M -s /sbin/nologin  nginx
[root@Nginx-1 ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src
[root@Nginx-1 ~]# cd /usr/src/nginx-1.6.2
[root@Nginx-1 nginx-1.6.2]# ./configure –prefix=/usr/local/nginx
–user=nginx –group=nginx && make && make install
[root@Nginx-1 nginx-1.6.2]# cd /usr/local/nginx/html/
[root@Nginx-1 html]# echo “server 192.168.200.202” > index.html
[root@Nginx-1 html]# /usr/local/nginx/sbin/nginx
[root@Nginx-1 html]# netstat -anpt |grep nginx
tcp        0      0 0.0.0.0:80                 
0.0.0.0:*                   LISTEN      4503/nginx 

搭建Nginx2, 同Nginx1搭建艺术是相同的。
与Nginx1唯一分歧的是:
[root@Nginx-2 html]# echo “server 192.168.200.203” > index.html

2、在Nginx二分之一上编译安装keepalived服务
[root@Nginx-1 ~]# yum -y install kernel-devel openssl-devel

[root@Nginx-1 ~]# tar xf keepalived-1.2.13.tar.gz
[root@Nginx-1 ~]# cd keepalived-1.2.13
[root@Nginx-1 keepalived-1.2.13]# ./configure –prefix=/
–with-kernel-dir=/usr/src/kernels/2.6.18-194.el5-i686 && make && make
install

[root@Nginx-1 ~]# chkconfig –add keepalived
[root@Nginx-1 ~]# chkconfig keepalived on
[root@Nginx-1 ~]# chkconfig –list keepalived
[root@Nginx-1 ~]# service keepalived start|stop   

③ 、修改keepalived配置文件
[root@Nginx-1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
        crushlinux@163.com
}
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123
    }
    virtual_ipaddress {
        192.168.200.254
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123
    }
    virtual_ipaddress {
        192.168.200.253
    }
}

==================================================================
[root@Nginx-2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
        crushlinux@163.com
}
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123
    }
    virtual_ipaddress {
        192.168.200.254
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123
    }
    virtual_ipaddress {
        192.168.200.253
    }
}

[root@Nginx-1 ~]# service keepalived start
[root@Nginx-1 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc
pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2d:3d:97 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.202/24 brd 192.168.200.255 scope global eth0
    inet 192.168.200.254/32 scope global eth0
    inet6 fe80::20c:29ff:fe2d:3d97/64 scope link
       valid_lft forever preferred_lft forever

[root@Nginx-2 ~]# service keepalived start
[root@Nginx-2 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc
pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:6f:7d:87 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.203/24 brd 192.168.200.255 scope global eth0
    inet 192.168.200.253/32 scope global eth0
    inet6 fe80::20c:29ff:fe6f:7d87/64 scope link
       valid_lft forever preferred_lft forever
   
[root@client ~]# elinks –dump
   server 192.168.200.202
[root@client ~]# elinks –dump
   server 192.168.200.203
  

  
Nginx-50% 二台机械都施行监督Nginx进度的脚本
[root@Nginx-1 ~]# cat nginx_pidcheck
#!/bin/bash
while :
do
 nginxpid=`ps -C nginx –no-header | wc -l`
 if [ $nginxpid -eq 0 ]
 then
  /usr/local/nginx/sbin/nginx
  sleep 5
  nginxpid=`ps -C nginx –no-header | wc -l`
  echo $nginxpid
  if [ $nginxpid -eq 0 ]
  then
   /etc/init.d/keepalived stop
  fi
 fi
 sleep 5
done

[root@Nginx-1 ~]# nohup sh nginx_pidcheck &

那是实施无限循环的台本,两台Nginx机器上都有执行此脚本,每隔5秒执行1遍,用ps
-C是命令来搜集nginx的PID值到底是或不是为0,假诺是0的话,即Nginx已经经过死掉,尝试运行nginx进度;若是一而再为0,即Nginx运转退步,则关闭本机的Keeplaived服务,VIP地址则会由备机接管,当然了,整个网站就会整整由备机的Nginx来提供劳动了,那样保证Nginx服务的高可用。

本子测试:
[root@Nginx-1 ~]# netstat -anpt |grep nginx
tcp        0      0 0.0.0.0:80                 
0.0.0.0:*                   LISTEN      4321/nginx         
[root@Nginx-1 ~]# killall -s QUIT nginx
[root@Nginx-1 ~]# netstat -anpt |grep nginx
tcp        0      0 0.0.0.0:80                 
0.0.0.0:*                   LISTEN      59418/nginx

VIP转移测试:
[root@Nginx-1 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc
pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2d:3d:97 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.202/24 brd 192.168.200.255 scope global eth0
    inet 192.168.200.254/32 scope global eth0
    inet6 fe80::20c:29ff:fe2d:3d97/64 scope link
       valid_lft forever preferred_lft forever
   
[root@Nginx-2 ~]# service keepalived stop
停止 keepalived:                                          [确定]

[root@Nginx-1 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc
pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2d:3d:97 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.202/24 brd 192.168.200.255 scope global eth0
    inet 192.168.200.254/32 scope global eth0
    inet 192.168.200.253/32 scope global eth0
    inet6 fe80::20c:29ff:fe2d:3d97/64 scope link
       valid_lft forever preferred_lft forever

[root@client ~]# elinks –dump
   server 192.168.200.202
[root@client ~]# elinks –dump
   server 192.168.200.202

 

二、Web端安装Web服务

mv apache-tomcat-7.0.54 /usr/local/tomcat

澳门金沙国际 1java编制程序—怎样搭建Keepalived+Nginx+汤姆cat高可用负载均衡架构

① 、查看是或不是安装JDK
[root@Tomcat1 ~]# java -version
java version “1.8.0_171”
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
2、安装JDK
官网下载二进制包
解压到/usr/local/java
    tar zxvf jdk-8u171-linux-x64.tar.gz -C /usr/local/java/
修改环境变量/etc/profile
    JAVA_HOME=/usr/local/java
    PATH=$JAVA_HOME/bin:$PATH
    CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar
    export PATH JAVA_HOME CLASSPATH
使环境变量生效:
Keepalived实现高可用Web集群,汤姆cat高可用负载均衡框架结构。    source /etc/profile
3、下载Tomcat源码包:
    wget -O /opt/apache-tomcat-9.0.7.tar.gz

4、解压到/usr/local/tomcat
    tar zxvf /opt/apache-tomcat-9.0.7.tar.gz -C /usr/local/
5、修改Tomcat的主页
    rm -rf /usr/local/apache-tomcat-9.0.7/webapps/ROOT/*
    echo “Tomcat1”
>/usr/local/apache-tomcat-9.0.7/webapps/ROOT/index.html  #Tomcat1
    echo “Tomcat2”
>/usr/local/apache-tomcat-9.0.7/webapps/ROOT/index.html  #Tomcat2
陆 、测试汤姆cat能不能够健康运转
[root@Tomcat1 bin]# ./usr/local/apache-tomcat-9.0.7/bin
/startup.sh
Using CATALINA_BASE:  /usr/local/apache-tomcat-9.0.7
Using CATALINA_HOME:  /usr/local/apache-tomcat-9.0.7
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-9.0.7/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:     
/usr/local/apache-tomcat-9.0.7/bin/bootstrap.jar:/usr/local/apache-tomcat-9.0.7/bin/tomcat-juli.jar
Tomcat started.

 

  • 用户通过域名请求到DNS,由DNS解析域名后重临对应的IP地址,该IP及为Keepalived映射服务器的虚构IP
  • 透过该虚拟IP访问到相应的载荷均衡器,那里Nginx计划多少个,然后经过Keepalived来担保NG的高可用,符合规律情形下由Keepalived-M将虚拟IP映射转载至Nginx-M,若是Nginx-M出现故障,此时Keepalived会切换至Keepalived-S始发工作,从而保险了NG的单点故障难题。
  • 经过Nginx负载均衡器,将请求路由到相应的汤姆cat服务。

curl 10.10.10.130:8080  #返回Tomcat1
curl 10.10.10.131:8080  #返回Tomcat2

tar vxf jdk-7u65-linux-x64.tar.gz

三. 搭建Keepalived + Nginx + 汤姆cat的高可用负载均衡架构

迄今截止web端配置实现。

mv jdk1.7.0_65/ /usr/local/java

  1. 要求防患未然的软件

贰 、Nginx反向代理安装

编辑vim /etc/profile加

  • apache-tomcat-8.5.16.tar.gz
  • nginx-1.12.2.tar.gz
  • keepalived-1.3.9.tar.gz

一 、安装注重软件
    yum install -y gcc gcc-c++
    yum install -y pcre pcre-devel openssl openssl-devel zlib
zlib-devel
贰 、官网下载源码包
③ 、解压源码包
    tar zxvf nginx-1.14.0.tar.gz -C /tmp/
肆 、编写翻译安装
    useradd -s /bin/false -M nginx
    ./configure –user=nginx –group=nginx
–prefix=/usr/local/nginx-1.14.0/ –with-http_v2_module
–with-http_ssl_module –with-http_sub_module
–with-http_stub_status_module –with-http_gzip_static_module
–with-pcre
    make && make install
⑤ 、配置反向代理/usr/local/nginx/conf/nginx.conf
worker_processes  1;
pid        /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
  use epoll;
  worker_connections  51200;
}
http {
  include      mime.types;
  default_type  application/octet-stream;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
  sendfile        on;
  tcp_nopush      on;
  keepalive_timeout  65;
  tcp_nodelay on;
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types    test/plain application/x-javascript test/css
application/xml;
  gzip_vary on;

export JAVA_HOME=/usr/local/java       #设置java根目录

2.服务器准备

  upstream backend {
        server 10.10.10.130:8080;
        server 10.10.10.131:8080;

export PATH=$PATH:$JAVA_HOME/bin 
#在PATH环境变量中添加java跟目录的bin子目录

两台服务器如:192.168.10.11,192.168.10.12

  }
  server {
      listen      80;
      server_name  10.10.10.128;  #Nginx2改为:10.10.10.129
      location / {
          root  /var/www/html;
          index  index.php index.html index.htm;
          proxy_pass ;
      }
  }
}     
先测试再开行:
[root@Nginx1 ~]# /usr/local/nginx-1.14.0/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.14.0//conf/nginx.conf
syntax is ok
nginx: configuration file /usr/local/nginx-1.14.0//conf/nginx.conf test
is successful
[root@Nginx1 ~]# /usr/local/nginx-1.14.0/sbin/nginx
[root@Nginx1 ~]# lsof -i:80
COMMAND  PID  USER  FD  TYPE DEVICE SIZE/OFF NODE NAME
nginx  4896  root    6u  IPv4  18439      0t0  TCP *:http (LISTEN)
nginx  4897 nginx    6u  IPv4  18439      0t0  TCP *:http (LISTEN)

启动tomcat服务

3.设置要求的依赖包

curl 10.10.10.128  #轮询重回汤姆cat1 汤姆cat2
Nginx2同样的搭建。
迄今截至Nginx反向代理搭建完毕。

/usr/local/tomcat/bin/startup.sh

$ yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel
open openssl-devel

三 、使用Keepalived达成高可用

   打开浏览器,ip:8080显得tomcat主页

4.安装

1、安装:
    yum install keepalived -y
二 、修改配置文件/etc/keepalived/keepalived.conf
MASTER 端:
! Configuration File for keepalived
vrrp_script check_nginx {
        script “/etc/keepalived/check_nginx.sh”
        interval 2
        weight 2
}
global_defs {
  notification_email {
    acassen@firewall.loc
    failover@firewall.loc
    sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL1
}

关门服务;

4.1 安装Tomcat

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.10.10.100/24 dev eth0
  }
 track_script {
        check_nginx
  }
}
BACKUP端:
修改:
router_id LVS_DEVEL2
state BACKUP
priority 90

  /usr/local/tomcat/bin/shutdown.sh

各自在两台服务器中装置汤姆cat,解压apache-tomcat-8.5.16.tar.gz及可做到安装。

检测Nginx脚本:
#!/bin/bash
nginxpid=`ps -C nginx –no-header | wc -l`
if [ $nginxpid -eq 0 ];then
        /etc/init.d/keepalived stop
fi
chmod +x /etc/keepalived/check_nginx.sh

成立1个web 站点

4.2 安装Nginx

重启Keepalived服务
service keepalived restart

cd webapps/

  • 解压安装包:tar -zxvf nginx-1.12.2.tar.gz
  • 进入到nginx-1.12.2目录:cd nginx-1.12.2
  • 编译:

④ 、检验服务的高可用

mkdir web

$ ./configure –with-http_stub_status_module
–with-http_ssl_module –prefix=/usr/local/nginx

$ sudo make && sudo make install

Nginx1执行:killall nginx
意识 web访问还是平常

vim index.jsp

4.3 安装Keepalived

Tomcat1执行:/usr/local/apache-tomcat-9.0.7/bin/shutdown.sh
发觉 web访问依然寻常

Server Info:  

  • 解压安装包:tar -zxvf keepalived-1.3.9.tar.gz
  • 进入到keepalived-1.3.9目录:cd keepalived-1.3.9
  • 执行编译:

高可用的条件搭建实现。

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

$ ./configure –prefix=/usr/local/keepalived –sysconf=/etc

$ sudo make && sudo make install

Linux公社的RSS地址

<br>

5 配置

正文永久更新链接地址:https://www.linuxidc.com/Linux/2018-06/152740.htm

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

5.1 分别铺排两台服务器的Nginx

澳门金沙国际 2

<br>

  • 分别修改两台服务器nginx配置文件,vi /usr/local/nginx/conf/nginx.conf
  • 内容如下:

SessionPort:<%=request.getServerPort()%>

nginx进程数

worker_processes 1;

单个进度最明斯克接数

events {

worker_connections 1024;

}

http服务器配置

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

长连接超时时间,单位是秒

keepalive_timeout 65;

upstream负载均衡配置,配置路由到tomcat的服务地方以及权重

upstream localhost{

server 192.168.10.11:8080 weight=2;

server 192.168.10.12:8080 weight=2;

}

虚拟主机的铺排

server {

监听端口

listen 80;

域名能够有八个,用空格隔绝

server_name localhost;

location / {

root html;

index index.html index.htm;

nginx跟后端服务器连接超时时间

proxy_connect_timeout 3;

后端服务器数据回传时间

proxy_send_timeout 30;

总是成功后,后端服务器响应时间

proxy_read_timeout 30;

proxy_pass ;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}

<br>

5.2 主Keepalived配置

<%

  • 修改11服务器的keepalived配置文件,vi /etc/keepalived/keepalived.conf
  • 内容如下:

  out.println(“server one”);

! Configuration File for keepalived

大局配置

global_defs {

keepalived切换的时候,发新闻到钦命的email,可安顿多个email

notification_email {

feinik1@foxmail.com

feinik2@foxmail.com

}

布告邮件从哪些地点产生

notification_email_from feinik@foxmail.com

布告邮件的smtp地址

smtp_server smtp.exmail.qq.com

连接smtp服务器的超时时间,单位秒

smtp_connect_timeout 30

Keepalived的机械标识,3个互联网内维持唯一

router_id nginx-master

}

施行脚本配置

vrrp_script chk_nginx {

本子所在路径

script “/home/project/keepalived/check_nginx.sh”

剧本执行间隔时间,秒

interval 2

优先级

weight 2

}

keepalived实例配置

vrrp_instance VI_1 {

点名实例的起来状态,MASTE陆风X8或BACKUP二种情状,并且需求大写

state MASTER

实例绑定的网卡

interface ens33

虚拟路由标识,是2个数字,整个VKuga途乐P内唯一,假如keepalived配置了主备,必要平等

virtual_router_id 51

优先级,数值愈大,优先级越高

priority 100

MASTE卡宴与BACKUP之间联检的时刻间隔,单位为秒

advert_int 1

通讯验证

authentication {

auth_type PASS

auth_pass feinik

}

追踪外围脚本

track_script {

那边配置vrrp_script的名称

chk_nginx

}

虚构ip配置,可配置多少个

virtual_ipaddress {

192.168.10.200

}

}

%>

5.3 备Keepalived配置

修改Tomcat的server.xml文件

  • 修改12服务器的keepalived配置文件,vi /etc/keepalived/keepalived.conf
  • 内容如下:

cp server.xml server.xml.bak

! Configuration File for keepalived

大局配置

global_defs {

keepalived切换的时候,发新闻到内定的email,可配备多少个email

notification_email {

feinik1@foxmail.com

feinik2@foxmail.com

}

notification_email_from feinik@foxmail.com

smtp_server smtp.exmail.qq.com

smtp_connect_timeout 30

router_id nginx-master

}

vrrp_script chk_nginx {

script “/home/project/keepalived/check_nginx.sh”

interval 2

weight 2

}

vrrp_instance VI_1 {

state BACKUP

interface ens33

virtual_router_id 51

priority 99

advert_int 1

authentication {

auth_type PASS

auth_pass feinik

}

track_script {

chk_nginx

}

virtual_ipaddress {

192.168.10.200

}

}

vim server.xml

5.4 Nginx状态检查脚本创造

<Host name=”localhost”  appBase=”webapps” unpackWARs=”true”
autoDeploy=”true”>

新建Nginx的场合检查脚本:check_nginx.sh

      <Context docBase=”web” path=””
reloadable=”flase”>                                                  

内容如下:

      </Context>

NGINX=/usr/common/nginx/sbin/nginx

PORT=80

nmap localhost -p
澳门金沙国际 3PORT/tcp
open”

if [ $? -ne 0 ];then

$NGINX -s stop

$NGINX

sleep 5

nmap localhost -p
澳门金沙国际 4PORT/tcp
open”

[ $? -ne 0 ] && cd /usr/common/keepalived/sbin && pkill keepalived

echo “stoped”

fi

      </Host>

4 运营测试

/usr/local/tomcat/bin/shutdown.sh

  • 为了更直观的查阅到keepalived切换的效应,将11服务器中的nginx的upstream服务只安插11的tomcat服务地方,12服务器中的upstream服务只布置12的tomcat服务地点,那样只需求着眼将11服务器中的nginx关闭看使用虚拟ip是不是足以访问到12服务器的tomcat。
  • 独家运营三个服务器中的tomcat、nginx、keepalived,访问虚拟ip:192.168.10.200,能够查看到走访的是主keepalived服务器的tomcat

/usr/local/tomcat/bin/startup.sh

澳门金沙国际 5java编程—如何搭建Keepalived+Nginx+汤姆cat高可用负载均衡架构

打开浏览器,ip:8080显得tomcat主页

  • image.png
  • 关门11服务器的nginx,nginx -s
    stop,再一次做客虚拟ip,如下:表明主keepalived通过安排的台本检查和测试到了本服务的nginx服务挂掉了,所以立刻切换至了备的keepalived,那时12服务器的keepalived升为了主,所以就走访到了12服务器的tomcat。

  第二台

澳门金沙国际 6java编制程序—怎样搭建Keepalived+Nginx+汤姆cat高可用负载均衡架构澳门金沙国际 7java编程—怎么样搭建Keepalived+Nginx+汤姆cat高可用负载均衡架构

与第②台基本一致,更改测试页的ip地址

scp root@172.16.1.2:/usr/local/tomcat/webapps/web/index.jsp .

scp root@172.16.1.2:/usr/local/tomcat/conf/server.xml  .

开行服务/usr/local/tomcat/bin/startup.sh

查阅服务

netstat -anpult |grep java

浏览器访问测试 http://ip:8080

展现经常

2.准备nginx

第一台

源码安装nginx,

修改配置文件,配置负载均衡

cp /usr/local/nginx/conf/nginx.conf{,.bak}

vim /usr/local/nginx/conf/nginx.conf

加upstream tomcat_server {

        server 172.16.1.12:8080 weight=1;

        server 172.16.1.2:8080 weight=1;

}

proxy_pass http://tomcat\_server;

语法检查

/usr/local/nginx/sbin/nginx –t

起步服务/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

或者killadd -1 nginx

打开浏览器访问:
http://ip ,该ip为nginx 的ip,刷新浏览器,会来得两台

第二台

nginx-2服务器配置:

布署方式与服务器nginx-1一致

客户端测试:

开辟浏览器访问:    
#不停刷新可观看由于权重相同,页面会反复切换

3.装置配备keepalived

yum -y install keepalived

修改配置文件vim keepalived.conf

安插一个实例名字为VI_2,状态作为主,优先级100

vrrp_instance VI_2 {

    #备用服务器上为 BACKUP

    state MASTER

    #绑定vip的网卡为ens33

    interface ens33

    virtual_router_id 51

    #备用服务器上为90

    nopreempt                                        #为不抢占情势

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    #track_script {

     #nginx

    #}

    virtual_ipaddress {                               #配备虚拟ip地址

        172.16.1.200

    }

}

vrrp_instance VI_1
{                                  #铺排另八个实例 VI_1,状态为 从,优先级为90

    state BACKUP

    interface ens33

    virtual_router_id 51

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress
{                                  #配备虚拟ip地址为254

        192.168.200.254

    }

}

安顿第2台

Vim keepalive.conf

vrrp_instance VI_2 {                                            
#实例名字为VI_2,状态作为从,优先级90

    #备用服务器上为 BACKUP

    state SLAVE

    #澳门金沙国际 ,绑定vip的网卡为ens33

    interface ens33

    virtual_router_id 51

    #备用服务器上为90

    nopreempt

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

       virtual_ipaddress {

       
172.16.1.200                                                         #配备虚拟ip地址

    }

}

vrrp_instance VI_1
{                                                         
#安顿另三个实例 VI_1,状态为主,优先级为100

    state MASTER

    interface ens33

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.200.254

    }

}

测试,关闭一台keepalived,打开浏览器,刷新页面,两台机器网页交替更新,测试平常

完结高可用。

4.装置memcached,达成session会话保持。

yum -y install gcc
openssl-devel pcre-devel zlib-devel

先安tar xf
libevent-2.0.15-stable.tar.gz

cd
libevent-2.0.15-stable

./configure
–prefix=/usr/local/libevent && make && make install

再装memcached 

tar xf
memcached-1.4.5.tar.gz

cd memcached-1.4.5

./configure
–prefix=/usr/local/memcached –with-libevent=/usr/local/libevent/ &&
make && make install 

配置

/usr/local/memcached/bin/memcached -u
root -m 512M -n 10 -f 2 -d -vvv -c 512

vim /etc/ld.so.conf

include
ld.so.conf.d/*.conf

/usr/local/libevent/lib/

Ldconfig

/usr/local/memcached/bin/memcached -u
root -m 512M -n 10 -f 2 -d -vvv -c 512

 netstat  -antp| grep
:11211

突显端口开启

安装telnet测试yum -y install telnet

telnet 192.168.200.103
11211

来得平常

末了执行让Tomcat-1  汤姆cat-2
通过(msm)连接到Memcached

 将session包中的“*.jar复制到/usr/local/tomcat/lib/
下面

cp session/*
/usr/local/tomcat/lib/使tomcat支持memcached, 

tomcat-1  和  tomcat-2   配置文件一模一样.

vim
/usr/local/tomcat7/conf/context.xml

重启服务tomcat

/usr/local/tomcat/bin/shutdown.sh

/usr/local/tomcat/bin/startup.sh

 倘若成功,tomcat与Memcached 端口会连在一起,前后有生成

Tomcat-1与Tomcat-2如下图

netstat -antp|grep java

tcp        0      0
::ffff:127.0.0.1:8005       :::*                        LISTEN     
62496/java         

tcp        0      0
:::8009                     :::*                        LISTEN     
62496/java         

tcp        0      0
:::8080                     :::*                        LISTEN     
62496/java         

tcp        0      0
::ffff:192.168.200.10:28232 ::ffff:192.168.200.10:11211 ESTABLISHED
62496/java         

tcp        0      0
::ffff:192.168.200.10:28231 ::ffff:192.168.200.10:11211 ESTABLISHED
62496/java         

tcp        0      0
::ffff:192.168.200.10:28230 ::ffff:192.168.200.10:11211 ESTABLISHED
62496/java         

tcp        0      0
::ffff:192.168.200.10:28228 ::ffff:192.168.200.10:11211 ESTABLISHED
62496/java         

tcp        0      0
::ffff:192.168.200.10:28229 ::ffff:192.168.200.10:11211 ESTABLISHED
62496/java         

[root@tomcat-1 ~]# netstat
-antp|grep memcached

tcp        0      0
0.0.0.0:11211               0.0.0.0:*                   LISTEN     
62402/memcached     

tcp        0      0
192.168.200.103:11211       192.168.200.103:28230       ESTABLISHED
62402/memcached    

tcp       45      0
192.168.200.103:11211       192.168.200.103:28228       ESTABLISHED
62402/memcached    

tcp        0      0
192.168.200.103:11211       192.168.200.103:28232       ESTABLISHED
62402/memcached    

tcp        0      0
192.168.200.103:11211       192.168.200.103:28229       ESTABLISHED
62402/memcached    

tcp        0      0
192.168.200.103:11211       192.168.200.103:28231       ESTABLISHED
62402/memcached    

tcp        0      0
:::11211                    :::*                        LISTEN     
62402/memcached   

相关文章