版本:CentOS 7.0,Redis-6.2.6
本文模拟于一台机器上安装6个redis节点实现redis集群,实际生产环境中操作基本类似。
节点:
192.168.14.133:8001
192.168.14.133:8002
192.168.14.133:8003
192.168.14.133:8004
192.168.14.133:8006
192.168.14.133:8007
1.从官网下载redis
2.将redis-6.2.4.tar.gz上传至192.168.14.132的/usr/local/路径下
cd /usr/local/
tar -zvxf redis-6.2.4.tar.gz
cd redis-6.2.4/
make && make install
3.创建/usr/local/redis-cluster文件夹
4.在redis-cluster文件夹下分别创建8001、8002、8003、8004、8006、8007文件夹
5.将/usr/local/redis-6.2.6/redis.conf复制到/usr/local/redis-cluster/8001/路径下
6.修改配置文件
daemonize no 改成: daemonize yes
注释掉bind 127.0.0.1
protected-mode yes 改成: protected-mode no
port 8001
dbfilename dump_8001.rdb
masterauth 密码
requirepass 密码
cluster-enabled yes
cluster-config-file nodes-8001.conf
7.将修改好的配置文件复制到8002、8003、8004、8006、8007文件夹下,并修改里面的端口号
8.切换至/usr/local/redis-cluster路径下,编辑批量启动脚本
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/8001/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/8002/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/8003/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/8004/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/8007/redis.conf
/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/8006/redis.conf
9.启动脚本
bash start.sh
10.查看进程
ps -ef | grep redis
11. 将redis-cli复制到/usr/local/bin/路径下
cp /usr/local/redis-cli /usr/local/bin
12.切换至/usr/local/bin路径下,开始创建集群
redis-cli -a 密码 --cluster create --cluster-replicas 1 192.168.14.133:8001 192.168.14.133:8002 192.168.14.133:8003 192.168.14.133:8004 192.168.14.133:8006 192.168.14.133:8007
说明Redis中有数据,需要连接该Redis执行“flushdb”清空数据。如果还不行,则停止Redis服务后,删除该节点下的nodes-800x.conf文件
如果提示:
Not all 16384 slots are covered by nodes.
则执行
redis-cli -a 密码 --cluster fix 192.168.14.133:8001
再执行以下命令检测状态
redis-cli -a 123123 --check 192.168.14.133:8001
13.连接其中一个节点,并检查集群状态
redis-cli -c -p 8001 -a 密码
cluster info
14.操作完成,可在任意节点写入,其他节点也可读取,连接命令不要忘记加-c
本文借鉴:https://www.icode9.com/content-2-855503.html,感谢原作者