1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Nginx+tomcat+redis实现高可用负载均衡session共享集群+redis哨兵监控

Nginx+tomcat+redis实现高可用负载均衡session共享集群+redis哨兵监控

时间:2024-01-23 18:15:21

相关推荐

Nginx+tomcat+redis实现高可用负载均衡session共享集群+redis哨兵监控

实验拓扑图```

实验步骤:一.做nginx和tomcat的代理二.做keepalived+nginx的双机热备份,vip:192.168.10.100三.做keepalived+redis的哨兵模式,vip:192.168.10.200四.在tomcat上做会话保持实验环境:Keepalived主+nginx101:192.168.10.1Keepalived从+nginx102:192.168.10.2Tomcat103:192.168.10.3Tomcat104:192.168.10.4Keepalived主+Redis主:192.168.10.5keepalived从+Redis从1:192.168.10.6Keepalived从+Redis从2: 192.168.10.7

1.两个tomcat主机都操作

①在两台tomcat服务器上都执行此操作,关闭防火墙,关闭selinux

[root@localhost ~]# systemctl stop firewalld[root@localhost ~]# setenforce 0

2.tomcat 10.3操作

①将tomcat7.0软件包和redis插件上传到服务器/usr/src目录下,解压tomcat包并移动tomcat到/usr/local/目录下并命名为tomcat

[root@localhost ~]# cd /usr/src/[root@localhost src]# tar xzvf apache-tomcat-7.0.90.tar.gz [root@localhost src]# mv apache-tomcat-7.0.90 /usr/local/tomcat

②新建test.jsp测试页到tomcat项目ROOT目录下,并启动tomcat服务

[root@localhost src]# vim /usr/local/tomcat/webapps/ROOT/test.jsp -------------------------------写入test页面内容--------------------------<html><body bgcolor="yellow"><center><%= request.getSession().getId() %><h1>192.168.10.3</h1><h1>port:8080</h1><h1>this is Tomcat-node1! </h1></center></body</html><%@ page contentType="text/html;charset=UTF-8" isELIgnored="false"%>SessionID:<%=session.getId()%><BR>SessionIP:<%=request.getServerName()%> <BR>SessionPort:<%=request.getServerPort()%><%out.println("This is Tomcat server 223 !");%>

[root@localhost src]# /usr/local/tomcat/bin/startup.sh #启动tomcat服务[root@localhost src]# ss -tnl | grep 8080#查看8080是否监听

3.tomcat 10.4操作

①将tomcat7.0软件包和redis插件上传到服务器/usr/src目录下,解压tomcat包并移动tomcat到/usr/local/目录下并命名为tomcat

[root@localhost ~]# cd /usr/src/[root@localhost src]# tar xzvf apache-tomcat-7.0.42.tar.gz [root@localhost src]# mv apache-tomcat-8.5.42 /usr/local/tomcat

②新建test.jsp测试页到tomcat项目ROOT目录下,并启动tomcat服务

[root@localhsot src]# vim /usr/local/tomcat/webapps/ROOT/test.jsp-------------------------------写入test页面内容--------------------------<html> <body bgcolor="red"> <center> <%= request.getSession().getId() %> <h1>192.168.10.4</h1> <h1>port:8080</h1> <h1>this is Tomcat-node2! </h1> </center> </body></html> <%@ page contentType="text/html;charset=UTF-8" isELIgnored="false"%>SessionID:<%=session.getId()%><BR>SessionIP:<%=request.getServerName()%> <BR>SessionPort:<%=request.getServerPort()%><%out.println("This is Tomcat server 224 !");%>

[root@tomcat-node2 src]# /usr/local/tomcat/bin/startup.sh #启动tomcat服务[root@localhsot src]# ss -tnl | grep 8080

2.Nginx-master主机上操作内容

①使用yum方式安装Nginx和keepalived服务程序

[root@Nginx-master /]# yum install -y nginx keepalived

②配置nginx-master代理服务器,新建一个代理配置文件,检测nginx配置文件语法是否正确并启动Nginx服务

[root@Nginx-master /]# vim /etc/nginx/conf.d/nginx-master.conf

upstream tomcat {#定义负载均衡列表 server 192.168.10.3:8080 max_fails=2 fail_timeout=2;##tomcat-node1 server 192.168.10.4:8080 max_fails=2 fail_timeout=2;##tomcat-node2#max_fails=2连续检测失败两次认为该节点不可用##fiil_timeout=2 每次检测超过2秒未响应为检测失败}server {listen 80;##客户端访问的端口server_name ;##绑定的域名location / {proxy_pass http://tomcat;##反代到负载均衡服务器列表}}}

[root@Nginx-master /]# nginx -t #检测NGINX语法nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@Nginx-master /]# nginx#启动NGINX服务

③配置Nginx-master的keepalived服务,修改keepalived配置文件,添加nginx健康检查脚本,启动keepalived服务

[root@Nginx-master /]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalivedglobal_defs {##默认配置段 router_id nginx-proxy-master##定义设备名称}vrrp_script chk_ngx {##定义nginx服务状态检查规则 script "/etc/keepalived/scripts/chk_ngx.sh"##检查脚本的位置 interval 3##每3秒执行一次脚本检查 fail 2 ##连续失败2次就认定为状态检查失败 rasi 1 ##只要成功1次就认定为成功 weight -2##检测失败本设备优先级减2}vrrp_instance nginx-proxy {##定义备份组实例 state MASTER##定义本设备在实例中的角色 interface ens33##vip要绑定的网卡 virtual_router_id 22##routeid,同一个组要一致 priority 100##本设备的优先级 advert_int 3##设备间每3秒进行一次心跳检测 authentication {##验证信息 auth_type PASS##验证类型 auth_pass nginx-proxy##验证的密码 } virtual_ipaddress { 192.168.10.100 dev ens33 label ens33:1##定义vip并设定一个网卡别名 } track_script { chk_ngx##调用nginx健康检查脚本 } }

##新建nginx状态检查脚本

[root@Nginx-master /]# mkdir /etc/keepalived/scripts##创建脚本存放目录[root@Nginx-master /]# vim /etc/keepalived/scripts/chk_ngx.sh##新建脚本文件

-------------------------------写入以下内容--------------------------#!/bin/bash#此脚本为检测nginx状态,如果正常则退出0,nginx服务未开启则会自动启动一次,如果启动失败则退出返回1STATUS=`netstat -anpt | grep 80 |wc -l`if [ "$STATUS" -eq 0 ];then/usr/sbin/nginx#如果开启当nginx死掉会尝试自动启动一次服务sleep 4STATUS=`netstat -anpt | grep 80 |wc -l`if [ "$STATUS" -eq 0 ];thenexit 1elseexit 0fielseexit 0fi

[root@Nginx-master /]# chmod +x /etc/keepalived/scripts/chk_ngx.sh #脚本加执行权限[root@Nginx-master scripts]# systemctl restart keepalived #启动服务[root@Nginx-master scripts]# ifconfig |grep -A 2 ens33:1#查看vip是否配置ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.10.100 netmask 255.255.255.255 broadcast 0.0.0.0 ether 00:0c:29:ab:e3:ca txqueuelen 1000 (Ethernet)

2.Nginx-backup主机上操作内容

①使用yum方式安装Nginx和keepalived服务程序

[root@Nginx-backup /]# yum install -y nginx keepalived

②配置nginx-master代理服务器,新建一个代理配置文件,检测nginx配置文件语法是否正确并启动Nginx服务

[root@Nginx-backup /]# vim /etc/nginx/conf.d/nginx-backup.conf

upstream tomcat { server 192.168.10.3:8080 max_fails=2 fail_timeout=2; server 192.168.10.4:8080 max_fails=2 fail_timeout=2;}server { listen 80; server_name ; location / { proxy_pass http://tomcat; }}

root@Nginx-backup /]# nginx -t #检测NGINX语法nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfulroot@Nginx-backup /]# nginx#启动NGINX服务[root@nginx-backup /]# lsof -i:80##查看服务是否启动并监听

③配置Nginx-backup的keepalived服务,修改keepalived配置文件,启动keepalived服务

[root@Nginx-backup /]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalivedglobal_defs {router_id nginx-proxy-slave}vrrp_instance nginx-proxy {state BACKUP interface ens33 virtual_router_id 22 priority 99 advert_int 3 authentication {auth_type PASS auth_pass nginx-proxy } virtual_ipaddress {192.168.10.100 dev ens33 label ens33:1 }}

④客户端添加一条hosts解析记录解析到vip的192.168.10.100,然后使用浏览器访问/test.jsp测试页面验证负载均衡是否生效

![测试]验证:

从图中可以看出会话标识符不一样,所以下面将做tomcat的会话保持

二、配置tomcat+redis会话共享

配置redis-master单机redis服务器,并配置tomcat使用redis缓存会话①将redis源码包上传到服务器/usr/src目录下,关闭防火墙,关闭selinux,解压redis源码包,编译并编译安装

[root@redis-master /]# cd /usr/src/[root@redis-master src]# tar xzvf redis-5.0.5.tar.gz[root@redis-master redis-5.0.5]# yum install -y gcc gcc-c++[root@redis-master redis-5.0.5]# make && make install

②初始化redis配置信息,修改redis配置文件,启动redis服务

[root@redis-master redis-5.0.5]# ./utils/install_server.sh 【一路回车】[root@redis-master redis-5.0.5]# vim /etc/redis/6379.conf protected-mode no##此参数改为no,默认yes的话tomcat无法连接#bind 127.0.0.1##将此参数注释掉,监听所有地址[root@redis-master redis-5.0.5]# /etc/init.d/redis_6379 start [root@redis-master redis-5.0.5]# netstat -anpt | grep 637912. tcp 00 0.0.0.0:6379 0.0.0.0:*LISTEN66150/redis-servertcp6 00 :::6379 :::*LISTEN66150/redis-server

③此步骤在两台tomcat服务器同样的操作,将打包对应版本的三个jar包上传到服务器/usr/src目录下,然后复制到tomcat根目录下的lib目录中,修改tomcat下的context.xml配置文件,添加redis配置项

[root@tomcat-node1 /]# cd /usr/src/[root@tomcat-node1 src]# cp commons-pool2-2.3.jar /usr/local/tomcat/lib/[root@tomcat-node1 src]# cp tomcat-redis-session-manager-master-2.0.0.jar /usr/local/tomcat8.5/lib/[root@tomcat-node1 src]# cp jedis-2.7.3.jar /usr/local/tomcat8.5/lib/[root@tomcat-node1 src]# vim /usr/local/tomcat8.5/conf/context.xml

-------------------------修改为以下内容---------------------<?xml version="1.0" encoding="UTF-8"?><Context> <WatchedResource>WEB-INF/web.xml</WatchedResource> <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /><Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="192.168.10.200" port="6379" database="0" maxInactiveInterval="60" /></Context>==提示:如果远程redis设置了密码,则就多加一行配置:password="123456"==

[root@tomcat-node1 src]# /usr/local/tomcat/bin/shutdown.sh ##停止tomcat服务[root@tomcat-node1 src]# /usr/local/tomcat/bin/startup.sh

一、安装redis和哨兵服务1、下载redis服务软件包到服务器,解压后并编译安装(主从步骤一样,这里只安装一次)

[root@centos01 /]# tar xzvf redis-3.2.9.tar.gz [root@centos01 /]# cd redis-3.2.9/[root@centos01 redis-3.2.9]# make && make install [root@centos01 redis-3.2.9]# ./utils/install_server.sh Welcome to the redis service installer##一路回车Installation successful!

2、修改redis配置文件,修改为后台运行、监听所有地址(其他参数按需修改),重启redis服务

[root@centos01 redis-3.2.9]# vim /etc/redis/6379.conf bind 0.0.0.0daemonize yes[root@centos01 redis-3.2.9]# /etc/init.d/redis_6379 restart3、复制哨兵的配置文件和启动文件到redis安装目录 [root@centos01 redis-3.2.9]# cp sentinel.conf /etc/redis/[root@centos01 redis-3.2.9]# cp src/redis-sentinel /etc/redis/

二、设置redis主从复制,并配置哨兵监控1、登录slave-redis服务器6和7,将master设置为192.168.10.5

[root@slave /]# redis-cli 127.0.0.1:6379> SLAVEOF 192.168.10.5 6379##设置master为10.5127.0.0.1:6379> INFO Replication

3、修改master和slave服务器的sentinel.conf配置文件(master和slave配置一致)##修改master的哨兵文件

[root@master /]# cd /etc/redis/[root@master /]# vim /etc/redis/sentinel.conf

port 26379dir "/tmp"sentinel monitor mymaster 192.168.10.5 6379 2#(指定监控的master地址和端口号,1表示多个sentinel同意才进行主从切换)sentinel down-after-milliseconds mymaster 4000#(超过多少毫秒连接不到master认定为master死掉)sentinel failover-timeout mydaemonize yes#(后台运行)logfile "/var/log/redis_sentinel.log"#(日志文件保存路径)

[root@master redis]# ./redis-sentinel sentinel.conf #启动哨兵服务

三、配置keepalived实现高可用

1、使用yum方式安装keepalived软件包(master和slave都安装)

[root@master redis]# yum install -y keepalived[root@master redis]# ./redis-sentinel sentinel.conf #启动哨兵服务2、修改keepalived配置文件10.5上Master配置文件内容

! Configuration File for keepalivedglobal_defs {router_id redis-10.5##设备名称}vrrp_instance VI_redis {state MASTER##当前设备为master interface eth0##vip绑定的网卡 virtual_router_id 79##备份组id,同一备份组要一致 priority 100##优先级,优先级高的成为master advert_int 3##每多少秒进行一次健康检查 authentication {auth_type PASS auth_pass redis } virtual_ipaddress {192.168.10.200 dev eth0 label eth0:1##vip地址并设置一个网卡别名 }}在10.6、10.7的BACKUP上配置文件内容! Configuration File for keepalivedglobal_defs {router_id redis-10.6}vrrp_script chkmaster {script "/etc/keepalived/scripts/chkmaster.sh"#检查当前redis是否为master interval 3#每3秒检查一次 weight 2#当本机为master检测结果返回0,将优先级加2 fall 2 ##检测失败多少次就才认为是真的失败 rise 1 ##检测成功1为就认为是成功的 }vrrp_instance VI_redis {state BACKUP#本机的状态信息 interface eth0#vip绑定的网卡 virtual_router_id 79##备份组id,同一备份组要一致 priority 99##优先级,优先级高的成为master advert_int 3##每多少秒进行一次健康检查 authentication {auth_type PASS auth_pass redis } virtual_ipaddress {192.168.10.200 dev eth0 label eth0:1 } track_script {##调用状态检查 chkmaster }}

状态检查脚本内容:#!/bin/bashSTATUS=`/usr/local/bin/redis-cli info | grep role:master | wc -l`echo $STATUSif [ "$STATUS" -eq 0 ];then exit 1else exit 0fi

在主、从redis上重启keepalived

[root@localhsot~]#systemctl restart keepalive

从上图中可以看出tomcat的sentinel会话已经成功,redis的哨兵模式也已搭建完毕,通过netstat -anpt | grep redis查看redis的26379端口,哨兵则开启!当105上的redis-master down掉后,redis-master将会转移到106或107上,脚本上说明当将测到redis为master,所在服务器上keepalived的优先级将加2,抢占原来主的vip:192.168.10.200

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。