要求
- 要求三主三从
- 扩容
- 缩容
环境信息
- Redis 版本:
6.0.10
- 虚拟机操作系统:
CentOS 7
集群信息
安装目录
/redis
|--cluster
|--|--7001 - 7008
主从信息
奇数为主,偶数为从
- 7001 - 7002
- 7003 - 7004
- 7005 - 7006
- 7007 - 7008
操作步骤
安装集群
-
新建安装文件夹,解压安装包
mkdir /redis/cluster -p cd /redis/cluster/ mkdir 7001 cd /redis tar -xzvf redis-6.0.10.tar.gz
-
升级 GCC 版本
# 查看gcc版本是否在5.3以上,centos7.6默认安装4.8.5 gcc -v # 升级gcc到5.3及以上,如下: # 升级到gcc 9.3: yum -y install centos-release-scl yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils scl enable devtoolset-9 bash
-
安装 Redis
cd /redis/redis-6.0.10 make make install PREFIX=/redis/cluster/7001 cp /redis/redis-6.0.10/redis.conf /redis/cluster/7001/bin/
-
修改
redis.conf
配置文件# bind 127.0.0.1 protected-mode no port 7001 daemonize yes cluster-enabled yes cluster-config-file nodes-7001.conf pidfile /var/run/redis_7001.pid dir /redis/cluster/7001/data/
-
复制生成其他端口实例,并修改配置文件
cd /redis/cluster cp -r 7001/ 7002 cp -r 7001/ 7003 cp -r 7001/ 7004 cp -r 7001/ 7005 cp -r 7001/ 7006 cp -r 7001/ 7007 cp -r 7001/ 7008 # 生成的节点信息文件都在 /redis/cluster 目录下
替换对应节点的配置文件中的配置:
port 7001 cluster-config-file nodes-7001.conf pidfile /var/run/redis_7001.pid dir /redis/cluster/7001/data/
创建持久化文件所在目录:
mkdir -p /redis/cluster/7001/data/ mkdir -p /redis/cluster/7002/data/ mkdir -p /redis/cluster/7003/data/ mkdir -p /redis/cluster/7004/data/ mkdir -p /redis/cluster/7005/data/ mkdir -p /redis/cluster/7006/data/ mkdir -p /redis/cluster/7007/data/ mkdir -p /redis/cluster/7008/data/
-
启动所有 Redis 实例
/redis/cluster/7001/bin/redis-server /redis/cluster/7001/bin/redis.conf /redis/cluster/7002/bin/redis-server /redis/cluster/7002/bin/redis.conf /redis/cluster/7003/bin/redis-server /redis/cluster/7003/bin/redis.conf /redis/cluster/7004/bin/redis-server /redis/cluster/7004/bin/redis.conf /redis/cluster/7005/bin/redis-server /redis/cluster/7005/bin/redis.conf /redis/cluster/7006/bin/redis-server /redis/cluster/7006/bin/redis.conf /redis/cluster/7007/bin/redis-server /redis/cluster/7007/bin/redis.conf /redis/cluster/7008/bin/redis-server /redis/cluster/7008/bin/redis.conf
-
创建 Redis 集群(创建时 Redis 里不要有数据)
/redis/cluster/7001/bin/redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --cluster-replicas 1
# 以集群方式连接 /redis/cluster/7001/bin/redis-cli -h 127.0.0.1 -p 7001 -c # 集群信息 cluster info # 集群节点 cluster nodes
# 测试集群,通过查看集群转发 set k1 v1 set k2 v2 set k3 v3 set k4 v4
集群扩容
-
集群扩容
/redis/cluster/7001/bin/redis-cli --cluster add-node 127.0.0.1:7007 127.0.0.1:7001 # 7008 作为 7007 的从节点加入集群,最后的参数是 7007 的集群 id /redis/cluster/7001/bin/redis-cli --cluster add-node 127.0.0.1:7008 127.0.0.1:7007 --cluster-slave --cluster-master-id 1439a52999e3b678a8104bb5d1dda87434bd1746 # 确认节点已加入 /redis/cluster/7001/bin/redis-cli -h 127.0.0.1 -p 7001 -c cluster nodes
-
给 7007 分槽
/redis/cluster/7001/bin/redis-cli --cluster reshard 127.0.0.1:7007 # 7007 的 ID : # 1439a52999e3b678a8104bb5d1dda87434bd1746
集群缩容
-
将槽分配出去
/redis/cluster/7001/bin/redis-cli --cluster reshard 127.0.0.1:7001
-
删除集群节点,先删除从节点,后删除主节点
/redis/cluster/7001/bin/redis-cli --cluster del-node 127.0.0.1:7008 69ac90b063478565b0aeb888871c2cd9acbbbb81 /redis/cluster/7001/bin/redis-cli --cluster del-node 127.0.0.1:7007 1439a52999e3b678a8104bb5d1dda87434bd1746