BlueXIII's Blog

热爱技术,持续学习

0%

Redis集群搭建

参考文档

redis.conf配置

创建6个目录,将/etc/redis.conf拷贝至其中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 端口号,每个目录都不同
port 7000
# 开启集群模式
cluster-enabled yes
#节点超时实际,单位毫秒
cluster-node-timeout 5000
#集群内部配置文件,绝对路径
cluster-config-file ~/opt/redis-cluster/7000/nodes.conf
# 启动 AOF
appendonly yes
# 密码
requirepass 123456
# 主从复制密码(只在3个从节点上配置)
masterauth 123456

简易操作步骤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 启动6个redis节点
nohup redis-server ~/opt/redis-cluster/7000/redis.conf > 7000.log 2>&1 &
nohup redis-server ~/opt/redis-cluster/7001/redis.conf > 7001.log 2>&1 &
nohup redis-server ~/opt/redis-cluster/7002/redis.conf > 7002.log 2>&1 &
nohup redis-server ~/opt/redis-cluster/7003/redis.conf > 7003.log 2>&1 &
nohup redis-server ~/opt/redis-cluster/7004/redis.conf > 7004.log 2>&1 &
nohup redis-server ~/opt/redis-cluster/7005/redis.conf > 7005.log 2>&1 &

# 登录7000,关联其它节点
redis-cli -a 123456 -p 7000
> cluster meet 127.0.0.1 7001
> cluster meet 127.0.0.1 7002
> cluster meet 127.0.0.1 7003
> cluster meet 127.0.0.1 7004
> cluster meet 127.0.0.1 7005

# 查看集群状态,获取节点ID
redis-cli -a 123456 -p 7000 cluster nodes

# 配置主从复制
redis-cli -a 123456 -p 7003 cluster replicate 21509e5b757d8bae0678efcdb525f59def83674e # 7000的ID
redis-cli -a 123456 -p 7004 cluster replicate ebb502cda79909b50350be31c3ffe96e9177bafd # 7001的ID
redis-cli -a 123456 -p 7005 cluster replicate 2aa5a4c37c92f86fdae681dd8c361fee3a99a7af # 7002的ID

# 检查并随机分配slots
redis-cli -a 123456 --cluster check 127.0.0.1:7000
redis-cli -a 123456 --cluster fix 127.0.0.1:7000

配置文件示例

https://bluexiii-files.oss-cn-beijing.aliyuncs.com/redis.conf