安装编译REDIS
参考之前文章:Ubuntu20.04安装Redis6.2.9和Redission连接实践
将编译好的redis文件copy出来单独存放
准备配置文件
分别将上述文件copy 6份
redis.conf配置如下:除了port外,其他一样
port 7000
bind 172.17.108.172
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
可使用sed命令依次修改配置文件端口号
sed -i 's/7000/7001/g' redis7001/conf/redis.conf
制作启动、停止脚本
启动脚本start.sh
#!/bin/bash
# 依次启动每个redis实例
/home/rediscl/redis7000/bin/redis-server /home/rediscl/redis7000/conf/redis.conf &
/home/rediscl/redis7001/bin/redis-server /home/rediscl/redis7001/conf/redis.conf &
/home/rediscl/redis7002/bin/redis-server /home/rediscl/redis7002/conf/redis.conf &
/home/rediscl/redis7003/bin/redis-server /home/rediscl/redis7003/conf/redis.conf &
/home/rediscl/redis7004/bin/redis-server /home/rediscl/redis7004/conf/redis.conf &
/home/rediscl/redis7005/bin/redis-server /home/rediscl/redis7005/conf/redis.conf &
# 创建redis cluster
/home/rediscl/redis7000/bin/redis-cli --cluster create 172.17.108.172:7000 172.17.108.172:7001 172.17.108.172:7002 172.17.108.172:7003 172.17.108.172:7004 172.17.108.172:7005 --cluster-replicas 1
执行完成后查看启动集群运行情况
rediscl@TOBY-HYW:~$ /home/rediscl/redis7000/bin/redis-cli -h 172.17.108.172 -p 7000 -c
172.17.108.172:7000> 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:224
cluster_stats_messages_pong_sent:206
cluster_stats_messages_sent:430
cluster_stats_messages_ping_received:206
cluster_stats_messages_pong_received:224
cluster_stats_messages_received:430
172.17.108.172:7000> get test
-> Redirected to slot [6918] located at 172.17.108.172:7001
(nil)
172.17.108.172:7001> set test 123
OK
172.17.108.172:7001> get test
"123"
停止脚本stop.sh
#!/bin/bash
# 定义要关闭的 Redis 进程的关键字列表
redis_keywords=(
"redis-server"
"redis-cli"
# 添加更多的关键字到列表中
)
# 循环遍历关键字列表并关闭匹配的进程
for keyword in "${redis_keywords[@]}"; do
echo "Closing Redis processes with keyword: $keyword"
# 使用 ps -ef 命令获取匹配关键字的进程信息,并通过 awk 提取 PID
pids=$(ps -ef | grep "$keyword" | grep -v grep | awk '{print $2}')
# 循环遍历 PID 列表并关闭每个进程
for pid in $pids; do
echo "Closing Redis process with PID: $pid"
kill "$pid"
done
done
集群操作命令汇总
CLUSTER MEET
用于在Redis集群中添加新的节点,并将其与集群中的其他节点连接起来。
CLUSTER MEET <ip> <port>
CLUSTER ADDSLOTS
将指定的槽位分配给当前节点。
CLUSTER ADDSLOTS <slot> [slot ...]
CLUSTER REPLICATE
将当前节点设置为指定节点的从节点。
CLUSTER REPLICATE <node_id>
CLUSTER FORGET
从集群中移除指定节点。
CLUSTER FORGET <node_id>
CLUSTER REBALANCE
重新分配集群中的槽位,以便在节点之间平衡数据分布。
CLUSTER REBALANCE [options]
CLUSTER INFO
获取有关Redis集群的信息,包括节点和槽位的状态。
CLUSTER INFO
CLUSTER NODES
获取有关所有节点的详细信息,包括节点ID、IP地址、端口号、角色等。
CLUSTER NODES
CLUSTER SLAVES
获取指定主节点的所有从节点信息。
CLUSTER SLAVES <node_id>
CLUSTER KEYSLOT
计算指定键属于哪个槽位。
CLUSTER KEYSLOT <key>
CLUSTER COUNTKEYSINSLOT
获取指定槽位中的键数量。
CLUSTER COUNTKEYSINSLOT <slot>