本文在三台云服务器上搭建三主三从6个节点
服务器信息
IP | port | 描述 |
121.36.172.25 | 6379 | |
122.112.237.126 | 6379 | |
119.3.91.242 | 6379 | |
121.36.172.25 | 6389 | |
122.112.237.126 | 6389 | |
119.3.91.242 | 6389 |
在每台机器上创建对应的文件夹
mkdir -p /data/redis/cluster/3679/conf /data/redis/cluster/3679/data /data/redis/cluster/3689/conf /data/redis/cluster/3689/data
在conf目录下创建配置文件
vim redis.conf
##节点端口
port 6379
##允许任何来源
bind 0.0.0.0
save ""
appendonly no
appendfilename appendonly.aof
appendfsync everysec
auto-aof-rewrite-min-size 10M
auto-aof-rewrite-percentage 100
requirepass qwer123456
masterauth qwer123456
cluster-enabled yes
cluster-node-timeout 10000
cluster-announce-ip 121.36.172.25 #暴露本机ip 重要
cluster-announce-port 6379
在每个conf目录下都创建对应的文件,修改对应的IP和端口,然后在每个机器上执行如下两条命令启动两个redis实例。注意由于前面的文章已经下载好redis镜像了,所以此处就不再描述了。直接启动容器
podman run -d -p 6379:6379 --name redis-master --net host --privileged=true -v /data/redis/cluster/6379/conf/redis.conf:/etc/redis/redis.conf -v /data/redis/cluster/6379/data:/data f1b6973564e9 redis-server /etc/redis/redis.conf
podman run -d -p 6389:6389 --name redis-slave --net host --privileged=true -v /data/redis/cluster/6389/conf/redis.conf:/etc/redis/redis.conf -v /data/redis/cluster/6389/data:/data f1b6973564e9 redis-server /etc/redis/redis.conf
随便进入其中一个容器
podman exec -it redis-master /bin/bash
##我们先不将从节点加入,所以副本数量写0即可
redis-cli -a qwer123456 --cluster create 121.36.172.25:6379 122.112.237.126:6379 119.3.91.242:6379 --cluster-replicas 0
出现如下说明这三个主节点已经加入到集群中了
添加slave
方式1:
redis-cli --cluster add-node 新IP:PORT 目标主节点 --cluster-slave
方式2:
先将节点加入到集群中,然后再指定主节点的从节点
先在从节点上执行
CLUSTER MEET 121.36.172.25 6389(集群中的节点)
CLUSTER REPLICATE cc7df9d2778108fde0a37030b6adac12f0a07e90(主节点)
(error) ERR To set a master the node must be empty and without assigned slots.
上面的错误意思是不能为一个非空并且分配了slot的主节点继续添加从节点,需要先将节点上的槽位删除掉
测试集群槽位的移动
redis-cli -a qwer123456 --cluster reshard 121.36.172.25:6379
root@hecs-407607:/data# redis-cli -a qwer123456 --cluster reshard 121.36.172.25:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 121.36.172.25:6379)
M: 6ee744600ce1ac19cbc8c1179e884677071d7486 121.36.172.25:6379
slots:[5500-9001],[10923-16383] (8963 slots) master
2 additional replica(s)
S: 949542703680d107ae639ca07fe3a06ab0ad0395 119.3.91.242:6399
slots: (0 slots) slave
replicates 6ee744600ce1ac19cbc8c1179e884677071d7486
S: 8e992c915ba2a8fc7caa82b9c21bc35bbb4cacd7 122.112.237.126:6389
slots: (0 slots) slave
replicates 6ee744600ce1ac19cbc8c1179e884677071d7486
M: a831692d39e692f512bd80451cb96eec98463197 122.112.237.126:6379
slots:[9002-10922] (1921 slots) master
1 additional replica(s)
S: 0866512829e1d813146db53b8e0557377bb69104 119.3.91.242:6389
slots: (0 slots) slave
replicates a831692d39e692f512bd80451cb96eec98463197
S: af824ba123599fd9d5e08bbe219bb49bfc7fb183 121.36.172.25:6389
slots: (0 slots) slave
replicates cc7df9d2778108fde0a37030b6adac12f0a07e90
M: cc7df9d2778108fde0a37030b6adac12f0a07e90 119.3.91.242:6379
slots:[0-5499] (5500 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.
How many slots do you want to move (from 1 to 16384)? 3500
需要移动的槽位数量
What is the receiving node ID? a831692d39e692f512bd80451cb96eec98463197
接受槽位的节点
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.
输入all表示从其他节点平分移动
Source node #1: 6ee744600ce1ac19cbc8c1179e884677071d7486
从哪个节点
Source node #2: done
开始分配
按照上面的步骤基本上都可以搭建集群了