Redis集群是一种通过将多个Redis节点连接在一起以实现高可用性、数据分片和负载均衡的技术。它允许Redis在不同节点上同时提供服务,提高整体性能和可靠性。根据搭建的方式和集群的特性,Redis集群主要有三种模式:主从复制模式(Master-Slave)、哨兵模式(Sentinel)和Cluster模式。Cluster模式是Redis的一种高级集群模式,它通过数据分片和分布式存储实现了负载均衡和高可用性。在Cluster模式下,Redis将所有的键值对数据分散在多个节点上。每个节点负责一部分数据,称为槽位。通过对数据的分片,Cluster模式可以突破单节点的内存限制,实现更大规模的数据存储。Redis Cluster将数据分为16384个槽位,每个节点负责管理一部分槽位。当客户端向Redis Cluster发送请求时,Cluster会根据键的哈希值将请求路由到相应的节点。具体来说,Redis Cluster使用CRC16算法计算键的哈希值,然后对16384取模,得到槽位编号。
准备6个配置文件,在当前工作目录创建conf目录,将配置文件放置于conf目录下面。配置文件具体内容如下(IP为搭建主机的本机IP):
第一个配置文件redis-01.conf:
bind 172.31.164.149
port 6001
cluster-enabled yes
cluster-config-file nodes-6001.conf
cluster-node-timeout 5000
appendonly yes
masterauth C*x#1a2b # Master 节点密码
requirepass C*x#1a2b # 集群密码
第二个配置文件redis-02.conf:
bind 172.31.164.149
port 6002
cluster-enabled yes
cluster-config-file nodes-6002.conf
cluster-node-timeout 5000
appendonly yes
masterauth C*x#1a2b
requirepass C*x#1a2b
第三个配置文件redis-03.conf:
bind 172.31.164.149
port 6003
cluster-enabled yes
cluster-config-file nodes-6003.conf
cluster-node-timeout 5000
appendonly yes
masterauth C*x#1a2b
requirepass C*x#1a2b
第四个配置文件redis-04.conf:
bind 172.31.164.149
port 6004
cluster-enabled yes
cluster-config-file nodes-6004.conf
cluster-node-timeout 5000
appendonly yes
masterauth C*x#1a2b
requirepass C*x#1a2b
第五个配置文件redis-05.conf:
bind 172.31.164.149
port 6005
cluster-enabled yes
cluster-config-file nodes-6005.conf
cluster-node-timeout 5000
appendonly yes
masterauth C*x#1a2b
requirepass C*x#1a2b
第六个配置文件redis-06.conf:
bind 172.31.164.149
port 6006
cluster-enabled yes
cluster-config-file nodes-6006.conf
cluster-node-timeout 5000
appendonly yes
masterauth C*x#1a2b
requirepass C*x#1a2b
docker run -d --restart always --name redis-01 -v ${PWD}/conf/redis-01.conf:/etc/redis-01.conf --net host redis:5.0.8 redis-server /etc/redis-01.conf
docker run -d --restart always --name redis-02 -v ${PWD}/conf/redis-02.conf:/etc/redis-02.conf --net host redis:5.0.8 redis-server /etc/redis-02.conf
docker run -d --restart always --name redis-03 -v ${PWD}/conf/redis-03.conf:/etc/redis-03.conf --net host redis:5.0.8 redis-server /etc/redis-03.conf
docker run -d --restart always --name redis-04 -v ${PWD}/conf/redis-04.conf:/etc/redis-04.conf --net host redis:5.0.8 redis-server /etc/redis-04.conf
docker run -d --restart always --name redis-05 -v ${PWD}/conf/redis-05.conf:/etc/redis-05.conf --net host redis:5.0.8 redis-server /etc/redis-05.conf
docker run -d --restart always --name redis-06 -v ${PWD}/conf/redis-06.conf:/etc/redis-06.conf --net host redis:5.0.8 redis-server /etc/redis-06.conf
进入其中一个节点:
docker exec -it redis-01 bash
输入以下命令,IP为搭建主机的本地IP:
redis-cli -a 'C*x#1a2b' --cluster create 172.31.164.149:6001 172.31.164.149:6002 172.31.164.149:6003 172.31.164.149:6004 172.31.164.149:6005 172.31.164.149:6006 --cluster-replicas 1
待通信完成后,查看集群信息。显示内容如下则搭建成功:
172.31.164.149:6001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:308
cluster_stats_messages_pong_sent:313
cluster_stats_messages_sent:621
cluster_stats_messages_ping_received:313
cluster_stats_messages_pong_received:308
cluster_stats_messages_received:621
查看集群节点信息:
172.31.164.149:6001> cluster nodes
2c3e75ba9ba690330fe12f644dee245536ae03c8 172.31.164.149:6005@16005 slave a917ef7525101f73e5b72b49ec4158a613c02bbc 0 1702455066301 5 connected
a917ef7525101f73e5b72b49ec4158a613c02bbc 172.31.164.149:6002@16002 master - 0 1702455066502 2 connected 5461-10922
a0238143b17860ae51a80096a0a7418f3f30d964 172.31.164.149:6001@16001 myself,master - 0 1702455066000 1 connected 0-5460
99902aacf4a2d697d000d123d16663ce0fe84428 172.31.164.149:6004@16004 slave a0238143b17860ae51a80096a0a7418f3f30d964 0 1702455067304 4 connected
05c3ff9b8316e22a2dd241642fac37f9a291fb8f 172.31.164.149:6003@16003 master - 0 1702455066101 3 connected 10923-16383
589515d5cc3c9ee9458daf7b719a6e6ee3f3b405 172.31.164.149:6006@16006 slave 05c3ff9b8316e22a2dd241642fac37f9a291fb8f 0 1702455066000 6 connected
1、目前使用容器部署Docker仅能使用host这种网络模式,其他模式暂未实验成功,如果有了解的小伙伴欢迎赐教;
2、使用host网络模式部署集群是因为Redis除了节点暴露端口之外,还有需要开放一个集群总线端口,官网说明:Every Redis Cluster node requires two open TCP connections: a Redis TCP port used to serve clients, e.g., 6379, and second port known as the cluster bus port. By default, the cluster bus port is set by adding 10000 to the data port (e.g., 16379); however, you can override this in the cluster-port configuration;
3、需要注意的是:本文使用一台主机部署6个节点的集群,仅用于开发、测试环境,一般不用于生产环境。如需在多台主机部署集群,请根据实际情况对IP和端口进行规划,但需确保各主机之间能够正常通信。