Redis高可用搭建方案
本次使用redis版本为redis-5.0.14,下载地址http://redis.io/download
1、linux环境安装gcc
yum install gcc
2、上传redis-5.0.14.tar.gz到usr/local文件夹下,解压
tar -zxf redis-5.0.14.tar.gz
3、进入redis-5.0.14目录,执行make命令编译、安装
4、在/usr/local下创建对应目录
mkdir -p redis-cluster mkdir 9001 9002 9003 9004 9005 9006
5、拷贝redis-5.0.14/redis.conf到800*等六个目录中
6、修改800*目录中的redis.conf配置文件
daemonize yes port 9001 #把pid进程号写入到pidfile配置的文件 pidfile /var/run/redis_9001.pid #指定数据文件存放位置 dir /usr/local/redis-cluster/9001/ #启动集群模式 cluster-enabled yes #集群节点信息文件 cluster-config-file nodes-9001.conf #集群节点请求超时时间设置 cluster-node-timeout 15000 #注释bind 127.0.0.1 bind绑定的是自己机器网卡的ip,如果有多块网卡可以配多个ip,代表允许客户端通 #过机器的哪些网卡ip去访问,内网一般可以不配置bind,注释掉即可 #bind 127.0.0.1 #关闭保护模式 protected-mode no #开启aof追加备份数据 appendonlyof yes #密码设置 #集群节点间访问密码 masterauth root #客户端访问密码 requirepass root
7、依次启动800*六个服务
/usr/local/redis-5.0.14/src/redis‐server /usr/local/redis‐cluster/8001/redis.conf ... /usr/local/redis-5.0.14/src/redis‐server /usr/local/redis‐cluster/8006/redis.conf
8、查看服务是否启动
ps -ef | grep redis
9、使用redis-cli 创建集群(redis5以前的版本集群是依靠ruby脚本redis‐trib.rb实现)
使用redis-5.0.14/src/redis-cli --help 帮助命令,查看具体操作

再使用redis-5.0.14/src/redis-cli --cluster help帮助命令,查看集群相关操作

10 、关闭防火墙
# 临时关闭防火墙
# systemctl stop firewalld
# 禁止开机启动
# systemctl disable firewalld
11、创建集群
#--cluster-replicas 1,1代表为每个创建的主服务器节点创建一个从服务器节点,-a 集群间访问密码 /usr/local/redis-5.0.14/src/redis-cli -a root --cluster create --cluster-replicas 1 127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003 127.0.0.1:9004 127.0.0.1:9005 127.0.0.1:9006
12、验证集群,./redis‐cli ‐c ‐h ‐p (‐a访问服务端密码,‐c表示集群模式,指定ip地址和端口号)
redis-5.0.14/src/redis-cli -a root -p 9001 -c -h 127.0.0.1
13、查看集群信息
cluster nodes cluster info info
14、新增加redis点
#新增节点8007,8008 cp 8006 8007 cp 8006 8008 #执行第六步6、修改800*目录中的redis.conf配置文件 #执行第七部启动服务 #添加节点 add-node 新节点 已存在节点 redis-5.0.14/src/redis-cli -a root --cluster add-node 127.0.0.1:9007 127.0.0.1:9001

15、为9007分配hash槽(slot)
#找到集群中任意一个节点,执行重新分片工作 redis-5.0.14/src/redis-cli -a root --cluster reshard 127.0.0.1:9001
How many slots do you want to move (from 1 to 16384)? 900 (ps:需要多少个槽移动到新的节点上,自己设置,比如900个hash槽) What is the receiving node ID? 2728a594a0498e98e4b83a537e19f9a0a3790f38 (ps:把这900个hash槽移动到哪个节点上去,需要指定节点id) 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. Source node 1:all (ps:输入all为从所有主节点(9001,9002,9003)中分别抽取相应的槽数指定到新节点中,抽取的总槽数为900个) ... ... Do you want to proceed with the proposed reshard plan (yes/no)? yes (ps:输入yes确认开始执行分片任务)
16、添加从节点9008到集群中
redis-5.0.14/src/redis-cli -a root --cluster add-node 127.0.0.1:9008 127.0.0.1:9001
17、配置9007、9008主从
#进入9008节点 redis-5.0.14/src/redis-cli -c -a root -p 9008 -h 127.0.0.1 #查看帮助命令 cluster help #把当前的9008(slave)节点指定到一个主节点下(这里使用之前创建的9007主节点)c6d52b323402b64ae5228dede6bc17ec50196984位9007节点ID cluster REPLICATE c6d52b323402b64ae5228dede6bc17ec50196984


18、删除9008节点
#af3ab65fad3e489ab7f8741540624168731c8df4是9008节点id redis-5.0.14/src/redis-cli -a root --cluster del-node 127.0.0.1:9008 af3ab65fad3e489ab7f8741540624168731c8df4

19、删除主节点9007
#删除9007之前,因为9007里面存在hash槽,需要先把9007节点的数据迁移到其他可用主节点不然会出现数据丢失 redis-5.0.14/src/redis-cli -a root --cluster reshard 127.0.0.1:9007 # How many slots do you want to move (from 1 to 16384)? 900 What is the receiving node ID? c37890979a9d2a72bd035690747c669417e7f924 (ps:这里是需要把数据移动到哪?9002的主节点id) 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. Source node c6d52b323402b64ae5228dede6bc17ec50196984 (ps:这里是需要数据源,也就是我们的9007节点id) Source node 2:done (ps:这里直接输入done 开始生成迁移计划) ... ... Do you want to proceed with the proposed reshard plan (yes/no)? Yes (ps:这里输入yes开始迁移)
#删除9007节点,c6d52b323402b64ae5228dede6bc17ec50196984为9007节点id redis-5.0.14/src/redis-cli -a root --cluster del-node 127.0.0.1:9007 c6d52b323402b64ae5228dede6bc17ec50196984

浙公网安备 33010602011771号