启动redis集群
docker run -d \
--name redis-node-1 \ #容器名称
--net host \ #使用宿主机的ip和端口
--privileged=true \ #开启权限 ,也可以不加
-v /data/redis/share/redis-node-1:/data \ #数据卷映射主机
redis:6.0.8 \
--cluster-enabled yes \ #开启集群
--appendonly yes \ #开启持久化
--port 6381
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
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
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
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
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
进入一个redis容器构建集群
redis-cli :进入redis容器内部
--cluster create:构建集群
--cluster-replicas 1: 为每个master(主人)建立一个slave(奴隶)(这里六台机器 3 主 3 从)
redis-cli --cluster create
192.168.17.128:6381
192.168.17.128:6382
192.168.17.128:6383
192.168.17.128:6384
192.168.17.128:6385
192.168.17.128:6386
--cluster-replicas 1
解决集群读写error
[root@VM-4-14-centos ~]# docker exec -it redis-node-1 /bin/bash
root@VM-4-14-centos:/data# redis-cli -p 6381
127.0.0.1:6381> keys *
(empty array)
127.0.0.1:6381> set k1 v1
(error) MOVED 12706 124.222.131.252:6383 # k1 ,v1 存不进去
127.0.0.1:6381> set k3 v3 # k3 ,v3 存进去
OK
127.0.0.1:6381> set k4 v4 # k4 ,v4 存不进去
(error) MOVED 8455 124.222.131.252:6382
这是由于hash槽算法 ,将 key 算一个数值,分配到hash槽里面去 , 但是redsi-node1 所管理的hash‘槽是有范围的,k1,算的hash槽的值不归redsi-node1 管理
解决redis集群读写失效
不能使用单机版的命令,应该使用集群的命令启动reids控制台
[root@VM-4-14-centos ~]# docker exec -it redis-node-1 /bin/bash
root@VM-4-14-centos:/data# redis-cli -p 6381 -c #以集群环境启动
127.0.0.1:6381> FLUSHALL
OK
127.0.0.1:6381> set k1 v1
-> Redirected to slot [12706] located at 124.222.131.252:6383 # Redirected :重定向槽不属于redis-node1 的卡槽
OK
检查集群健康
root@VM-4-14-centos:/data# redis-cli --cluster check 124.222.131.252:6384
124.222.131.252:6382 (69da31a4...) -> 0 keys | 5462 slots | 1 slaves.
124.222.131.252:6383 (69cbe56e...) -> 1 keys | 5461 slots | 1 slaves.
124.222.131.252:6381 (2ce205ba...) -> 0 keys | 5461 slots | 1 slaves.
容错数据迁移
- 停止主机6381
[root@VM-4-14-centos ~]# docker exec -it redis-node-2 /bin/bash
root@VM-4-14-centos:/data# redis-cli -p 6382 -c
127.0.0.1:6382> cluster nodes
0b0c2bade9189cb1bfb072ac4ee6f83c908f6c50 124.222.131.252:6384@16384 slave 69da31a44b15e03baf4fdba9094d7ff367ffdf7c 0 1659845356786 2 connected
69da31a44b15e03baf4fdba9094d7ff367ffdf7c 10.0.4.14:6382@16382 myself,master - 0 1659845356000 2 connected 5461-10922
882a32691859a8dbd7c4417f25c6c2cd1b71a473 124.222.131.252:6386@16386 master - 0 1659845357000 7 connected 0-5460
2ce205ba36565df8bde6d43f09f922c20533411c 124.222.131.252:6381@16381 master,fail - 1659845306117 1659845298606 1 disconnected
69cbe56e493caea7989c8d2932adb000f496d931 124.222.131.252:6383@16383 master - 0 1659845355000 3 connected 10923-16383
3cf2f881fc78f048f2156544f91161fa86c6f5e0 124.222.131.252:6385@16385 slave 69cbe56e493caea7989c8d2932adb000f496d931 0 1659845357792 3 connected
127.0.0.1:6382> get k1
-> Redirected to slot [12706] located at 124.222.131.252:6383
"v1"
#即使停止redisnode1 他的数据还在
redis路由增强
集群健康检查
redis-cli --cluster check ip+: + 端口号
cluster nodes
集群扩容
- 将新节点加入不会分配节点和槽位
步骤: (diocker)上线新节点和奴隶-》-》 集群上线新节点-》分配槽位-》绑定他的奴隶(奴隶自动加入集群了)
启动两台机器 ,一主一丛
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
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
将新节点加入
124.222.131.252:6387:新redis ip+端口号
124.222.131.252:6386:master节点的端口号
redis-cli --cluster add-node 124.222.131.252:6387 124.222.131.252:6386
重新分配槽位
root@VM-4-14-centos:/data# redis-cli --cluster reshard 124.222.131.252:6381
How many slots do you want to move (from 1 to 16384)? 4096
# 16384/4 = 4096 四主分配16384的槽片
# M: cd354b904908291b671a1b2657ea535ca927c23f 124.222.131.252:6387
# slots: (0 slots) master
# ID:是新加入节点的id
What is the receiving node ID? cd354b904908291b671a1b2657ea535ca927c23f
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1: all
*** The specified node (all ) is not known or not a master, please retry.
res
M: cd354b904908291b671a1b2657ea535ca927c23f 124.222.131.252:6387
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
每个人均匀分配了一点槽片给新节点
# 124.222.131.252:6388 -cluster-slave 奴隶 124.222.131.252:6387 cluster-master主人
#cd354b904908291b671a1b2657ea535ca927c23f 主人的id
redis-cli --cluster add-node 124.222.131.252:6388 124.222.131.252:6387 --cluster-slave --cluster-master-id cd354b904908291b671a1b2657ea535ca927c23f
集群缩容
步骤: 下线奴隶节点 -》 释放他主人的槽片给别的主人-》删除他
下线新增的奴隶节点
redis-cli --cluster del-node 124.222.131.252:6388 a6300d653f0212b3f44bfc9828fa14157db5acd6
How many slots do you want to move (from 1 to 16384)? 4096
# 69cbe56e493caea7989c8d2932adb000f496d931:接受槽位
What is the receiving node ID? 69cbe56e493caea7989c8d2932adb000f496d931
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
# 哪个节点释放槽位(6387,6387有4096个槽位)
Source node #1: cd354b904908291b671a1b2657ea535ca927c23f
Source node #2: done
删除6387
redis-cli --cluster del-node ip:端口号 id
redis-cli --cluster del-node 124.222.131.252:6387 cd354b904908291b671a1b2657ea535ca927c23f
如果有写的不详细的地方欢迎告诉我