Redis集群搭建
redis版本:6.2.5
1、搭建Redis
2、创建集群目录
[root@localhost ~] mkdir redis-cluster
3、搭建集群
搭建集群最少也得需要 3 台主机,如果每台主机再配置 1 台从机的话,则最少需要 6 台机器。这里设定这 6 台主机端口号为 7001 - 7006
操作流程:
- 复制 1 份安装好的 redis 目录文件到集群目录 7001 中
- 如果存在持久化文件,如 dump.rdb 、appendonly.aof 等需要使用
rm -rf dump.rdb
删除 - 修改 redis.conf 中的 port 为 7001
- 去掉 redis.conf 中的
cluster-enable yes
前面的注释,开启集群模式 - 修改 redis.conf 中的 bind 后面的地址 设为 0.0.0.0 表示任意位置可以访问,设为127.0.0.1 表示只能本机访问,设为本地ip地址表示局域网内可以访问
- 关闭防火墙
systemctl stop firewalld
- 复制几份 7001 到 7002 - 7006 目录中
cp 7001/ 7002 -r
- 依次修改 7002 - 7006 中的 redis.conf 中的 port 为 7002 - 7006
- 为了避免不必要的重复操作,可以创建并编辑启动脚本 startall.sh ( 文件名随便起 ),输入一下内容并保存
cd 7001 ./bin/redis-server ./redis.conf cd .. cd 7002 ./bin/redis-server ./redis.conf cd .. cd 7003 ./bin/redis-server ./redis.conf cd .. cd 7004 ./bin/redis-server ./redis.conf cd .. cd 7005 ./bin/redis-server ./redis.conf cd .. cd 7006 ./bin/redis-server ./redis.conf cd ..
- 修改启动脚本的权限
chmod u+x startall.sh
- 运行脚本,启动所有的redis实例
- 使用
ps -ef | grep redis -i
查看是否成功启动 - 随便选择 7001 - 7006 的目录, cd 进该目录的 bin 目录,使用
./redis-cli --cluster create ip:port ip:port --cluster-replicas 1
创建集群 - 接着使用
./redis-cli -h ip地址 -p 端口号 -c
连接集群, -c 代表指定集群连接 - 连接之后,使用
cluster info
可以查看集群信息,使用cluster nodes
可以查看集群节点的主从分布