单机搭建Redis Sentinel和Redis Cluster

一、单机搭建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 <master-redis-name> <master-redis-ip> <master-redis-port> <quorum>
# 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值