目录
一、基本思路
1.利用nginx实现负载均衡和反向代理,减轻主服务器访问压力;
2.利用springboot实现多tomcat的session共享;
IP | 角色 |
192.168.0.170 | nginx(主)服务器、Tomcat服务器 |
192.168.0.171 | nginx(从)服务器、Tomcat服务器 |
二、安装Tengine(Nginx)并添加服务
1.Tengine简介
Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。
2.Tengine下载
http://tengine.taobao.org/download.html下载对应的Tengine版本
3.安装nginx对应的依赖库
yum -y install gcc openssl-devel pcre-devel zlib-devel
4.解压缩Tengine到相对应的文件夹
tar -zxvf 到相对应的文件夹
5.配置nginx对应的选项
./configure
6.安装Tengine
make & make install
7.添加nginx到服务并设置自启动
vi /lib/systemd/system/nginx.service --添加文件
---------------------添加以下代码----------------------------
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# 修改文件权限
cd /lib/systemd/system/
chmod 745 nginx.service
# 设置为开机启动 systemctl enable nginx.service
# 启动nginx服务 systemctl start nginx.service
# 设置开机自启动 systemctl enable nginx.service
# 停止开机自启动 systemctl disable nginx.service
# 查看服务当前状态 systemctl status nginx.service
# 重新启动服务 systemctl restart nginx.service
# 查看所有已启动的服务 systemctl list-units --type=service
8.启动Nginx服务,浏览输入http://192.168.0.170如出现nginx相应画面标识安装成功,nginx默认端口80
三、安装Keepalived并添加服务
1.keepalived下载
下载方式一:
https://www.keepalived.org/download.html下载最新稳定版的keepalived,ftp上传到home目录下
yum install -y gcc gcc-c++ openssl-devel 安装依赖库,如已经安装可不比进行此操作
下载方式二:
wget https://www.keepalived.org/software/keepalived-2.0.19.tar.gz
2.解压缩keepalived文件
进入到home目录先
cd /home
tar -zxvf keepalived-2.0.19.tar.gz
3.检查配置
cd keepalived-2.0.19
./configure --prefix=/usr/local/keepalived
若系统出现如下警告
“*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.”
yum -y install libnl libnl-devel 执行此命令,重新进行./configure --prefix=/usr/local/keepalived 操作
4.安装
make & make install
5.配置keepalived服务
mkdir -p /etc/keepalived
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp keepalived/etc/init.d/keepalived /etc/init.d/
chmod 755 /etc/init.d/keepalived
vi /lib/systemd/system/keepalived.service 编辑keepalived服务文件
将所有的字符串 “/usr/local/keepalived” 替换为空白,替换完如下:
----------------------------------------------------------------------------------------------------------
[Unit]
Description=LVS and VRRP High Availability Monitor
After=network-online.target syslog.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/keepalived.pid
KillMode=process
EnvironmentFile=-/etc/sysconfig/keepalived
ExecStart=/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
6.修改keepalived.conf
------------------------------------------------按照以下代码修改(主Nginx服务器192.168.0.170)-----------------------------------
! Configuration File for keepalived
global_defs {
## keepalived 自带的邮件提醒需要开启 sendmail 服务。 建议用独立的监控或第三方 SMTP
router_id tdrc ## 标识本节点的字条串,通常为 hostname
}
## keepalived 会定时执行脚本并对脚本执行的结果进行分析,动态调整 vrrp_instance 的优先级。如果脚本执行结果为 0,并且 weight 配置的值大于 0,则优先级相应的增加。如果脚本执行结果非 0,并且 weight配置的值小于 0,则优先级相应的减少。其他情况,维持原本配置的优先级,即配置文件中 priority 对应的值。
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径
interval 2 ## 检测时间间隔
weight -20 ## 如果条件成立,权重-20
}
## 定义虚拟路由, VI_1 为虚拟路由的标示符,自己定义名称
vrrp_instance VI_1 {
state MASTER ## 主节点为 MASTER, 对应的备份节点为 BACKUP
interface ens33 ## 绑定虚拟 IP 的网络接口,与本机 IP 地址所在的网络接口相同, 我的是 eth0
virtual_router_id 33 ## 虚拟路由的 ID 号, 两个节点设置必须一样, 可选 IP 最后一段使用, 相同的 VRID 为一个组,他将决定多播的 MAC 地址
mcast_src_ip 192.168.0.171 ## 本机 IP 地址
priority 100 ## 节点优先级, 值范围 0-254, MASTER 要比 BACKUP 高
nopreempt ## 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题
advert_int 1 ## 组播信息发送间隔,两个节点设置必须一样, 默认 1s
## 设置验证信息,两个节点必须一致
authentication {
auth_type PASS
auth_pass 1111 ## 真实生产,按需求对应该过来
}
## 将 track_script 块加入 instance 配置块
track_script {
chk_nginx ## 执行 Nginx 监控的服务
} #
# 虚拟 IP 池, 两个节点设置必须一样
virtual_ipaddress {
192.168.0.180 ## 虚拟 ip,可以定义多个
}
}
-----------------------------------------------------------------------------------------------------------------------
------------------------------------------------按照以下代码修改(从)---------------------------------------
! Configuration File for keepalived
global_defs {
router_id tdrc
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 33
mcast_src_ip 192.168.0.171
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.0.180
}
}
--------------------------------------------------------------------------------------------------------------------------
7. /etc/keepalived 下添加nginx_check.sh脚本
-----------------------------------------------主(从)下添加以下代码------------------------------------------------------------------
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
------------------------------------------------------------------------------------------------------------------
保存后,给脚本赋执行权限:
# chmod +x /etc/keepalived/nginx_check.sh
8.重启nginx和keepqlived
浏览器访问(VIP)192.068.0.180 或192.168.0.170 或192.168.0.171
四、Nginx配置多个应用
测试需提前在192.168.0.170和192.168.0.171上部署号相应的测试应用
vi /usr/local/nginx/conf/nginx.conf
htt{}中添加
upstream cluster {
# 服务器访问的路径,可以指定多个服务器,此处可根据需求配置单个或者多个应用服务器、负载均衡策略
server 192.168.0.170:8080 weight=1;
server 192.168.0.171:8080 weight=1;
}
server{}中添加
location / {
proxy_pass http://cluster2;
root html;
index index.html index.htm;
}
五、实现多应用服务器session共享
测试应用采用的是springboot,springboot自带session注释,@EnableRedisHttpSession
部分内容参考自https://blog.csdn.net/qq_33200967/article/details/80707616
来源:CSDN
作者:窦朋飞
链接:https://blog.csdn.net/doupengzp/article/details/103761850