Cluster集群搭建步骤:
Redis集群是Redis提供的一个核心特性,可以将多个Redis实例组成一个分布式数据库,实现数据的自动分片和负载均衡。在Redis集群中,数据会被分成16384个槽位,每个槽位都会被分配给不同的Redis实例,当客户端发送数据请求时,Redis集群会自动将请求路由到相应的Redis实例,从而实现数据的分布式存储和查询。
使用Redis集群可以提高系统的性能和可靠性,同时也可以提供更灵活的扩展方案,满足不同的业务需求。但是需要注意的是,Redis集群需要对数据进行分片,可能会导致一些操作的复杂性和性能问题,需要根据具体业务场景进行评估和选择。
第一步 创建节点
1)通过utils路径下install_server.sh执行文件创建6个不同的redis节点,
端口号分别为:6379、6380、6381、8362、6383、6384
注意事项:
如果搭集群之前设置过关于主从复制或者哨兵去配置文件注释掉关于replicaof指令及相关配置,在Redis集群模式下,多个节点的是水平扩展关系。所以replicaof指令在集群模式下是无效的,会产生replicaof directive not allowed in cluster mode的错误。
- 如果各个节点原本有存储数据,则先将各个节点的数据文件清空
- 如果以前在配置文件中设置过replicaof指令及相关配置,则先将删除(注释)replicaof指令及相关配置,在Redis集群中,主从关系是由集群自动管理的,无法手动设置。因此,使用replicaof命令会导致报replicaof directive not allowed in cluster mode的错误
第二部 修改配置文件
2)修改6个redis节点的配置文件
[root@bogon utils]# cd /etc/redis
[root@bogon redis]# ll
-rw-r--r--. 1 root root 84755 Apr 25 09:11 6379.conf
-rw-r--r--. 1 root root 84821 Apr 25 09:12 6380.conf
-rw-r--r--. 1 root root 84794 Apr 25 09:12 6381.conf
-rw-r--r--. 1 root root 84801 Apr 25 09:12 6382.conf
-rw-r--r--. 1 root root 84717 Apr 25 09:46 6383.conf
-rw-r--r--. 1 root root 84717 Apr 25 09:47 6384.conf
这里是一个Redis集群的配置文件示例,包含6个节点的配置:
# 节点1
port 6379
protected-mode no
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
bind 0.0.0.0
# 节点2
port 6380
protected-mode no
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-node-timeout 15000
bind 0.0.0.0
# 节点3
port 6381
protected-mode no
cluster-enabled yes
cluster-config-file nodes-6381.conf
cluster-node-timeout 15000
bind 0.0.0.0
# 节点4
port 6382
protected-mode no
cluster-enabled yes
cluster-config-file nodes-6382.conf
cluster-node-timeout 15000
bind 0.0.0.0
# 节点5
port 6383
protected-mode no
cluster-enabled yes
cluster-config-file nodes-6383.conf
cluster-node-timeout 15000
bind 0.0.0.0
# 节点6
port 6384
protected-mode no
cluster-enabled yes
cluster-config-file nodes-6384.conf
cluster-node-timeout 15000
bind 0.0.0.0
该配置文件包含以下资源描述:
- 6个节点,端口分别为6379-6384。
- 每个节点开启集群功能,并指定节点的集群配置文件名称为nodes-端口号.conf。
- 设置cluster-node-timeout为15秒,如果节点间超过该时间未收到PING消息则视为下线。
- 绑定0.0.0.0,允许外网访问。
- 每个节点的配置放在单独的配置文件中,并指定文件名为nodes-端口号.conf。
- 执行redis-cli --cluster create命令创建集群时,需要提供所有节点地址,格式为: redis-cli --cluster create node1_host:6379 node2_host:6380 … node6_host:6384
- 该命令会自动执行集群初始化,将这6个节点组建为一个集群。
- 集群将自动选举3个主节点,并在其他节点上设置主从关系,管理数据分片和故障转移。
- 客户端连接任意节点即可实现对整个集群的操作
第三部 启动redis
3)启动六个节点
[root@bogon src]# ./redis-server /etc/redis/6379.conf
[root@bogon src]# ./redis-server /etc/redis/6380.conf
[root@bogon src]# ./redis-server /etc/redis/6381.conf
[root@bogon src]# ./redis-server /etc/redis/6382.conf
[root@bogon src]# ./redis-server /etc/redis/6383.conf
[root@bogon src]# ./redis-server /etc/redis/6384.conf
- 查看各个节点是否启动成功
[root@bogon src]# ps -ef | grep redis
root 3889 1 0 09:56 ? 00:00:03 ./redis-server 0.0.0.0:6379 [cluster]
root 3895 1 0 09:56 ? 00:00:03 ./redis-server 0.0.0.0:6380 [cluster]
root 3901 1 0 09:57 ? 00:00:03 ./redis-server 0.0.0.0:6381 [cluster]
root 3907 1 0 09:57 ? 00:00:02 ./redis-server 0.0.0.0:6382 [cluster]
root 3913 1 0 09:57 ? 00:00:02 ./redis-server 0.0.0.0:6383 [cluster]
root 3919 1 0 09:57 ? 00:00:02 ./redis-server 0.0.0.0:6384 [cluster]
root 4247 2418 0 10:22 pts/0 00:00:00 grep --color=auto redis
第四步 配置集群
4)命令格式: --cluster-replicas 1 表示为每个master创建一个slave节点
注意:这里的IP为每个节点所在机器的真实IP
[root@localhost ~]# redis-cli --cluster create --cluster-replicas 1 192.168.157.128:6379 192.168.157.128:6381 192.168.157.128:6383 192.168.157.128:6380 192.168.157.128:6382 192.168.157.128:6384
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.157.128:6382 to 192.168.157.128:6379
Adding replica 192.168.157.128:6384 to 192.168.157.128:6381
Adding replica 192.168.157.128:6380 to 192.168.157.128:6383
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: de68dc07649bebafdc4d6d337c565c9573c4e76e 192.168.157.128:6379
slots:[0-5460] (5461 slots) master
M: 2324ca9d5ab670d5161a7bb6ff1319e787be91ec 192.168.157.128:6381
slots:[5461-10922] (5462 slots) master
M: f53aaa6993b10dfca37f082dc76384c222cf8e93 192.168.157.128:6383
slots:[10923-16383] (5461 slots) master
S: 3c79a5f3eac455e3bf1bfae1893f4a8230d4664f 192.168.157.128:6380
replicates 2324ca9d5ab670d5161a7bb6ff1319e787be91ec
S: beb288390555cc53216830d5e95484107164ccad 192.168.157.128:6382
replicates f53aaa6993b10dfca37f082dc76384c222cf8e93
S: 5fc4cecad886d622eb16c695646b6022b19a508c 192.168.157.128:6384
replicates de68dc07649bebafdc4d6d337c565c9573c4e76e
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 192.168.157.128:6379)
M: de68dc07649bebafdc4d6d337c565c9573c4e76e 192.168.157.128:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: beb288390555cc53216830d5e95484107164ccad 192.168.157.128:6382
slots: (0 slots) slave
replicates f53aaa6993b10dfca37f082dc76384c222cf8e93
S: 3c79a5f3eac455e3bf1bfae1893f4a8230d4664f 192.168.157.128:6380
slots: (0 slots) slave
replicates 2324ca9d5ab670d5161a7bb6ff1319e787be91ec
S: 5fc4cecad886d622eb16c695646b6022b19a508c 192.168.157.128:6384
slots: (0 slots) slave
replicates de68dc07649bebafdc4d6d337c565c9573c4e76e
M: 2324ca9d5ab670d5161a7bb6ff1319e787be91ec 192.168.157.128:6381
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: f53aaa6993b10dfca37f082dc76384c222cf8e93 192.168.157.128:6383
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost ~]#
- 查看主从关系
命令格式:redis-cli --cluster check 【本台redis自己的IP】:【本台redis自己的端口】
[root@localhost ~]# redis-cli --cluster check 192.168.157.128:6379
192.168.157.128:6379 (de68dc07...) -> 0 keys | 5461 slots | 1 slaves.
192.168.157.128:6381 (2324ca9d...) -> 0 keys | 5462 slots | 1 slaves.
192.168.157.128:6383 (f53aaa69...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.157.128:6379)
M: de68dc07649bebafdc4d6d337c565c9573c4e76e 192.168.157.128:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: beb288390555cc53216830d5e95484107164ccad 192.168.157.128:6382
slots: (0 slots) slave
replicates f53aaa6993b10dfca37f082dc76384c222cf8e93
S: 3c79a5f3eac455e3bf1bfae1893f4a8230d4664f 192.168.157.128:6380
slots: (0 slots) slave
replicates 2324ca9d5ab670d5161a7bb6ff1319e787be91ec
S: 5fc4cecad886d622eb16c695646b6022b19a508c 192.168.157.128:6384
slots: (0 slots) slave
replicates de68dc07649bebafdc4d6d337c565c9573c4e76e
M: 2324ca9d5ab670d5161a7bb6ff1319e787be91ec 192.168.157.128:6381
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: f53aaa6993b10dfca37f082dc76384c222cf8e93 192.168.157.128:6383
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost ~]#
第五步 测试
5)测试集群
测试一
主节点数据写测试
加参数 -c ,防止路由失效
[root@bogon src]# ./redis-cli -p 6381 -c
127.0.0.1:6381> get name
-> Redirected to slot [5798] located at 192.168.109.149:6380
(nil)
192.168.109.149:6380> get name
(nil)
192.168.109.149:6380>
测试一 (可选)
我们可以使用Redis提供的redis-trib.rb测试脚本。该测试脚本可以执行以下操作:
- 创建Redis集群。
- 添加节点或移除节点。
- 分片键slot测试。
- 重新分片slot。
- 节点迁移测试。
使用该脚本测试Redis集群的步骤如下:
- 下载Redis源码,该脚本在src目录下。
- 确保Ruby环境正常,并安装redis gem包。
- 启动6个Redis节点,端口从7000到7005,每个节点启用集群模式并设置节点名 myredis-n。
- 执行脚本,创建集群:
bash
cd src
ruby redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001
127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
- 添加测试用节点:
bash
ruby redis-trib.rb add-node 127.0.0.1:7006 myredis-6
- 移除节点测试
bash
ruby redis-trib.rb del-node 127.0.0.1:7003 myredis-3
- 其他测试:slots,reshard,failover等。具体参数查看redis-trib.rb脚本帮助信息。
- 所有测试完成后,通过shutdown命令停止每个Redis节点。
通过该测试脚本,我们可以对Redis集群在不同场景下的行为进行测试,熟悉槽位(slot)的分配规则,节点添加/移除,故障转移等机制,使我们能够更高效的设计和管理Redis集群。