Redis Cluster命令大全

半兽人 发表于: 2023-09-18   最后更新时间: 2023-09-26 17:24:05  
{{totalSubscript}} 订阅, 445 游览

访问Redis

进入命令行:

redis-cli -h $(hostname -i) -p 6379

k8s进入容器,再进入命令行

kubectl exec -it redis-cluster-0 -n my-space -- redis-cli -p 6379

查看

## 查看集群
cluster nodes
## 检查集群(任意连接一个集群节点,进行集群状态检查)
$ redis-cli --cluster check 172.23.7.168:6379 --cluster-search-multiple-owners

172.23.244.37:6379 (5e89e3b2...) -> 42749 keys | 5461 slots | 1 slaves.
172.23.146.147:6379 (6d0ee30b...) -> 63117 keys | 5461 slots | 1 slaves.
172.23.133.158:6379 (6a159d1b...) -> 63103 keys | 5462 slots | 1 slaves.
[OK] 168969 keys in 3 masters.
10.31 keys per slot on average.
>>> Performing Cluster Check (using node 172.23.7.168:6379)
S: dc1bc023bc4a102319be3f014c160cf8a1b42eee 172.23.7.168:6379
   slots: (0 slots) slave
   replicates 5e89e3b2ecbf70f3ee11f8c18f27ca2bf24e4fb0
M: 5e89e3b2ecbf70f3ee11f8c18f27ca2bf24e4fb0 172.23.244.37:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 6d0ee30b5858c29a603d24544e0cd2518e8130a4 172.23.146.147:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: bc1b04750e941675c55733fb39addc232b56337f 172.23.90.254:6379
   slots: (0 slots) slave
   replicates 6a159d1b2a3d1ba7d2bdb1fb80c2ce077d7ba348
M: 6a159d1b2a3d1ba7d2bdb1fb80c2ce077d7ba348 172.23.133.158:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: c3277fec40e97c74484ddfa222907a401157d767 172.23.26.253:6379
   slots: (0 slots) slave
   replicates 6d0ee30b5858c29a603d24544e0cd2518e8130a4
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

管理集群

## 启动集群
create-cluster start

## 创建集群
create-cluster create

# 停止集群
create-cluster stop

# 查看集群信息
redis-cli -p 7000 cluster nodes

# 如果想要再次加入集群,则需要先在该节点执行reset,再用add-node进行添加,进行增量同步复制。
cluster reset

# 重新分片
redis-cli --cluster reshard 127.0.0.1:7000
redis-cli --cluster reshard <host>:<port> --cluster-from <node-id> --cluster-to <node-id> --cluster-slots <number of slots> --cluster-yes

# 验证集群
redis-cli --cluster check 127.0.0.1:7000

中断集群

# 执行一个非法的内存访问从而让 Redis 崩溃,仅在开发时用于 BUG 调试。
$ redis-cli -p 7002 debug segfault
Error: Server closed the connection

k8s相关

创建集群:

kubectl exec -it redis-cluster-0 -n my-space -- redis-cli --cluster create --cluster-replicas 1 $(kubectl get pods -n my-space -l app=redis-cluster -o jsonpath='{range.items[*]}{.status.podIP}:6379 ' | sed "s/ :6379//g")

REF

https://redis.io/docs/management/scaling/

更新于 2023-09-26

查看redis更多相关的文章或提一个关于redis的问题,也可以与我们一起分享文章