单机搭建Redis Sentinel和Redis Cluster
创始人
2024-06-02 08:32:43
0

单机搭建Redis Sentinel和Redis Cluster

  • 一、单机搭建Redis Sentinel
    • 1.1 基于sentinel.conf复制sentinel-26379.conf sentinel-26379.conf sentinel-26379.conf
    • 1.2 修改文件中端口(以sentinel-26379.conf为示例) 主要配置如下
    • 1.3 用不同的配置文件启动redis seninel
    • 1.4 验证redis sentinel
  • 二、单机搭建Redis Cluster
    • 2.1 基于redis.conf 复制文件redis-8001.conf redis-8002.conf redis-8003.conf redis-8004.conf redis-8005.conf redis-8006.conf
    • 2.2 编写redis cluser配置文件
    • 2.3 启动
    • 2.4 验证
    • 2.5 关闭集群
  • 三、出现的问题
    • 3.1 哨兵启动之后,连接查看状态发现 master_link_status:down。
    • 3.2 Could not create Server TCP listening socket *:6379: bind: Address already in use
    • 3.3 取消了第一步的操作,那么master_link_status:down 问题就依然存在,继续从启动日志出发。
    • 3.4 No such master with specified name

一、单机搭建Redis Sentinel

1.1 基于sentinel.conf复制sentinel-26379.conf sentinel-26379.conf sentinel-26379.conf

cp sentinel.conf sentinel-26379.conf
cp sentinel.conf sentinel-26380.conf
cp sentinel.conf sentinel-26381.conf

如下图:
在这里插入图片描述

1.2 修改文件中端口(以sentinel-26379.conf为示例) 主要配置如下

port 26379
daemonize yes
protected-mode no
pidfile "/var/run/redis-sentinel-26379.pid"
logfile "26379.log"
dir "/usr/local/redis-5.0.3/data"
# sentinel monitor    
# quorum是一个数字,指明当有多少个sentinel认为一个master失效时(值一般为:sentinel总数/2 + 1),master才算真正失效
sentinel monitor mymaster 192.168.80.139 6379 2   # mymaster这个名字随便取,客户端访问时会用到

1.3 用不同的配置文件启动redis seninel

## 进入到redis安装目录
## 加 & 代表后台启动
src/redis-sentinel sentinel-26379.conf &
src/redis-sentinel sentinel-26380.conf &
src/redis-sentinel sentinel-26381.conf &

1.4 验证redis sentinel

(1)看进程

ps -ef | grep redis-sentinel

在这里插入图片描述

(2)客户端连接查看状态

src/redis-cli -p 26379

在这里插入图片描述在这里插入图片描述

二、单机搭建Redis Cluster

2.1 基于redis.conf 复制文件redis-8001.conf redis-8002.conf redis-8003.conf redis-8004.conf redis-8005.conf redis-8006.conf

因为cluster至少要三个master节点,每个节点最少一个slave节点。所以最少需要6分配置文件。

cp redis.conf redis-8001.conf
cp redis.conf redis-8002.conf
cp redis.conf redis-8003.conf
cp redis.conf redis-8004.conf
cp redis.conf redis-8005.conf
cp redis.conf redis-8006.conf

2.2 编写redis cluser配置文件

daemonize yes
port 8001(分别对每个机器的端口号进行设置)
pidfile /var/run/redis_8001.pid  # 把pid进程号写入pidfile配置的文件
dir /usr/local/redis-cluster/8001/(指定数据文件存放位置,必须要指定不同的目录位置,不然会丢失数据)
cluster-enabled yes(启动集群模式)
cluster-config-file redis-8001.conf(集群节点信息文件,这里800x最好和port对应上)
cluster-node-timeout 10000
# bind 127.0.0.1(bind绑定的是自己机器网卡的ip,如果有多块网卡可以配多个ip,代表允许客户端通过机器的哪些网卡ip去访问,内网一般可以不配置bind,注释掉即可)
protected-mode  no   (关闭保护模式)
appendonly yes
#如果要设置密码需要增加如下配置:
requirepass redis(设置redis访问密码)
masterauth redis(设置集群节点间访问密码,跟上面一致)

2.3 启动

## & 代表后台启动
src/redis-server redis-8001.conf &
src/redis-server redis-8002.conf &
src/redis-server redis-8003.conf &
src/redis-server redis-8004.conf &
src/redis-server redis-8005.conf &
src/redis-server redis-8006.conf &

检查这6个端口都已经启动

/usr/local/redis-5.0.3/src/redis-cli -a redis--cluster create --cluster-replicas 1 192.168.80.139:8001 192.168.80.139 :8002 192.168.80.139:8003 192.168.80.139:8004 192.168.80.139:8005 192.168.80.139:8006

2.4 验证

## 连接任意一个客户端即可:
/usr/local/redis-5.0.3/src/redis-cli -a redis-c -h 192.168.80.139-p 8001## 查看集群信息
cluster info## 查看节点列表
cluster nodes

2.5 关闭集群

sr/local/redis-5.0.3/src/redis-cli -a redis-c -h 192.168.80.139-p 800* shutdown

三、出现的问题

3.1 哨兵启动之后,连接查看状态发现 master_link_status:down。

通过网络搜索解决方案得出:出现该问题是因为主节点设置了密码,可以通过继承redis.conf的配置来解决,所以要修改sentinel的配置如下

## 增加这一行
include /usr/local/redis-5.0.3/redis.conf

按照上面的修改出现了第二个问题

3.2 Could not create Server TCP listening socket *:6379: bind: Address already in use

通过查看26379、26380、26381的日志可以发现,在启动过程中都要绑定 6379端口,但是实际上这是不应该的,因为哨兵单独指定了端口,出现了这个问题的原因就是因为第一步操作中继承了 redis.conf 所以会绑定6379,知道了问题那就好解决了。
解决方案:取消第一步的操作

3.3 取消了第一步的操作,那么master_link_status:down 问题就依然存在,继续从启动日志出发。

发现哨兵的日志中有这个提示
+sdown master mymaster 192.168.x.x
这个错误的原因是:redis-sentinel和redis master无法通信,所以会判断master sdown.这时想起来,redis.conf中是设置了访问密码,哨兵的配置文件未设置访问master节点时的密码。所以在哨兵的配置文件增加如下配置

## 哨兵在和master节点通信时 使用配置的密码连接sentinel auth-pass mymaster xxxx

增加了密码配置后,又进行重启这时又出现了问题,也就是问题4

3.4 No such master with specified name

出现这个问题是因为配置的顺序有问题

下面这个配置声明了主节点的ip端口和名称为 mymaster,如果在这个声明之前使用了 mymater代指主节点就会出现这个错。

sentinel monitor mymaster 192.168.80.139 6381 2

在这里插入图片描述
知道原因后,检查配置,果然发现顺序不对
在这里插入图片描述
修改 将密码配置放在sentinel monitor …之后重启,发现问题解决。
连接redis-sentinel查看状态

## 连接
src/redis-sentinel conf/sentinel-26379.conf
## 查看状态
info

在这里插入图片描述

总结:到此整个哨兵模式的搭建就成功了,过程中出现了很多低级错误,但是作为程序员抱着学习的心态出现问题并且解决了就是学到了。所以把出现的问题记录一下,希望能帮助同样会出现问题的你。

相关内容

热门资讯

山东移动泰安分公司筑牢反诈防线... 转自:新华财经在电信网络诈骗日益猖獗的当下,利用“猫池”(GOIP)设备实施诈骗的手段严重威胁人民群...
有色金属ETF(512400)... 5月14日,南方中证申万有色金属ETF(512400)收盘涨0.75%,成交额9079.29万元。有...
军工ETF易方达(512560... 5月14日,易方达中证军工ETF(512560)收盘跌0.46%,成交额4596.39万元。军工ET...
调研速递|[公司名称未提及]接...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! 近日,[公司名称未提...
粤宏远A涨2.80%,成交额1... 5月14日,粤宏远A涨2.80%,成交额1.96亿元,换手率7.07%,总市值28.08亿元。异动分...
高盛:将欧洲斯托克600指数未...   高盛将欧洲斯托克600指数(STOXX 600 Index)未来12个月的目标从520点上调至5...
创业板ETF南方(159948... 5月14日,南方创业板ETF(159948)收盘涨1.02%,成交额4780.91万元。创业板ETF...
“阳光普照 爱满人间”公益项... 近日,“阳光普照 爱满人间”——关爱老人儿童公益项目在青浦区多个社区及养老机构开展系列活动,通过健...
康欣新材涨4.61%,成交额1... 5月14日,康欣新材涨4.61%,成交额1.53亿元,换手率5.03%,总市值30.52亿元。异动分...
1.2万余名群众加入这个队伍,... 转自:中国长安网4月30日,在四川省内江市资中县“护航甜城2025”防汛救灾暨应急救援综合实战演练现...
巫溪红池坝镇:“苦药材”铺就“... 中新网重庆新闻5月14日电 近日,在巫溪红池坝镇渔沙村300亩黄连种植基地里,药农们正趁着好时节除草...
挂“洋”头   “新西兰羊奶初乳高钙粉”,实际上产于江西宜春;“澳洲品牌蓝胖子牛奶粉”罐体包装全是英文,实则在安...
中企助力伊拉克打造“智慧油田” 01:34在位于伊拉克首都巴格达的振华石油控股有限公司(以下简称“振华石油”)东巴油田中心处理站内,...
秘望男士控油爽肤沐浴露成为假期... 转自:衡水日报 如今,越来越多的人选择一种更为惬意的度假方式。他们会前往城郊,住进一家小而干净的民宿...
商南县检察院:“四进四讲” 让... 连日来,商洛市商南县检察院以第五个“民法典宣传月”为契机,紧扣“美好生活·民法典相伴”主题,创新开展...
吉木萨尔县启动水电气行业价格与... 近日,为进一步优化营商环境,切实减轻企业和群众负担,提升人民群众满意度,吉木萨尔县市场监督管理局积极...
警惕强对流!今天下午河南多地阵... 18日起高温范围扩大 热力加码昨天中西部、西北部部分地区出现了35℃以上的高温天气,林州达到40.2...
全国助残日“京鹰”来了 北京市... 5月14日上午,全市首支以残疾人为主体的无人机操控员团队——“京鹰飞手队”正式亮相。这支由30名残疾...
业绩分化加剧 头部效应凸显 2025年第一季度,各大上市车企的业绩呈现明显分化,汽车行业内部洗牌加剧,头部车企与尾部阵营的业绩差...
国家铁路局局长费东斌会见哥伦比... 人民财讯5月14日电,2025年5月13日,国家铁路局局长费东斌在北京会见了哥伦比亚交通部长罗哈斯。...