Docker搭建Redis集群

2023-12-13 03:46:48

目录

Docker搭建redis集群

?在集群状态下存储数据

?redis集群容错切换迁移

redis集群扩容

redis集群缩容


Docker搭建redis集群

启动6台redis实例

[root@localhost ~]# docker run -d --name redis-node-1 --net host --privileged=true -v /data/redis/share/redis-node-1:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6381
5d6908c5b6119abfe6cace98ff52aeb45359fee61195d3bb297ad2072422789e
[root@localhost ~]# docker run -d --name redis-node-2 --net host --privileged=true -v /data/redis/share/redis-node-2:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6382
5de1719273220b6230013edd135ee1ba261b0ff48f6acb8a3fc6ba62f781ec4d
[root@localhost ~]# docker run -d --name redis-node-3 --net host --privileged=true -v /data/redis/share/redis-node-3:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6383
2918ceb7a3ce617be4995b7906f4adadea89e10984a2a67dfd2e24c270b8d680
[root@localhost ~]# docker run -d --name redis-node-4 --net host --privileged=true -v /data/redis/share/redis-node-4:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6384
cc58a3771160d075a22f75dc86579e6558108a12047c5bea7070fbda0da8b1ea
[root@localhost ~]# docker run -d --name redis-node-5 --net host --privileged=true -v /data/redis/share/redis-node-5:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6385
30fb3c96db18bb7439dbb5b674af3b7978c2680739c30f3a1ff0f28a4a89a8bb
[root@localhost ~]# docker run -d --name redis-node-6 --net host --privileged=true -v /data/redis/share/redis-node-6:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6386
0f8e3380a01f1d78c43b8a308617deb4e60de7bd7d3a878ca3e9c1f365a37d1f

查看容器是否启动

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                                      COMMAND                   CREATED              STATUS                           PORTS                                                                                  NAMES
0f8e3380a01f   redis:6.0.8                                "docker-entrypoint.s…"   About a minute ago   Up About a minute                                                                                                       redis-node-6
30fb3c96db18   redis:6.0.8                                "docker-entrypoint.s…"   About a minute ago   Up About a minute                                                                                                       redis-node-5
cc58a3771160   redis:6.0.8                                "docker-entrypoint.s…"   About a minute ago   Up About a minute                                                                                                       redis-node-4
2918ceb7a3ce   redis:6.0.8                                "docker-entrypoint.s…"   2 minutes ago        Up 2 minutes                                                                                                            redis-node-3
5de171927322   redis:6.0.8                                "docker-entrypoint.s…"   3 minutes ago        Up 2 minutes                                                                                                            redis-node-2
5d6908c5b611   redis:6.0.8                                "docker-entrypoint.s…"   3 minutes ago        Up 3 minutes         

进入容器redis-node-1

[root@localhost ~]# docker exec -it redis-node-1 /bin/bash
root@localhost:/data# 

构建主从关系

root@localhost:/data# redis-cli --cluster create 192.168.117.131:6381 192.168.117.131:6382 192.168.117.131:6383 192.168.117.131:6384 192.168.117.131:6385 192.168.117.131:6386 --cluster-replicas 1
>>> 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.117.131:6385 to 192.168.117.131:6381
Adding replica 192.168.117.131:6386 to 192.168.117.131:6382
Adding replica 192.168.117.131:6384 to 192.168.117.131:6383
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 51539e2f5dfef307cc396a7768bf662757b20cae 192.168.117.131:6381
   slots:[0-5460] (5461 slots) master
M: 8db79b2ccf0047e55462b46e099cdc9f27f4403d 192.168.117.131:6382
   slots:[5461-10922] (5462 slots) master
M: fffca402ea55ee311f83d4672f946ae36b607e50 192.168.117.131:6383
   slots:[10923-16383] (5461 slots) master
S: 44dd1dde14aade509a9cecff4a01bcaf77ab97ae 192.168.117.131:6384
   replicates 51539e2f5dfef307cc396a7768bf662757b20cae
S: 180941bcc67c057fca840122e97b2774a1303879 192.168.117.131:6385
   replicates 8db79b2ccf0047e55462b46e099cdc9f27f4403d
S: d5b4bf12e60f2cf68432af77d1c3f0c74030b6f3 192.168.117.131:6386
   replicates fffca402ea55ee311f83d4672f946ae36b607e50
Can I set the above configuration? (type 'yes' to accept): 

输入yes

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.117.131:6381)
M: 51539e2f5dfef307cc396a7768bf662757b20cae 192.168.117.131:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 180941bcc67c057fca840122e97b2774a1303879 192.168.117.131:6385
   slots: (0 slots) slave
   replicates 8db79b2ccf0047e55462b46e099cdc9f27f4403d
M: fffca402ea55ee311f83d4672f946ae36b607e50 192.168.117.131:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 44dd1dde14aade509a9cecff4a01bcaf77ab97ae 192.168.117.131:6384
   slots: (0 slots) slave
   replicates 51539e2f5dfef307cc396a7768bf662757b20cae
S: d5b4bf12e60f2cf68432af77d1c3f0c74030b6f3 192.168.117.131:6386
   slots: (0 slots) slave
   replicates fffca402ea55ee311f83d4672f946ae36b607e50
M: 8db79b2ccf0047e55462b46e099cdc9f27f4403d 192.168.117.131:6382
   slots:[5461-10922] (5462 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:/data# 

这样主从关系构建成功

查看集群状态

随便连接一台redis服务器

root@localhost:/data# redis-cli -p 6381
127.0.0.1:6381> 

查看集群状态

也可以使用cluster nodes查看集群节点状态

127.0.0.1:6381> cluster nodes
180941bcc67c057fca840122e97b2774a1303879 192.168.117.131:6385@16385 slave 8db79b2ccf0047e55462b46e099cdc9f27f4403d 0 1702055686000 2 connected
51539e2f5dfef307cc396a7768bf662757b20cae 192.168.117.131:6381@16381 myself,master - 0 1702055684000 1 connected 0-5460
fffca402ea55ee311f83d4672f946ae36b607e50 192.168.117.131:6383@16383 master - 0 1702055685000 3 connected 10923-16383
44dd1dde14aade509a9cecff4a01bcaf77ab97ae 192.168.117.131:6384@16384 slave 51539e2f5dfef307cc396a7768bf662757b20cae 0 1702055686956 1 connected
d5b4bf12e60f2cf68432af77d1c3f0c74030b6f3 192.168.117.131:6386@16386 slave fffca402ea55ee311f83d4672f946ae36b607e50 0 1702055685000 3 connected
8db79b2ccf0047e55462b46e099cdc9f27f4403d 192.168.117.131:6382@16382 master - 0 1702055683931 2 connected 5461-10922

?在集群状态下存储数据

127.0.0.1:6381> set k1 v1
(error) MOVED 12706 192.168.117.131:6383

因为我们在6381上进行数据存储,但是落在的哈希槽位于6383,超过了目前6381哈希槽,所以存储失败。

可以在连接redis的时候加上-c参数

127.0.0.1:6381> exit
root@localhost:/data# redis-cli -p 6381 -c
127.0.0.1:6381> 

存储数据成功?

127.0.0.1:6381> set k1 v1
-> Redirected to slot [12706] located at 192.168.117.131:6383
OK

可以通过如下命令来查看集群节点的状态

redis-cli --cluster check 192.168.117.131:6381

?redis集群容错切换迁移

查看运行的redis容器

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                                      COMMAND                   CREATED          STATUS                            PORTS                                                                                  NAMES
0f8e3380a01f   redis:6.0.8                                "docker-entrypoint.s…"   26 minutes ago   Up 26 minutes                                                                                                            redis-node-6
30fb3c96db18   redis:6.0.8                                "docker-entrypoint.s…"   27 minutes ago   Up 27 minutes                                                                                                            redis-node-5
cc58a3771160   redis:6.0.8                                "docker-entrypoint.s…"   27 minutes ago   Up 27 minutes                                                                                                            redis-node-4
2918ceb7a3ce   redis:6.0.8                                "docker-entrypoint.s…"   28 minutes ago   Up 28 minutes                                                                                                            redis-node-3
5de171927322   redis:6.0.8                                "docker-entrypoint.s…"   28 minutes ago   Up 28 minutes                                                                                                            redis-node-2
5d6908c5b611   redis:6.0.8                                "docker-entrypoint.s…"   29 minutes ago   Up 29 minutes                                                                                                            redis-node-1

停掉redis-node-1

[root@localhost ~]# docker stop redis-node-1
redis-node-1

再次查看运行的redis容器

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                                      COMMAND                   CREATED          STATUS                            PORTS                                                                                  NAMES
0f8e3380a01f   redis:6.0.8                                "docker-entrypoint.s…"   28 minutes ago   Up 28 minutes                                                                                                            redis-node-6
30fb3c96db18   redis:6.0.8                                "docker-entrypoint.s…"   28 minutes ago   Up 28 minutes                                                                                                            redis-node-5
cc58a3771160   redis:6.0.8                                "docker-entrypoint.s…"   28 minutes ago   Up 28 minutes                                                                                                            redis-node-4
2918ceb7a3ce   redis:6.0.8                                "docker-entrypoint.s…"   29 minutes ago   Up 29 minutes                                                                                                            redis-node-3
5de171927322   redis:6.0.8                                "docker-entrypoint.s…"   29 minutes ago   Up 29 minutes                                                                                                            redis-node-2

进入到2号redis

?6384以前是从机,现在变成了主机,完成了集群故障迁移

在另一个xshell窗口启动redis6381

?查看集群节点状态

6381现在变成了从机,原来的主机状态依然没有发生变化?

redis集群扩容

将redis三主三从扩容到四主四从

新建6387,6388两个节点

[root@localhost ~]# docker run -d --name redis-node-7 --net host --privileged=true -v /data/redis/share/redis-node-7:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6387
1a64a6c08cb25b2ae05af721dd04c79883d339345833163c7e3085452a7af3fc
[root@localhost ~]# docker run -d --name redis-node-8 --net host --privileged=true -v /data/redis/share/redis-node-8:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6388
0ea9e8cec7f42d1c37b160a10b358912341f341abea51662f70b1915a545b7cd

查看容器状态

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                                      COMMAND                   CREATED          STATUS                            PORTS                                                                                  NAMES
0ea9e8cec7f4   redis:6.0.8                                "docker-entrypoint.s…"   2 minutes ago    Up 2 minutes                                                                                                             redis-node-8
1a64a6c08cb2   redis:6.0.8                                "docker-entrypoint.s…"   3 minutes ago    Up 3 minutes                                                                                                             redis-node-7
0f8e3380a01f   redis:6.0.8                                "docker-entrypoint.s…"   56 minutes ago   Up 4 seconds                                                                                                             redis-node-6
30fb3c96db18   redis:6.0.8                                "docker-entrypoint.s…"   56 minutes ago   Up 6 seconds                                                                                                             redis-node-5
cc58a3771160   redis:6.0.8                                "docker-entrypoint.s…"   57 minutes ago   Up 7 seconds                                                                                                             redis-node-4
2918ceb7a3ce   redis:6.0.8                                "docker-entrypoint.s…"   57 minutes ago   Up 9 seconds                                                                                                             redis-node-3
5de171927322   redis:6.0.8                                "docker-entrypoint.s…"   58 minutes ago   Up 11 seconds                                                                                                            redis-node-2
5d6908c5b611   redis:6.0.8                                "docker-entrypoint.s…"   59 minutes ago   Up 13 seconds                                                                                                            redis-node-1

进入6387容器实例

[root@localhost ~]# docker exec -it redis-node-7 /bin/bash
root@localhost:/data# redis-cli --cluster add-node 192.168.117.131:6387 192.168.117.131:6381

6387就是将要新增的节点

6381是集群中的领路人

?查看集群状态

root@localhost:/data# redis-cli --cluster check 192.168.117.131:6381

发现6387这台机器暂时没有槽位,也没有从机?

重新分配槽号

命令如下

redis-cli --cluster reshard IP地址:端口号
root@localhost:/data# redis-cli --cluster reshard 192.168.117.131:6381

之后输入yes

查看集群状态

[root@localhost ~]# redis-cli --cluster check 192.168.117.131:6381

发现6387号机器以经分配了哈希槽

?为主节点6387分配从节点6388

命令如下?

redis-cli --cluster add-node ip:新slave端口 ip:新master端口 --cluster-slave --cluster-master-id 新主机节点ID
redis-cli --cluster add-node 192.168.117.131:6388 192.168.117.131:6387 --cluster-slave --cluster-master-id aab8b312971b569d5e39317cbce03d6256d3d536

查看集群节点

[root@localhost ~]# redis-cli --cluster check 192.168.117.131:6381

至此redis集群扩容完成?

redis集群缩容

使得6387和6388下线

查看集群状态

[root@localhost ~]# docker exec -it redis-node-1 /bin/bash

?删除从节点6388

命令如下

redis-cli --cluster del-node ip:从机端口 从机6388节点ID
root@localhost:/data# redis-cli --cluster del-node 192.168.117.131:6388 474a1ba3f2dfbf48a020ffc3d489b2f3e0b79633

查看集群节点状态

root@localhost:/data# redis-cli --cluster del-node 192.168.117.131:6388 474a1ba3f2dfbf48a020ffc3d489b2f3e0b79633

?发现删除成功,从节点由原来的4台变成了现在的3

将6387的槽号清空,重新分配给6382

root@localhost:/data# redis-cli --cluster reshard 192.168.117.131:6381

输入4096,6382的id,6387的id,done,yes

再次查看集群节点状态

root@localhost:/data# redis-cli --cluster check 192.168.117.131:6381

发现6387的哈希槽全部分配给6382了?

删除6387

命令如下?

redis-cli --cluster del-node ip:端口 6387节点ID
root@localhost:/data# redis-cli --cluster del-node 192.168.117.131:6387 aab8b312971b569d5e39317cbce03d6256d3d536

?查看节点信息

root@localhost:/data# redis-cli --cluster check 192.168.117.131:6381

至此redis集群缩容完成

文章来源:https://blog.csdn.net/m0_72832574/article/details/134889748
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。