每台服务器上安装redis:
- 创建文件路径和拷贝安装包
# mkdir rediscluster 创建redis目录
# cd rediscluster 进入redis目录
# mkdir config 创建配置文件目录
# mkdir logs 创建日志目录
# cp redis-5.0.14.tar.gz 拷贝安装包到当前位置
- 解压并安装,删除安装包
# tar zxf redis-5.0.14.tar.gz
# cd redis-5.0.4
# make && make install
# rm -rf redis-5.0.14.tar.gz
- 拷贝配置文件(可修改端口和密码)
# cp redis_8001.conf /home/dx/rediscluster/config/
# cp redis_8002.conf /home/dx/rediscluster/config/
# cp redis_8003.conf /home/dx/rediscluster/config/
- 启动服务节点
# cd /home/dx/rediscluster/redis-5.0.4/src
# ./redis-server ../../config/redis_8001.conf
# ./redis-server ../../config/redis_8002.conf
# ./redis-server ../../config/redis_8003.conf
搭建集群:
- 集群节点握手
# cd /home/dx/rediscluster/redis-5.0.4/src
# ./redis-cli -h 192.195.128.10 -p 8001 -a abcd1234
# CLUSTER MEET 192.198.68.10 8001
# CLUSTER MEET 192.198.68.10 8002
# CLUSTER MEET 192.198.68.10 8003
# CLUSTER MEET 192.198.68.11 8001
# CLUSTER MEET 192.198.68.11 8002
# CLUSTER MEET 192.198.68.11 8003
# CLUSTER MEET 192.198.68.12 8001
# CLUSTER MEET 192.198.68.12 8002
# CLUSTER MEET 192.198.68.12 8003
- 查看集群节点ID,目的是分配主从关系
# cd /home/dx/rediscluster/redis-5.0.4/src
# ./redis-cli -h 192.198.68.10 -p 8001 -a abcd1234
# cluster nodes
查看的信息如下:
9a12508daf857787525cb28bb84d4bb8940a0fd3 192.198.68.10:8001@18003 myself,master - 0 1646813330000 2 connected 10923-16383
acf896e8a66300f78e1f7d97eb8031bb61f42187 192.198.68.10:8002@18005 master 0 1646813330487 4 connected
db7dd59bf7cdfb326258d07a2a033a17c0f9ecd5 192.198.68.10:8003@18004 master 0 1646813331488 7 connected
1ec8de0b5def7e091b6af6b289f7806430fdf9b5 192.198.68.11:8001@18001 master - 0 1646813332000 7 connected 0-5461
b62d6100b44a2cbff5d848a0c5fb7a0616cbea51 192.198.68.11:8002@18002 master - 0 1646813332491 3 connected 5462-10922
510d43b46defb9f7e31b942d4dd32c52f9824cb5 192.198.68.11:8003@18006 master 0 1646813331000 5 connected
2ec8de0b5def7e091b6af6b289f7806430fdf9b6 192.198.68.12:8001@18001 master - 0 1646813332000 7 connected 0-5461
c62d6100b44a2cbff5d848a0c5fb7a0616cbea52 192.198.68.12:8002@18002 master - 0 1646813332491 3 connected 5462-10922
810d43b46defb9f7e31b942d4dd32c52f9824cb3 192.198.68.12:8003@18006 master 0 1646813331000 5 connected
- 分配主从关系
将节点192.198.68.11:8001和192.198.68.12:8001设置为192.198.68.10:8001的从节点,9a12508daf857787525cb28bb84d4bb8940a0fd3是192.198.68.10:8001的标识
挂从节点192.198.68.11:8001
# cd /home/dx/rediscluster/redis-5.0.4/src
# ./redis-cli -h 192.198.68.11 -p 8001 -a abcd1234
# cluster replicate 9a12508daf857787525cb28bb84d4bb8940a0fd3
挂从节点192.198.68.12:8001 同上
8002、8003主从方法同上。
注意:主节点1 192.198.68.10:8001
从节点1 192.198.68.11:8001,192.198.68.12:8001
主节点2 192.198.68.11:8002
从节点2 192.198.68.10:8002,192.198.68.12:8002
主节点3 192.198.68.12:8003
从节点3 192.198.68.10:8003,192.198.68.11:8003
- 集群节点槽位分配(一个#开头的为一个命令)
# ./redis-cli -h 192.198.68.10 -p 8001 -a abcd1234
cluster addslots {0..5461}
# ./redis-cli -h 192.198.68.11 -p 8002 -a abcd1234
cluster addslots {5462..10922}
# ./redis-cli -h 192.198.68.12 -p 8003 -a abcd1234
cluster addslots {10923..16383}
安装完毕。