什么是redis集群?
redis集群是redis提供的分布式数据方案,可以在多个redis节点之间进行数据共享。它通过分片来提供一定程度的可用性,即使集群中有一部分节点失效或者无法进行通信,集群也能继续处理命令请求,并提供复制和故障转移功能。
伪集群环境搭建
使用虚拟机128,使用端口7001~7006。
第一步:新建文件夹,将redis安装包放到文件夹下。
[root@localhost ~]# cd /opt
[root@localhost opt]# mkdir redis
[root@localhost opt]# cd redis
[root@localhost redis]# mkdir cluster
[root@localhost cluster]# mkdir 7001
[root@localhost cluster]# mkdir 7002
[root@localhost cluster]# mkdir 7003
[root@localhost cluster]# mkdir 7004
[root@localhost cluster]# mkdir 7005
[root@localhost cluster]# mkdir 7006
[root@localhost cluster]# ls
7001 7002 7003 7004 7005 7006
[root@localhost cluster]# pwd
/opt/redis/cluster
[root@localhost cluster]# cp /opt/redis-5.0.4.tar.gz /opt/redis/cluster/redis-5.0.4.tar.gz
[root@localhost cluster]# ls
7001 7002 7003 7004 7005 7006 redis-5.0.4.tar.gz
第二步:解压安装包,并进行编译,编译遇到问题可以参考【Linux下安装redis】
[root@localhost cluster]# tar zxvf redis-5.0.4.tar.gz
.
.
.
[root@localhost cluster]# cd redis-5.0.4
[root@localhost redis-5.0.4]# make
.
.
.
没有redis安装包的可以点击【Redis官网】,根据Installation下命令完成上面第一步和第二步。
第三步:修改redis配置文件。
[root@localhost redis-5.0.4]# cd ..
[root@localhost cluster]# ls
7001 7002 7003 7004 7005 7006 redis-5.0.4 redis-5.0.4.tar.gz
[root@localhost cluster]# cp redis-5.0.4/redis.conf 7001/redis7001.conf
[root@localhost cluster]# vim 7001/redis7001.conf
需要修改的配置
#绑定ip,也可以在bind配置前加上#,注掉bind
bind 192.168.136.128
#关闭保护模式
protected-mode no
#设置端口
port 7001
#后台运行
daemonize yes
#pid文件路径
pidfile /opt/redis/cluster/7001/redis_7001.pid
#日志文件路径
logfile "/opt/redis/cluster/7001/redis7001.log"
#开启集群模式
cluster-enabled yes
#redis集群节点配置文件
cluster-config-file /opt/redis/cluster/7001/nodes-7001.conf
#集群节点访问超时时间
cluster-node-timeout 15000
#集群迁移屏障
cluster-migration-barrier 1
#当负责一个插槽的主库下线且没有相应的从库进行故障恢复时,集群不可用
cluster-require-full-coverage yes
再将配置文件依次复制到7002、7003、7004、7005、7006下。使用vim 7002/redis7002.conf 命令后,可以使用全局替换,命令::%s/7001/7002/g,将7001替换成7002,然后依次7003~7006下配置文件相关配置中7001改成7003~7006。
[root@localhost cluster]# cp 7001/redis7001.conf 7002/redis7002.conf
[root@localhost cluster]# cp 7001/redis7001.conf 7003/redis7003.conf
[root@localhost cluster]# cp 7001/redis7001.conf 7004/redis7004.conf
[root@localhost cluster]# cp 7001/redis7001.conf 7005/redis7005.conf
[root@localhost cluster]# cp 7001/redis7001.conf 7006/redis7006.conf
[root@localhost cluster]# vim 7002/redis7002.conf
[root@localhost cluster]# vim 7003/redis7003.conf
[root@localhost cluster]# vim 7004/redis7004.conf
[root@localhost cluster]# vim 7005/redis7005.conf
[root@localhost cluster]# vim 7006/redis7006.conf
第四步:启动redis伪集群。
启动redis服务
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7001/redis7001.conf &
[1] 7170
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7002/redis7002.conf &
[2] 7175
[1] Done ./redis-5.0.4/src/redis-server 7001/redis7001.conf
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7003/redis7003.conf &
[3] 7180
[2] Done ./redis-5.0.4/src/redis-server 7002/redis7002.conf
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7004/redis7004.conf &
[4] 7186
[3] Done ./redis-5.0.4/src/redis-server 7003/redis7003.conf
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7005/redis7005.conf &
[5] 7191
[4] Done ./redis-5.0.4/src/redis-server 7004/redis7004.conf
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7006/redis7006.conf &
[6] 7197
[5] Done ./redis-5.0.4/src/redis-server 7005/redis7005.conf
[root@localhost cluster]# ps -ef|grep redis
root 7171 1 0 11:03 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7001 [cluster]
root 7176 1 0 11:03 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7002 [cluster]
root 7181 1 0 11:03 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7003 [cluster]
root 7187 1 0 11:03 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7004 [cluster]
root 7192 1 0 11:03 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7005 [cluster]
root 7198 1 0 11:03 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7006 [cluster]
root 7203 2599 0 11:04 pts/2 00:00:00 grep redis
[6]+ Done ./redis-5.0.4/src/redis-server 7006/redis7006.conf
redis5.0以上不需要借助ruby等来进行集群启动,直接使用redis-cli即可。
可以根据自己需求来设置主节点(主机)和从节点(从机)。假如想要设置redis伪集群中7001、7003、7005为主节点,7002、7004、7006为从节点,执行命令:./redis-5.0.4/src/redis-cli --cluster-replicas 1 --cluster create 192.168.136.128:7001 192.168.136.128:7003 192.168.136.128:7005 192.168.136.128:7002 192.168.136.128:7004 192.168.136.128:7006
[root@localhost cluster]# ./redis-5.0.4/src/redis-cli --cluster-replicas 1 --cluster create 192.168.136.128:7001 192.168.136.128:7003 192.168.136.128:7005 192.168.136.128:7002 192.168.136.128:7004 192.168.136.128:7006
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.136.128:7004 to 192.168.136.128:7001
Adding replica 192.168.136.128:7006 to 192.168.136.128:7003
Adding replica 192.168.136.128:7002 to 192.168.136.128:7005
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: b0f9136c77ad95517fc482182876c1a6f8a12110 192.168.136.128:7001
slots:[0-5460] (5461 slots) master
M: 5f386c6814f9abdc25bd387d7720eaedd3bc032f 192.168.136.128:7003
slots:[5461-10922] (5462 slots) master
M: ed1cf236e4336cc7dd0f1c201c914013d9f9d98b 192.168.136.128:7005
slots:[10923-16383] (5461 slots) master
S: 06f11443ea10d85949b89e5ab7c2f59ab0ae4e28 192.168.136.128:7002
replicates b0f9136c77ad95517fc482182876c1a6f8a12110
S: ba1cf9c11c90adfe385b2cb328cd4f5beb18d8f0 192.168.136.128:7004
replicates 5f386c6814f9abdc25bd387d7720eaedd3bc032f
S: 606ad6d26baeca2c8e4a12ea06781ca8e17fcfdd 192.168.136.128:7006
replicates ed1cf236e4336cc7dd0f1c201c914013d9f9d98b
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 192.168.136.128:7001)
M: b0f9136c77ad95517fc482182876c1a6f8a12110 192.168.136.128:7001
slots:[0-5460] (5461 slots) master
13828609934981332993 additional replica(s)
S: 06f11443ea10d85949b89e5ab7c2f59ab0ae4e28 192.168.136.128:7002
slots: (0 slots) slave
replicates b0f9136c77ad95517fc482182876c1a6f8a12110
M: 5f386c6814f9abdc25bd387d7720eaedd3bc032f 192.168.136.128:7003
slots:[5461-10922] (5462 slots) master
645911442926600193 additional replica(s)
S: ba1cf9c11c90adfe385b2cb328cd4f5beb18d8f0 192.168.136.128:7004
slots: (0 slots) slave
replicates 5f386c6814f9abdc25bd387d7720eaedd3bc032f
M: ed1cf236e4336cc7dd0f1c201c914013d9f9d98b 192.168.136.128:7005
slots:[10923-16383] (5461 slots) master
645915325577035777 additional replica(s)
S: 606ad6d26baeca2c8e4a12ea06781ca8e17fcfdd 192.168.136.128:7006
slots: (0 slots) slave
replicates ed1cf236e4336cc7dd0f1c201c914013d9f9d98b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
第五步:查看伪集群信息。
[root@localhost cluster]# ./redis-5.0.4/src/redis-cli -h 192.168.136.128 -p 7001
192.168.136.128:7001> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.136.128,port=7002,state=online,offset=266,lag=0
master_replid:d94aaa1bce25e74d96a6cadcc0c07fa0931f2260
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:266
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:266
第六步:关闭与启动
关闭:ps -ef|grep xxx查找出redis进程;kill -9 pid 强制杀死进程。
[root@localhost cluster]# ps -ef|grep redis
root 7171 1 0 11:03 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7001 [cluster]
root 7176 1 0 11:03 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7002 [cluster]
root 7181 1 0 11:03 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7003 [cluster]
root 7187 1 0 11:03 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7004 [cluster]
root 7192 1 0 11:03 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7005 [cluster]
root 7198 1 0 11:03 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7006 [cluster]
root 7203 2599 0 11:04 pts/2 00:00:00 grep redis
[root@localhost cluster]# kill -9 7171
[root@localhost cluster]# kill -9 7176
[root@localhost cluster]# kill -9 7181
[root@localhost cluster]# kill -9 7187
[root@localhost cluster]# kill -9 7192
[root@localhost cluster]# kill -9 7198
[root@localhost cluster]# ps -ef|grep redis
root 7208 2599 0 11:06 pts/0 00:00:00 grep redis
启动:直接通过配置文件启动redis服务,./redis-5.0.4/src/redis-server 7001/redis7001.conf &
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7001/redis7001.conf &
[1] 2119
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7002/redis7002.conf &
[2] 2124
[1] Done ./redis-5.0.4/src/redis-server 7001/redis7001.conf
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7003/redis7003.conf &
[3] 2130
[2] Done ./redis-5.0.4/src/redis-server 7002/redis7002.conf
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7004/redis7004.conf &
[4] 2135
[3] Done ./redis-5.0.4/src/redis-server 7003/redis7003.conf
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7005/redis7005.conf &
[5] 2148
[4] Done ./redis-5.0.4/src/redis-server 7004/redis7004.conf
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7006/redis7006.conf &
[6] 2153
[5] Done ./redis-5.0.4/src/redis-server 7005/redis7005.conf
[root@localhost cluster]# ps -ef|grep redis
root 2120 1 0 11:49 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7001 [cluster]
root 2125 1 0 11:49 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7002 [cluster]
root 2131 1 0 11:49 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7003 [cluster]
root 2136 1 0 11:49 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7004 [cluster]
root 2149 1 0 11:50 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7005 [cluster]
root 2154 1 0 11:50 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7006 [cluster]
root 2160 2042 0 11:50 pts/0 00:00:00 grep redis
[6]+ Done ./redis-5.0.4/src/redis-server 7006/redis7006.conf
查看节点信息,发现主从节点信息跟之前创建集群时(可以查看上面创建集群日志)是一致的,因为节点信息等已经保存在 nodes-7001.conf 、redis_7001.pid等文件中。
[root@localhost cluster]# ./redis-5.0.4/src/redis-cli -h 192.168.136.128 -p 7001
192.168.136.128:7001> cluster nodes
ba1cf9c11c90adfe385b2cb328cd4f5beb18d8f0 192.168.136.128:7004@17004 slave 5f386c6814f9abdc25bd387d7720eaedd3bc032f 0 1563249132902 5 connected
606ad6d26baeca2c8e4a12ea06781ca8e17fcfdd 192.168.136.128:7006@17006 slave ed1cf236e4336cc7dd0f1c201c914013d9f9d98b 0 1563249133909 6 connected
5f386c6814f9abdc25bd387d7720eaedd3bc032f 192.168.136.128:7003@17003 master - 0 1563249132000 2 connected 5461-10922
ed1cf236e4336cc7dd0f1c201c914013d9f9d98b 192.168.136.128:7005@17005 master - 0 1563249134914 3 connected 10923-16383
b0f9136c77ad95517fc482182876c1a6f8a12110 192.168.136.128:7001@17001 myself,master - 0 1563249133000 1 connected 0-5460 [6918-<-5f386c6814f9abdc25bd387d7720eaedd3bc032f]
06f11443ea10d85949b89e5ab7c2f59ab0ae4e28 192.168.136.128:7002@17002 slave b0f9136c77ad95517fc482182876c1a6f8a12110 0 1563249133000 4 connected
当然,还有一种启动方式就是kill所有redis服务后,删除700x下除了redis700x.conf的其他文件,然后依次启动redis服务,再次搭建集群环境。
[root@localhost cluster]# ps -ef|grep redis
root 8768 8261 0 02:00 pts/0 00:00:00 grep redis
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7001/redis7001.conf &
[1] 8814
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7002/redis7002.conf &
[2] 8819
[1] Done ./redis-5.0.4/src/redis-server 7001/redis7001.conf
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7003/redis7003.conf &
[3] 8824
[2] Done ./redis-5.0.4/src/redis-server 7002/redis7002.conf
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7004/redis7004.conf &
[4] 8830
[3] Done ./redis-5.0.4/src/redis-server 7003/redis7003.conf
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7005/redis7005.conf &
[5] 8835
[4] Done ./redis-5.0.4/src/redis-server 7004/redis7004.conf
[root@localhost cluster]# ./redis-5.0.4/src/redis-server 7006/redis7006.conf &
[6] 8840
[5] Done ./redis-5.0.4/src/redis-server 7005/redis7005.conf
[root@localhost cluster]# ps -ef|grep redis
root 8815 1 0 02:06 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7001 [cluster]
root 8820 1 0 02:06 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7002 [cluster]
root 8825 1 0 02:06 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7003 [cluster]
root 8831 1 0 02:06 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7004 [cluster]
root 8836 1 0 02:07 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7005 [cluster]
root 8841 1 0 02:07 ? 00:00:00 ./redis-5.0.4/src/redis-server 0.0.0.0:7006 [cluster]
root 8846 8261 0 02:07 pts/0 00:00:00 grep redis
[6]+ Done ./redis-5.0.4/src/redis-server 7006/redis7006.conf
[root@localhost cluster]# ./redis-5.0.4/src/redis-cli --cluster-replicas 1 --cluster create 192.168.136.128:7001 192.168.136.128:7003 192.168.136.128:7005 192.168.136.128:7002 192.168.136.128:7004 192.168.136.128:7006
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.136.128:7004 to 192.168.136.128:7001
Adding replica 192.168.136.128:7006 to 192.168.136.128:7003
Adding replica 192.168.136.128:7002 to 192.168.136.128:7005
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 77e942817051f9cf220eccd16741fd1f7b21b413 192.168.136.128:7001
slots:[0-5460] (5461 slots) master
M: ea442fcebe30ee16cc2d6f9013d001c025a9aa10 192.168.136.128:7003
slots:[5461-10922] (5462 slots) master
M: 0cf998890fbd6dfabb56147981399e78c7b2b449 192.168.136.128:7005
slots:[10923-16383] (5461 slots) master
S: e0ccfc620af2be1edb48722b66beb9622e394c1c 192.168.136.128:7002
replicates 0cf998890fbd6dfabb56147981399e78c7b2b449
S: 0a88a19d957e6113da54c97baf025781f1f1c511 192.168.136.128:7004
replicates 77e942817051f9cf220eccd16741fd1f7b21b413
S: 40734497951e50d3ef753cc7317ba29bf53e40e4 192.168.136.128:7006
replicates ea442fcebe30ee16cc2d6f9013d001c025a9aa10
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
..
>>> Performing Cluster Check (using node 192.168.136.128:7001)
M: 77e942817051f9cf220eccd16741fd1f7b21b413 192.168.136.128:7001
slots:[0-5460] (5461 slots) master
13826323058169741313 additional replica(s)
S: 40734497951e50d3ef753cc7317ba29bf53e40e4 192.168.136.128:7006
slots: (0 slots) slave
replicates ea442fcebe30ee16cc2d6f9013d001c025a9aa10
S: e0ccfc620af2be1edb48722b66beb9622e394c1c 192.168.136.128:7002
slots: (0 slots) slave
replicates 0cf998890fbd6dfabb56147981399e78c7b2b449
S: 0a88a19d957e6113da54c97baf025781f1f1c511 192.168.136.128:7004
slots: (0 slots) slave
replicates 77e942817051f9cf220eccd16741fd1f7b21b413
M: 0cf998890fbd6dfabb56147981399e78c7b2b449 192.168.136.128:7005
slots:[10923-16383] (5461 slots) master
583691729178198017 additional replica(s)
M: ea442fcebe30ee16cc2d6f9013d001c025a9aa10 192.168.136.128:7003
slots:[5461-10922] (5462 slots) master
583692107135320065 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
如果有写的不对的地方,请大家多多批评指正,非常感谢!