1 2 3
| redis cluster: 自动master+slave复制和读写分离 master+slave高可用和主备切换 支持多个master的hash slot支持数据分布式存储
|
1 2 3
| 192.168.31.231 matrix-cache01 192.168.31.232 matrix-cache02 192.168.31.233 matrix-cache03
|
部署Redis Cluster集群前提
1
| 停止之前所有的实例,包括redis主从和哨兵集群
|
1 2 3 4 5 6
| [root@matrix-cache01 ~] root 1164 0.0 0.0 5976 764 pts/0 S+ 21:42 0:00 grep redis [root@matrix-cache02 ~] root 1087 0.0 0.0 5976 768 pts/0 S+ 21:43 0:00 grep redis [root@matrix-cache03 ~] root 1094 0.0 0.0 5976 768 pts/0 S+ 21:43 0:00 grep redis
|
redis cluster的重要配置
1 2 3 4 5 6 7 8 9 10 11 12
| cluster-enabled <yes/no>
cluster-config-file <filename>: 这是指定一个文件,供cluster模式下的redis实例将集群状态保存在那里 包括集群中其他机器的信息 比如节点的上线和下限,故障转移,不是我们去维护的 给它指定一个文件,让redis自己去维护的
cluster-node-timeout <milliseconds>: 节点存活超时时长,超过一定时长,认为节点宕机 master宕机的话就会触发主备切换 slave宕机就不会提供服务
|
在三台机器上启动6个redis实例
1 2 3 4 5 6 7 8 9 10 11 12
| redis cluster集群,要求至少3个master 去组成一个高可用,健壮的分布式的集群
每个master都建议至少给一个slave 3个master 3个slave 最少的要求
正式环境下,建议都是说在6台机器上去搭建,至少3台机器
保证,每个master都跟自己的slave不在同一台机器上 如果是6台自然更好,一个master + 一个slave就死了
|
3台机器搭建6个redis实例的redis cluster
1 2 3 4
| 至少要用3个master节点启动,每个master加一个slave节点,先选择6个节点,启动6个实例
将上面的配置文件,在/etc/redis下放6个 分别为: 7001.conf,7002.conf,7003.conf,7004.conf,7005.conf,7006.conf
|
matrix-cache01节点
1 2 3 4
| [root@matrix-cache01 ~] [root@matrix-cache01 ~] [root@matrix-cache01 ~] [root@matrix-cache01 ~]
|
[root@matrix-cache01 redis]# vi 7001.conf
1 2 3 4 5 6 7 8 9 10
| port 7001 cluster-enabled yes cluster-config-file /etc/redis-cluster/node-7001.conf cluster-node-timeout 5000 daemonize yes pidfile /var/run/redis_7001.pid dir /var/redis/7001 logfile /var/log/redis/7001.log bind 192.168.31.231 appendonly yes
|
[root@matrix-cache01 redis]# vi 7002.conf
1 2 3 4 5 6 7 8 9 10
| port 7002 cluster-enabled yes cluster-config-file /etc/redis-cluster/node-7002.conf cluster-node-timeout 5000 daemonize yes pidfile /var/run/redis_7002.pid dir /var/redis/7002 logfile /var/log/redis/7002.log bind 192.168.31.231 appendonly yes
|
1 2 3 4 5
| [root@matrix-cache01 redis] 总用量 12 -rw-r--r-- 1 root root 221 6月 24 11:50 6379.conf -rw-r--r-- 1 root root 261 6月 24 21:11 7001.conf -rw-r--r-- 1 root root 261 6月 24 21:10 7002.conf
|
1 2 3
| [root@matrix-cache01 ~] [root@matrix-cache01 init.d] [root@matrix-cache01 init.d]
|
1 2
| [root@matrix-cache01 init.d] REDISPORT 7001
|

1 2
| [root@matrix-cache01 init.d] REDISPORT 7002
|

1 2 3
| [root@matrix-cache01 ~] [root@matrix-cache01 init.d] [root@matrix-cache01 init.d]
|
1
| [root@matrix-cache01 init.d]
|

1
| [root@matrix-cache01 init.d]
|

matrix-cache02节点
1 2 3 4
| [root@matrix-cache02 ~] [root@matrix-cache02 ~] [root@matrix-cache02 ~] [root@matrix-cache02 ~]
|
[root@matrix-cache02 redis]# vi 7003.conf
1 2 3 4 5 6 7 8 9 10
| port 7003 cluster-enabled yes cluster-config-file /etc/redis-cluster/node-7003.conf cluster-node-timeout 5000 daemonize yes pidfile /var/run/redis_7003.pid dir /var/redis/7003 logfile /var/log/redis/7003.log bind 192.168.31.232 appendonly yes
|
[root@matrix-cache02 redis]# vi 7004.conf
1 2 3 4 5 6 7 8 9 10
| port 7004 cluster-enabled yes cluster-config-file /etc/redis-cluster/node-7004.conf cluster-node-timeout 5000 daemonize yes pidfile /var/run/redis_7004.pid dir /var/redis/7004 logfile /var/log/redis/7004.log bind 192.168.31.232 appendonly yes
|
1 2 3 4 5
| [root@matrix-cache02 redis] 总用量 12 -rw-r--r-- 1 root root 241 6月 24 11:39 6379.conf -rw-r--r-- 1 root root 261 6月 24 21:40 7003.conf -rw-r--r-- 1 root root 260 6月 24 21:40 7004.conf
|
1 2 3
| [root@matrix-cache02 ~] [root@matrix-cache02 init.d] [root@matrix-cache02 init.d]
|
1 2
| [root@matrix-cache02 init.d] REDISPORT 7003
|

1 2
| [root@matrix-cache02 init.d] REDISPORT 7004
|

1 2 3
| [root@matrix-cache02 ~] [root@matrix-cache02 init.d] [root@matrix-cache02 init.d]
|
1
| [root@matrix-cache02 init.d]
|

1
| [root@matrix-cache02 init.d]
|

matrix-cache03节点
1 2 3 4
| [root@matrix-cache03 ~] [root@matrix-cache03 ~] [root@matrix-cache03 ~] [root@matrix-cache03 ~]
|
[root@matrix-cache03 redis]# vi 7005.conf
1 2 3 4 5 6 7 8 9 10
| port 7005 cluster-enabled yes cluster-config-file /etc/redis-cluster/node-7005.conf cluster-node-timeout 5000 daemonize yes pidfile /var/run/redis_7005.pid dir /var/redis/7005 logfile /var/log/redis/7005.log bind 192.168.31.233 appendonly yes
|
[root@matrix-cache03 redis]# vi 7006.conf
1 2 3 4 5 6 7 8 9 10
| port 7006 cluster-enabled yes cluster-config-file /etc/redis-cluster/node-7006.conf cluster-node-timeout 5000 daemonize yes pidfile /var/run/redis_7006.pid dir /var/redis/7006 logfile /var/log/redis/7006.log bind 192.168.31.233 appendonly yes
|
1 2 3 4 5
| [root@matrix-cache03 redis] 总用量 12 -rw-rw-r-- 1 root root 214 6月 24 11:39 6379.conf -rw-r--r-- 1 root root 260 6月 24 21:41 7005.conf -rw-r--r-- 1 root root 261 6月 24 21:41 7006.conf
|
1 2 3
| [root@matrix-cache03 ~] [root@matrix-cache03 init.d] [root@matrix-cache03 init.d]
|
1 2
| [root@matrix-cache03 init.d] REDISPORT=7005
|

1 2
| [root@matrix-cache03 init.d] REDISPORT=7006
|

1 2 3
| [root@matrix-cache03 ~] [root@matrix-cache03 init.d] [root@matrix-cache03 init.d]
|
1
| [root@matrix-cache03 init.d]
|

1
| [root@matrix-cache03 init.d]
|

创建集群
1 2 3
| [root@matrix-cache01 ~] [root@matrix-cache01 ~] [root@matrix-cache01 ~]
|
1 2
| [root@matrix-cache01 ~] [root@matrix-cache01 local]
|


1
| [root@matrix-cache01 local]
|

1
| --replicas: 每个master有几个slave
|
1
| 6台机器,3个master,3个slave,尽量自己让master和slave不在一台机器上
|
集群效果
1 2 3
| 读写分离:每个master都有一个slave 高可用:master宕机,slave自动被切换过去 多master:横向扩容支持更大数据量
|