双机容灾&负载均衡&动态代理:(keepalived结合openresty+redis实现)
日期: 2020-12-12 分类: 跨站数据测试 425次阅读
鉴于杰克公司部分系统偶尔宕机,造成一定的生产影响,做了这套双机系统,完整代码有空再更,不足之处欢迎指出
可视化页面路由管理页面,支持多台软件服务器
支持实时添加软件服务器,修改和删除操作,包含服务器掉线短信提醒功能
效果图,技术原理图
目录
采取keepalived+openresty+redis节点同时部署在一台服务器的策略,路由服务原子性+动态代理的高效性
1.keepalived的yum安装和基础指令
yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
yum install -y keepalived
systemctl enable keepalived自启
systemctl start keepalived启动
systemctl stop keepalived关闭
systemctl restart keepalived重启
2.keepalived基础配置详解
***
! Configuration File for keepalived
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 nginx01 # router_id 唯一标识符
vrrp_skip_check_adv_addr
vrrp_stricti #使用yum安装的话,这里需要注释掉,不然无法访问
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check_nginx {
script "/etc/keepalived/nginx_check.sh" #nginx服务检查脚本
interval 1 #检测间隔要大于sh脚本中的sleep时间
weight -2
}
vrrp_instance VI_1 {
state MASTER #从机设置为BACKUP
interface enp0s3 #网卡
virtual_router_id 52 #默认为51 配置完发现主备切换有问题 更改为52 发现好了 原因未知
priority 200 #主备的优先级priority
advert_int 1 #检查时间1秒
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.56.201/24 #vip地址
}
}
}
***
3.监测脚本nginx_check.sh
***
#检测nginx服务是否正常
if [ $(netstat -tlnp|grep nginx|wc -l) -ne 1 ]
then
#尝试重启nginx
systemctl start nginx
sleep 2
#再次检测
if [ $(netstat -tlnp|grep nginx|wc -l) -ne 1 ]
then
#nginx启动失败-->停止当前keepalived,自动切换到另一台路由服务器
systemctl stop keepalived
fi
fi
#检测redis和哨兵是否运行
if [ $(netstat -tlnp|grep redis|wc -l) -ne 2 ]
then
#尝试启动redis和哨兵
systemctl start redis
systemctl start redis-sentinel
sleep 2
if [ $(netstat -tlnp|grep redis|wc -l) -ne 2 ]
then
#启动失败-->关停当前keepalived
systemctl stop keepalived
fi
fi
***
动态代理由nginx+lua+redis实现,java监测模块进行监测
基于openresty平台,暂不做openresty基础介绍,后续开篇博客介绍,
1.nginx.conf引入lua.conf
include lua/lua.conf;
2.lua.conf配置
server {
listen 80;
server_name _;
location / {
proxy_set_header Host $host;
lua_code_cache off;
resolver 8.8.8.8;
set $backend '';
set $errurl '';
rewrite_by_lua_file conf/lua/redis_url.lua;
proxy_pass http://$backend;
}
}
3.redis_url.lua配置
--关闭redis
local function close_redis(red)
if not red then
return
end
local ok, err = red:close()
if not ok then
ngx.say("close redis error : ", err)
end
end
--开始redis
local redis = require("resty.redis")
--创建实例
local red = redis:new()
--设置超时(毫秒)
red:set_timeout(1000)
--建立连接
local ip = "192.168.0.185" --ip
local port = 6379 --端口号
local ok, err = red:connect(ip, port)
--redis密码验证,没有的话,可以不写
local a,b=red:auth("密码") --redis密码
if not ok then
ngx.say("connect to redis error : ", res)
return close_redis(red)
end
local res, err = red
if not res then
ngx.say("failed to authenticate: ", err)
return
end
--调用API获取数据
local resp, err = red:get("自定义key") --自定义key
if not resp then
ngx.say("get msg error : ", err)
return close_redis(red)
end
--得到的数据为空处理
if resp == ngx.null then
resp = '' --比如默认值
end
ngx.var.backend=resp
--最后调用关闭redis
close_redis(red)
4.java检测服务并写入redis,前台可视化添加服务器
后端 https://github.com/zhengbo199317/RouteManager
前端 https://gitee.com/super_bo/routing-control-system-front
暂不公开,
除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog
上一篇: 单点接地 or 多点接地?
下一篇: 几种网赚项目引流的方法
精华推荐