单机搭建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

在这里插入图片描述

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

相关内容

热门资讯

激发暑期文旅消费新动能   内蒙古阿尔山,漫步国家森林公园,感受清凉夏日;甘肃敦煌,坐在鸣沙山上,听一场星空演唱会;江苏连云...
日股收跌0.19% 格隆汇7月11日|日经225指数收盘下跌76.68点,跌幅0.19%,报39569.68点。
遵义小伙跳水救人牺牲,年仅31... (转自:遵义晚报)7月8日下午6时,遵义市播州区出现感人一幕——尚嵇镇大坝社区全军坝组28岁的村民陈...
“小记者”走进河北省邯郸市食品... 中国质量新闻网讯 7月7日下午,河北省邯郸市食品药品检验中心(市食品药品安全科普基地)联合邯郸新闻传...
局地超42℃!下周河南持续高温... 大范围高温闷热天气强势来袭!12日起,西太平洋副热带高压将再次控制我省,受其影响,13日至20日我省...
毕市监处罚[2025]1021... 毕市监处罚[2025]1021号浏览:1次基本信息:处罚机关毕节市市场监督管理局处罚日期2025年0...
达势股份-达美乐中国以4D战略...   达势股份-达美乐中国("达美乐中国"或"达势股份"或"公司")(1405.HK)是达美乐比萨在中...
“向上取整”:变相“明抢” 转自:中国质量报□ 胡立彪近日,有媒体对快递行业称重计费问题进行测评,结果发现,被测评的8家快递企业...
以改革破局 盐城经开区多点突破... 中新网江苏新闻7月11日电(倪玲)改革是发展的根本动力,开放是进步的必由之路。近年来,盐城经开区将推...
中国代表强调国际刑事法院应依法... 转自:千龙网新华社联合国7月10日电 中国常驻联合国副代表孙磊10日在安理会审议国际刑事法院涉苏丹问...