docker-compose部署redis cluster集群及常用集群命令

目录

    • 一、编写redis.conf
    • 二、编写docker-compose.yaml文件
    • 三、生成cluster集群,并自动分配slot
    • 四、手动切换主从
      • 方法1:
    • 五、手动设置集群、分配槽位
      • 1、cluster meet ip port 加入集群
      • 2、cluster replicate 设置主从关系
      • 3、cluster addslots 分配槽位
      • 4、增加master节点
        • 4.1、增加新节点
        • 4.2、给新节点分配散列槽
      • 5、增加slave节点
      • 6、删除slave节点
      • 7、删除master节点
        • 7.1、数据迁移到其他主节点中 (逻辑和添加节点分配散列槽一样)
        • 7.2、删除节点:
      • 六、其他命令
        • 1、查询槽位分配情况:
        • 2、查询某个key的槽位:
        • 3、查询槽 slot 包含的键值对数量,需要去slot所在的容器里查询:
        • 4、移除一个或多个槽对当前节点的指派:
        • 5、将一个或多个槽( slot)指派( assign)给当前节点:
        • 6、移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
        • 7、将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么会自动让另一个节点删除该槽,然后再指派到指定节点。
        • 8、获取count个属于槽slot的键

一、编写redis.conf

[root@app01 redis-cluster]# cat redis.conf
appendonly yes
cluster-enabled yes
masterauth 1111
requirepass 1111
bind 0.0.0.0
port 6379
io-threads-do-reads yes
io-threads 3
maxmemory 6G
cluster-config-file nodes.conf
#cluster-announce-ip 192.168.180.45 #可以写在redis.conf,可以写在docker-compose.yaml
[root@app01 redis-cluster]#

1、cluster-enabled yes 该配置表示启用群集支持:开启后,将Redis服务实例作为cluster节点启动
普通Redis服务实例不能是Redis Cluster的一部分,只有作为Redis Cluster节点启动的服务实例才可以。

2、cluster-config-file nodes.conf 该配置用于指定集群配置文件名:每个Redis Cluster节点都有一个集群配置文件,此文件不可手动编辑。它由Redis节点创建和更新。每个Redis Cluster节点都需要不同的集群配置文件。确保在同一系统中运行的实例没有重叠的集群配置文件名。

3、cluster-node-timeout 15000 该配置单位为毫秒,超过 cluster-node-timeout 无法访问才能被视为处于故障状态,大多数其他内部时间限制是该节点超时的倍数。

二、编写docker-compose.yaml文件

[root@app01 redis-cluster]# cat docker-compose.yaml
version: '3.2'
services:
  redis-node-master-1:
    image: redis:7.0
    container_name: cluster-redis-1
    ports:
      - 6381:6379
      - 16381:16379
    command:
      - "redis-server"
      - "/etc/redis/redis.conf"
      - "--protected-mode"
      - "no"
      - "--cluster-announce-ip 192.168.180.45"
      - "--cluster-announce-port"
      - "6381"
      - "--cluster-announce-bus-port"
      - "16381"
    volumes:
      - ./redis.conf:/etc/redis/redis.conf
      - ./redis1:/data
      - /etc/localtime:/etc/localtime
  redis-node-master-2:
    image: redis:7.0
    container_name: cluster-redis-2
    ports:
      - 6382:6379
      - 16382:16379
    command:
      - "redis-server"
      - "/etc/redis/redis.conf"
      - "--protected-mode"
      - "no"
      - "--cluster-announce-ip 192.168.180.45"
      - "--cluster-announce-port"
      - "6382"
      - "--cluster-announce-bus-port"
      - "16382"
    volumes:
      - ./redis.conf:/etc/redis/redis.conf
      - ./redis2:/data
      - /etc/localtime:/etc/localtime
  redis-node-master-3:
    image: redis:7.0
    container_name: cluster-redis-3
    ports:
      - 6383:6379
      - 16383:16379
    command:
      - "redis-server"
      - "/etc/redis/redis.conf"
      - "--protected-mode"
      - "no"
      - "--cluster-announce-ip 192.168.180.45"
      - "--cluster-announce-port"
      - "6383"
      - "--cluster-announce-bus-port"
      - "16383"
    volumes:
      - ./redis.conf:/etc/redis/redis.conf
      - ./redis3:/data
      - /etc/localtime:/etc/localtime
  redis-node-slave-1:
    image: redis:7.0
    container_name: cluster-redis-1-slave
    ports:
      - 6384:6379
      - 16384:16379
    command:
      - "redis-server"
      - "/etc/redis/redis.conf"
      - "--protected-mode"
      - "no"
      - "--cluster-announce-ip 192.168.180.45"
      - "--cluster-announce-port"
      - "6384"
      - "--cluster-announce-bus-port"
      - "16384"
    volumes:
      - ./redis.conf:/etc/redis/redis.conf
      - ./redis1-slave:/data
      - /etc/localtime:/etc/localtime
  redis-node-slave-2:
    image: redis:7.0
    container_name: cluster-redis-2-slave
    ports:
      - 6385:6379
      - 16385:16379
    command:
      - "redis-server"
      - "/etc/redis/redis.conf"
      - "--protected-mode"
      - "no"
      - "--cluster-announce-ip 192.168.180.45"
      - "--cluster-announce-port"
      - "6385"
      - "--cluster-announce-bus-port"
      - "16385"
    volumes:
      - ./redis.conf:/etc/redis/redis.conf
      - ./redis2-slave:/data
      - /etc/localtime:/etc/localtime
  redis-node-slave-3:
    image: redis:7.0
    container_name: cluster-redis-3-slave
    ports:
      - 6386:6379
      - 16386:16379
    command:
      - "redis-server"
      - "/etc/redis/redis.conf"
      - "--protected-mode"
      - "no"
      - "--cluster-announce-ip 192.168.180.45"
      - "--cluster-announce-port"
      - "6386"
      - "--cluster-announce-bus-port"
      - "16386"
    volumes:
      - ./redis.conf:/etc/redis/redis.conf
      - ./redis3-slave:/data
      - /etc/localtime:/etc/localtime
[root@app01 redis-cluster]#

1、cluster-announce-ip 192.168.180.45 #指定集群访问ip
2、cluster-announce-port 6381 #指定集群每个容器对外端口
3、cluster-announce-bus-port 16381 #指定集群每个容器对外端口

三、生成cluster集群,并自动分配slot

注:自动生成cluster,需要把三台服务器的某一个pod放到前面,从pod放到后面依次错开,这样三台服务器才会都有一个master,都会有一个不是自己master的slave,说白了就是三个master要在三台服务器上,三个slave不能是自己服务器上master的slave

[root@app01 redis-cluster]# docker exec -it cluster-redis-1 bash
root@37441dffd83d:/data#
root@37441dffd83d:/data# redis-cli -a 1111--cluster create --cluster-replicas 1 192.168.180.45:6381 192.168.180.45:6382 192.168.180.45:6383 192.168.180.45:6384 192.168.180.45:6385 192.168.180.45:6386
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> 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.180.45:6385 to 192.168.180.45:6381
Adding replica 192.168.180.45:6386 to 192.168.180.45:6382
Adding replica 192.168.180.45:6384 to 192.168.180.45:6383
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 5ca2e1d0ce417913264505dd65a1e10bc86238f0 192.168.180.45:6381
   slots:[0-5460] (5461 slots) master
M: e078464a86cb11b2be58647a44a219145de0ec77 192.168.180.45:6382
   slots:[5461-10922] (5462 slots) master
M: c4f97daeb35d2ae6da2225875c62c10c1c01af31 192.168.180.45:6383
   slots:[10923-16383] (5461 slots) master
S: 1ed65f1d9d19f106089cbe34091b45cfb6ac14de 192.168.180.45:6384
   replicates e078464a86cb11b2be58647a44a219145de0ec77
S: 257ed7082d1b125186ccb76ff92a606822ae8251 192.168.180.45:6385
   replicates c4f97daeb35d2ae6da2225875c62c10c1c01af31
S: ed6b843f7948d21893185cf4915a38f345a9c535 192.168.180.45:6386
   replicates 5ca2e1d0ce417913264505dd65a1e10bc86238f0

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.180.45:6381)
M: 5ca2e1d0ce417913264505dd65a1e10bc86238f0 192.168.180.45:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: ed6b843f7948d21893185cf4915a38f345a9c535 192.168.180.45:6386
   slots: (0 slots) slave
   replicates 5ca2e1d0ce417913264505dd65a1e10bc86238f0
M: c4f97daeb35d2ae6da2225875c62c10c1c01af31 192.168.180.45:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: e078464a86cb11b2be58647a44a219145de0ec77 192.168.180.45:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 1ed65f1d9d19f106089cbe34091b45cfb6ac14de 192.168.180.45:6384
   slots: (0 slots) slave
   replicates e078464a86cb11b2be58647a44a219145de0ec77
S: 257ed7082d1b125186ccb76ff92a606822ae8251 192.168.180.45:6385
   slots: (0 slots) slave
   replicates c4f97daeb35d2ae6da2225875c62c10c1c01af31
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

root@37441dffd83d:/data# redis-cli -a 1111 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
ed6b843f7948d21893185cf4915a38f345a9c535 192.168.180.45:6386@16386 slave 5ca2e1d0ce417913264505dd65a1e10bc86238f0 0 1659678687245 1 connected
c4f97daeb35d2ae6da2225875c62c10c1c01af31 192.168.180.45:6383@16383 master - 0 1659678687000 3 connected 10923-16383
e078464a86cb11b2be58647a44a219145de0ec77 192.168.180.45:6382@16382 master - 0 1659678688000 2 connected 5461-10922
1ed65f1d9d19f106089cbe34091b45cfb6ac14de 192.168.180.45:6384@16384 slave e078464a86cb11b2be58647a44a219145de0ec77 0 1659678688246 2 connected
5ca2e1d0ce417913264505dd65a1e10bc86238f0 192.168.180.45:6381@16381 myself,master - 0 1659678685000 1 connected 0-5460
257ed7082d1b125186ccb76ff92a606822ae8251 192.168.180.45:6385@16385 slave c4f97daeb35d2ae6da2225875c62c10c1c01af31 0 1659678687000 3 connected

root@759ae1f1b1d0:/data# redis-cli -a 1111
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

127.0.0.1:6379> cluster nodes
e078464a86cb11b2be58647a44a219145de0ec77 192.168.180.45:6382@16382 master - 0 1659680247164 2 connected 5461-10922
ed6b843f7948d21893185cf4915a38f345a9c535 192.168.180.45:6386@16386 slave 5ca2e1d0ce417913264505dd65a1e10bc86238f0 0 1659680246000 1 connected
257ed7082d1b125186ccb76ff92a606822ae8251 192.168.180.45:6385@16385 slave c4f97daeb35d2ae6da2225875c62c10c1c01af31 0 1659680245000 3 connected
c4f97daeb35d2ae6da2225875c62c10c1c01af31 192.168.180.45:6383@16383 master - 0 1659680246159 3 connected 10923-16383
5ca2e1d0ce417913264505dd65a1e10bc86238f0 192.168.180.45:6381@16381 myself,master - 0 1659680244000 1 connected 0-5460
1ed65f1d9d19f106089cbe34091b45cfb6ac14de 192.168.180.45:6384@16384 slave e078464a86cb11b2be58647a44a219145de0ec77 0 1659680246000 2 connected
127.0.0.1:6379>

四、手动切换主从

当前主从关系

master 10.244.5.183:6379 --------> slave 10.244.8.167:6379
master 10.244.5.185:6379 --------> slave 10.244.8.168:6379
master 10.244.7.91:6379 --------> slave 10.244.7.92:6379

自动发生主从切换之后的集群状态

master 10.244.5.183:6379 --------> slave 10.244.8.167:6379
master 10.244.8.168:6379 --------> slave 10.244.5.185:6379
master 10.244.7.91:6379 --------> slave 10.244.7.92:6379

方法1:

在10.244.8.168:6379节点上执行cluster failover

>> cluster nodes

e6480bcec15d81d1599796718f5b373f614c46db 10.244.5.183:6379@16379 master - 0 1683618542678 1 connected 0-5460
9dc28a9f588cbf76982796d46cf88dac8c82be0c 10.244.5.185:6379@16379 master - 0 1683618540670 2 connected 5461-10922
57d46d9ce2f049a64d9997df3c0fe7d3f540aa6e 10.244.8.167:6379@16379 slave e6480bcec15d81d1599796718f5b373f614c46db 0 1683618541000 1 connected
c4c6629498252926edd7607ba63140d60c17c2df 10.244.7.92:6379@16379 slave 16004e4295729feb794ac67e0ecf5513a276c4db 0 1683618541000 3 connected
f05bd513ba28c84dc993c80f378e637af0393b9e 10.244.8.168:6379@16379 myself,slave 9dc28a9f588cbf76982796d46cf88dac8c82be0c 0 1683618542000 2 connected
16004e4295729feb794ac67e0ecf5513a276c4db 10.244.7.91:6379@16379 master - 0 1683618541674 3 connected 10923-16383

>> cluster failover

"OK"
>> cluster nodes

e6480bcec15d81d1599796718f5b373f614c46db 10.244.5.183:6379@16379 master - 0 1683618571818 1 connected 0-5460
9dc28a9f588cbf76982796d46cf88dac8c82be0c 10.244.5.185:6379@16379 slave f05bd513ba28c84dc993c80f378e637af0393b9e 0 1683618571000 7 connected
57d46d9ce2f049a64d9997df3c0fe7d3f540aa6e 10.244.8.167:6379@16379 slave e6480bcec15d81d1599796718f5b373f614c46db 0 1683618572822 1 connected
c4c6629498252926edd7607ba63140d60c17c2df 10.244.7.92:6379@16379 slave 16004e4295729feb794ac67e0ecf5513a276c4db 0 1683618571000 3 connected
f05bd513ba28c84dc993c80f378e637af0393b9e 10.244.8.168:6379@16379 myself,master - 0 1683618572000 7 connected 5461-10922
16004e4295729feb794ac67e0ecf5513a276c4db 10.244.7.91:6379@16379 master - 0 1683618570000 3 connected 10923-16383

到此主从切换完成

五、手动设置集群、分配槽位

1、cluster meet ip port 加入集群

登入 cluster-redis-1 容器:

docker exec -it cluster-redis-1 /bin/bash

接着,使用 redis-cli 命令进入 Redis客户端,再执行以下命令:

cluster meet 192.168.180.45 6382
cluster meet 192.168.180.45 6383
cluster meet 192.168.180.45 6384
cluster meet 192.168.180.45 6385
cluster meet 192.168.180.45 6386

执行后,在 cluster-redis-1 容器内的 Redis 客户端使用 cluster nodes:
登入 cluster-redis-2 容器,执行类似的操作,也能得到差不多的结果

删除节点:
cluster forget :从集群中移除指定的节点,这样就无法完成握手,过期时间为60s,60s后两节点又会继续完成握手。

2、cluster replicate 设置主从关系

这个命令要进入 cluster-redis-1 容器,在打开的 Bash 中执行,nodeid在cluster nodes里查询出来的那个特别长的字符串:

redis-cli -h 192.168.180.45 -p 6384 -a 1111 cluster replicate nodeid
redis-cli -h 192.168.180.45 -p 6385 -a 1111 cluster replicate nodeid
redis-cli -h 192.168.180.45 -p 6386 -a 1111 cluster replicate 5ca2e1d0ce417913264505dd65a1e10bc86238f0

3、cluster addslots 分配槽位

这个命令要进入 cluster-redis-1 容器,在打开的 Bash 中执行,:

redis-cli -h 192.168.180.45 -p 6381 -a 1111 cluster addslots {0..5500}
redis-cli -h 192.168.180.45 -p 6382 -a 1111 cluster addslots {5501..11000}
redis-cli -h 192.168.180.45 -p 6383 -a 1111 cluster addslots {11001..16383}

使用 cluster info 检查一下集群信息槽是否都已经分配了:
docker-compose部署redis cluster集群及常用集群命令_第1张图片
cluster:fail,可能是你还没有分配槽,或者 16384 个槽位没有全部分配出去。

4、增加master节点

4.1、增加新节点

#命令:old_ip只要是集群中的任意一个ip即可
redis-cli -a [password] --cluster add-node [new_ip:port [old_ip:port]
#例子
redis-cli -a 1111 --cluster add-node 192.168.180.45:6387 192.168.180.45:6381

root@759ae1f1b1d0:/data# redis-cli -a 1111 --cluster add-node 192.168.180.45:6387 192.168.180.45:6381
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.180.45:6387 to cluster 192.168.180.45:6381
>>> Performing Cluster Check (using node 192.168.180.45:6381)
M: 5ca2e1d0ce417913264505dd65a1e10bc86238f0 192.168.180.45:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: e078464a86cb11b2be58647a44a219145de0ec77 192.168.180.45:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: ed6b843f7948d21893185cf4915a38f345a9c535 192.168.180.45:6386
   slots: (0 slots) slave
   replicates 5ca2e1d0ce417913264505dd65a1e10bc86238f0
S: 257ed7082d1b125186ccb76ff92a606822ae8251 192.168.180.45:6385
   slots: (0 slots) slave
   replicates c4f97daeb35d2ae6da2225875c62c10c1c01af31
M: c4f97daeb35d2ae6da2225875c62c10c1c01af31 192.168.180.45:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 1ed65f1d9d19f106089cbe34091b45cfb6ac14de 192.168.180.45:6384
   slots: (0 slots) slave
   replicates e078464a86cb11b2be58647a44a219145de0ec77
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Getting functions from cluster
>>> Send FUNCTION LIST to 192.168.180.45:6387 to verify there is no functions in it
>>> Send FUNCTION RESTORE to 192.168.180.45:6387
>>> Send CLUSTER MEET to node 192.168.180.45:6387 to make it join the cluster.
[OK] New node added correctly.
root@759ae1f1b1d0:/data# 
4.2、给新节点分配散列槽

#命令
redis-cli -a [password] --cluster reshard [new_ip:port]
#例子
redis-cli -a 1111 --cluster reshard 192.168.180.45:6387

root@759ae1f1b1d0:/data# redis-cli -a 1111 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
e078464a86cb11b2be58647a44a219145de0ec77 192.168.180.45:6382@16382 master - 0 1659685577246 2 connected 5461-10922
ed6b843f7948d21893185cf4915a38f345a9c535 192.168.180.45:6386@16386 slave 5ca2e1d0ce417913264505dd65a1e10bc86238f0 0 1659685577000 1 connected
b85a8b29bf1d2c90669e3fbc47297318dcf5b052 192.168.180.45:6387@16387 master - 0 1659685577000 0 connected
257ed7082d1b125186ccb76ff92a606822ae8251 192.168.180.45:6385@16385 slave c4f97daeb35d2ae6da2225875c62c10c1c01af31 0 1659685577000 3 connected
c4f97daeb35d2ae6da2225875c62c10c1c01af31 192.168.180.45:6383@16383 master - 0 1659685576243 3 connected 10923-16383
5ca2e1d0ce417913264505dd65a1e10bc86238f0 192.168.180.45:6381@16381 myself,master - 0 1659685574000 1 connected 0-5460
1ed65f1d9d19f106089cbe34091b45cfb6ac14de 192.168.180.45:6384@16384 slave e078464a86cb11b2be58647a44a219145de0ec77 0 1659685578250 2 connected

root@759ae1f1b1d0:/data# redis-cli -a 1111 --cluster reshard 192.168.180.45:6387
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.180.45:6387)
M: b85a8b29bf1d2c90669e3fbc47297318dcf5b052 192.168.180.45:6387
   slots: (0 slots) master
M: e078464a86cb11b2be58647a44a219145de0ec77 192.168.180.45:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 257ed7082d1b125186ccb76ff92a606822ae8251 192.168.180.45:6385
   slots: (0 slots) slave
   replicates c4f97daeb35d2ae6da2225875c62c10c1c01af31
M: 5ca2e1d0ce417913264505dd65a1e10bc86238f0 192.168.180.45:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 1ed65f1d9d19f106089cbe34091b45cfb6ac14de 192.168.180.45:6384
   slots: (0 slots) slave
   replicates e078464a86cb11b2be58647a44a219145de0ec77
M: c4f97daeb35d2ae6da2225875c62c10c1c01af31 192.168.180.45:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: ed6b843f7948d21893185cf4915a38f345a9c535 192.168.180.45:6386
   slots: (0 slots) slave
   replicates 5ca2e1d0ce417913264505dd65a1e10bc86238f0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 100
What is the receiving node ID? b85a8b29bf1d2c90669e3fbc47297318dcf5b052
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: c4f97daeb35d2ae6da2225875c62c10c1c01af31
Source node #2: done

Ready to move 100 slots.
  Source nodes:
    M: c4f97daeb35d2ae6da2225875c62c10c1c01af31 192.168.180.45:6383
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
  Destination node:
    M: b85a8b29bf1d2c90669e3fbc47297318dcf5b052 192.168.180.45:6387
       slots: (0 slots) master
  Resharding plan:
    Moving slot 10923 from c4f97daeb35d2ae6da2225875c62c10c1c01af31
    Moving slot 10924 from c4f97daeb35d2ae6da2225875c62c10c1c01af31
   ......
    Moving slot 11022 from c4f97daeb35d2ae6da2225875c62c10c1c01af31
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 10923 from 192.168.180.45:6383 to 192.168.180.45:6387:
Moving slot 10924 from 192.168.180.45:6383 to 192.168.180.45:6387:
......
Moving slot 11022 from 192.168.180.45:6383 to 192.168.180.45:6387:

root@759ae1f1b1d0:/data# redis-cli -a 1111 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
e078464a86cb11b2be58647a44a219145de0ec77 192.168.180.45:6382@16382 master - 0 1659685781000 2 connected 5461-10922
ed6b843f7948d21893185cf4915a38f345a9c535 192.168.180.45:6386@16386 slave 5ca2e1d0ce417913264505dd65a1e10bc86238f0 0 1659685783000 1 connected
b85a8b29bf1d2c90669e3fbc47297318dcf5b052 192.168.180.45:6387@16387 master - 0 1659685783000 7 connected 10923-11022
257ed7082d1b125186ccb76ff92a606822ae8251 192.168.180.45:6385@16385 slave c4f97daeb35d2ae6da2225875c62c10c1c01af31 0 1659685783051 3 connected
c4f97daeb35d2ae6da2225875c62c10c1c01af31 192.168.180.45:6383@16383 master - 0 1659685784054 3 connected 11023-16383
5ca2e1d0ce417913264505dd65a1e10bc86238f0 192.168.180.45:6381@16381 myself,master - 0 1659685782000 1 connected 0-5460
1ed65f1d9d19f106089cbe34091b45cfb6ac14de 192.168.180.45:6384@16384 slave e078464a86cb11b2be58647a44a219145de0ec77 0 1659685782047 2 connected
root@759ae1f1b1d0:/data#

说明下执行命令redis-cli -a 1111 --cluster reshard 192.168.180.45:6387时出现的询问:

How many slots do you want to move (from 1 to 16384)?:表示需要移动槽的数量。填写自己的需要即可。

What is the receiving node ID?:表示:哪个id来接收它。即新的节点的id。

Source node #1:从哪个节点id上移动散列插槽。填写移动的源node的id。当然如果需要平均节点插槽,那么每个主节点的id都写上或者写all,然后需要移动的节点数量处理主节点数。

Source node #2:最后时填写done结束。

Do you want to proceed with the proposed reshard plan (yes/no)?:确定要移动这些槽的计划吗?输入yes或者no。

5、增加slave节点

#最快捷的方式(当然你首先要查看主节点的id)

redis-cli -a 1111 --cluster add-node 192.168.180.45:6388 192.168.180.45:6387 -a 1111 --cluster-slave b85a8b29bf1d2c90669e3fbc47297318dcf5b052

参数说明:
-a 123456 : 如果redis有密码则需要加这个命令
192.168.180.45:6388:新节点host+port
192.168.180.45:6387:已经存在集群的任意host+端口
–cluster-slave:表示新的节点是一个从节点,如果后面没有跟ID则随机分配到一个master下。
b85a8b29bf1d2c90669e3fbc47297318dcf5b052:指定master的ID

root@759ae1f1b1d0:/data# redis-cli -a 1111 --cluster add-node 192.168.180.45:6388 192.168.180.45:6387 -a 1111 --cluster-slave b85a8b29bf1d2c90669e3fbc47297318dcf5b052
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.180.45:6388 to cluster 192.168.180.45:6387
>>> Performing Cluster Check (using node 192.168.180.45:6387)
M: b85a8b29bf1d2c90669e3fbc47297318dcf5b052 192.168.180.45:6387
   slots:[10923-11022] (100 slots) master
M: e078464a86cb11b2be58647a44a219145de0ec77 192.168.180.45:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 257ed7082d1b125186ccb76ff92a606822ae8251 192.168.180.45:6385
   slots: (0 slots) slave
   replicates c4f97daeb35d2ae6da2225875c62c10c1c01af31
M: 5ca2e1d0ce417913264505dd65a1e10bc86238f0 192.168.180.45:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 1ed65f1d9d19f106089cbe34091b45cfb6ac14de 192.168.180.45:6384
   slots: (0 slots) slave
   replicates e078464a86cb11b2be58647a44a219145de0ec77
M: c4f97daeb35d2ae6da2225875c62c10c1c01af31 192.168.180.45:6383
   slots:[11023-16383] (5361 slots) master
   1 additional replica(s)
S: ed6b843f7948d21893185cf4915a38f345a9c535 192.168.180.45:6386
   slots: (0 slots) slave
   replicates 5ca2e1d0ce417913264505dd65a1e10bc86238f0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Automatically selected master 192.168.180.45:6387
>>> Send CLUSTER MEET to node 192.168.180.45:6388 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 192.168.180.45:6387.
[OK] New node added correctly.

root@759ae1f1b1d0:/data# redis-cli -a 1111 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
e078464a86cb11b2be58647a44a219145de0ec77 192.168.180.45:6382@16382 master - 0 1659686821000 2 connected 5461-10922
ed6b843f7948d21893185cf4915a38f345a9c535 192.168.180.45:6386@16386 slave 5ca2e1d0ce417913264505dd65a1e10bc86238f0 0 1659686820000 1 connected
b85a8b29bf1d2c90669e3fbc47297318dcf5b052 192.168.180.45:6387@16387 master - 0 1659686819146 7 connected 10923-11022
257ed7082d1b125186ccb76ff92a606822ae8251 192.168.180.45:6385@16385 slave c4f97daeb35d2ae6da2225875c62c10c1c01af31 0 1659686821152 3 connected
c4f97daeb35d2ae6da2225875c62c10c1c01af31 192.168.180.45:6383@16383 master - 0 1659686820149 3 connected 11023-16383
5ca2e1d0ce417913264505dd65a1e10bc86238f0 192.168.180.45:6381@16381 myself,master - 0 1659686818000 1 connected 0-5460
309355d8003155ed495efde5a0c1ca19e069200a 192.168.180.45:6388@16388 slave b85a8b29bf1d2c90669e3fbc47297318dcf5b052 0 1659686822156 7 connected
1ed65f1d9d19f106089cbe34091b45cfb6ac14de 192.168.180.45:6384@16384 slave e078464a86cb11b2be58647a44a219145de0ec77 0 1659686820000 2 connected
root@759ae1f1b1d0:/data#

6、删除slave节点

#命令
redis-cli -a password --cluster del-node ip:prot 要删除的节点ID
#例子
redis-cli -a 1111 --cluster del-node 192.168.180.45:6387 b85a8b29bf1d2c90669e3fbc47297318dcf5b052
redis-cli -a sinoeyes --cluster del-node 10.244.5.185:6379 9dc28a9f588cbf76982796d46cf88dac8c82be0c

root@759ae1f1b1d0:/data# redis-cli -a 1111 --cluster del-node 192.168.180.45:6387 b85a8b29bf1d2c90669e3fbc47297318dcf5b052
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node b85a8b29bf1d2c90669e3fbc47297318dcf5b052 from cluster 192.168.180.45:6387
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

7、删除master节点

因为主节点是分配了slot槽的,所以需要把删除节点的solt槽移动到其他master节点中去,否则数据会丢失。

7.1、数据迁移到其他主节点中 (逻辑和添加节点分配散列槽一样)

#命令
redis-cli -a password --cluster reshard host:prot
#例子
redis-cli -a 1111 --cluster reshard 192.168.180.45:6387

root@759ae1f1b1d0:/data# redis-cli -a 1111 --cluster reshard 192.168.180.45:6387
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.180.45:6387)
M: b85a8b29bf1d2c90669e3fbc47297318dcf5b052 192.168.180.45:6387
   slots:[10923-11022] (100 slots) master
M: e078464a86cb11b2be58647a44a219145de0ec77 192.168.180.45:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 257ed7082d1b125186ccb76ff92a606822ae8251 192.168.180.45:6385
   slots: (0 slots) slave
   replicates c4f97daeb35d2ae6da2225875c62c10c1c01af31
M: 5ca2e1d0ce417913264505dd65a1e10bc86238f0 192.168.180.45:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 1ed65f1d9d19f106089cbe34091b45cfb6ac14de 192.168.180.45:6384
   slots: (0 slots) slave
   replicates e078464a86cb11b2be58647a44a219145de0ec77
M: c4f97daeb35d2ae6da2225875c62c10c1c01af31 192.168.180.45:6383
   slots:[11023-16383] (5361 slots) master
   1 additional replica(s)
S: ed6b843f7948d21893185cf4915a38f345a9c535 192.168.180.45:6386
   slots: (0 slots) slave
   replicates 5ca2e1d0ce417913264505dd65a1e10bc86238f0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#表示移动slots的数量
How many slots do you want to move (from 1 to 16384)? 100
#表示哪个节点ID来接受这个移动的slots
What is the receiving node ID? c4f97daeb35d2ae6da2225875c62c10c1c01af31
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.
#表示哪个节点ID需要移动
Source node #1: b85a8b29bf1d2c90669e3fbc47297318dcf5b052
#输入done
Source node #2: done
Ready to move 100 slots.
  Source nodes:
    M: b85a8b29bf1d2c90669e3fbc47297318dcf5b052 192.168.180.45:6387
       slots:[10923-11022] (100 slots) master
  Destination node:
    M: c4f97daeb35d2ae6da2225875c62c10c1c01af31 192.168.180.45:6383
       slots:[11023-16383] (5361 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 10923 from b85a8b29bf1d2c90669e3fbc47297318dcf5b052
    Moving slot 10924 from b85a8b29bf1d2c90669e3fbc47297318dcf5b052
    ......
    Moving slot 11022 from b85a8b29bf1d2c90669e3fbc47297318dcf5b052
#最后确认是否是想要的结果
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 10923 from 192.168.180.45:6387 to 192.168.180.45:6383:
Moving slot 10924 from 192.168.180.45:6387 to 192.168.180.45:6383:
......
Moving slot 11022 from 192.168.180.45:6387 to 192.168.180.45:6383:
root@759ae1f1b1d0:/data# redis-cli -a 1111 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
e078464a86cb11b2be58647a44a219145de0ec77 192.168.180.45:6382@16382 master - 0 1659687694000 2 connected 5461-10922
ed6b843f7948d21893185cf4915a38f345a9c535 192.168.180.45:6386@16386 slave 5ca2e1d0ce417913264505dd65a1e10bc86238f0 0 1659687694645 1 connected
b85a8b29bf1d2c90669e3fbc47297318dcf5b052 192.168.180.45:6387@16387 slave c4f97daeb35d2ae6da2225875c62c10c1c01af31 0 1659687696652 8 connected
257ed7082d1b125186ccb76ff92a606822ae8251 192.168.180.45:6385@16385 slave c4f97daeb35d2ae6da2225875c62c10c1c01af31 0 1659687695000 8 connected
c4f97daeb35d2ae6da2225875c62c10c1c01af31 192.168.180.45:6383@16383 master - 0 1659687697657 8 connected 10923-16383
5ca2e1d0ce417913264505dd65a1e10bc86238f0 192.168.180.45:6381@16381 myself,master - 0 1659687695000 1 connected 0-5460
1ed65f1d9d19f106089cbe34091b45cfb6ac14de 192.168.180.45:6384@16384 slave e078464a86cb11b2be58647a44a219145de0ec77 0 1659687695648 2 connected
7.2、删除节点:

#命令
redis-cli -a password --cluster del-node ip:prot 删除点ID
#例子
redis-cli -a 1111 --cluster del-node 192.168.180.45:6387 b85a8b29bf1d2c90669e3fbc47297318dcf5b052

root@759ae1f1b1d0:/data# redis-cli -a 1111 --cluster del-node 192.168.180.45:6387 b85a8b29bf1d2c90669e3fbc47297318dcf5b052
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node b85a8b29bf1d2c90669e3fbc47297318dcf5b052 from cluster 192.168.180.45:6387
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

六、其他命令

1、查询槽位分配情况:

登入 cluster-redis-1 容器,使用 redis-cli 命令进入 Redis客户端,再执行以下命令

127.0.0.1:6379> cluster slots
1) 1) (integer) 0
   2) (integer) 5460
   3) 1) "192.168.180.45"
      2) (integer) 6381
      3) "5ca2e1d0ce417913264505dd65a1e10bc86238f0"
      4) (empty array)
   4) 1) "192.168.180.45"
      2) (integer) 6386
      3) "ed6b843f7948d21893185cf4915a38f345a9c535"
      4) (empty array)
2) 1) (integer) 5461
   2) (integer) 10922
   3) 1) "192.168.180.45"
      2) (integer) 6382
      3) "e078464a86cb11b2be58647a44a219145de0ec77"
      4) (empty array)
   4) 1) "192.168.180.45"
      2) (integer) 6384
      3) "1ed65f1d9d19f106089cbe34091b45cfb6ac14de"
      4) (empty array)
3) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "192.168.180.45"
      2) (integer) 6383
      3) "c4f97daeb35d2ae6da2225875c62c10c1c01af31"
      4) (empty array)
   4) 1) "192.168.180.45"
      2) (integer) 6385
      3) "257ed7082d1b125186ccb76ff92a606822ae8251"
      4) (empty array)
127.0.0.1:6379>
2、查询某个key的槽位:
127.0.0.1:6379> cluster keyslot test1
(integer) 4768
127.0.0.1:6379>
3、查询槽 slot 包含的键值对数量,需要去slot所在的容器里查询:
127.0.0.1:6379> cluster countkeysinslot 4768
(integer) 1
127.0.0.1:6379>
4、移除一个或多个槽对当前节点的指派:

登入 cluster-redis-1 容器(仅对当前节点有效)

root@759ae1f1b1d0:/data# redis-cli -a 1111 cluster delslots {34..36}
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
5、将一个或多个槽( slot)指派( assign)给当前节点:

登入 cluster-redis-1 容器(仅对当前节点有效)

root@759ae1f1b1d0:/data# redis-cli -a 1111 cluster addslots {34..36}
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
6、移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
root@79bb83f5d8cb:/data# redis-cli -a 1111 cluster flushslots
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
7、将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么会自动让另一个节点删除该槽,然后再指派到指定节点。
root@79bb83f5d8cb:/data# redis-cli -a 1111 cluster setslot 40 node b85a8b29bf1d2c90669e3fbc47297318dcf5b052
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
8、获取count个属于槽slot的键
root@79bb83f5d8cb:/data# redis-cli -a 1111 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> get test2
-> Redirected to slot [8899] located at 192.168.180.45:6382
"222"
192.168.180.45:6382> cluster getkeysinslot 8899 5
1) "test2"
192.168.180.45:6382>

你可能感兴趣的:(redis,redis,docker-compose)