Redis搭建主从复制&高可用&多master的cluster集群

发布 : 2017-06-24 分类 : 大数据 浏览 :
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 ~]# ps aux | grep redis
root 1164 0.0 0.0 5976 764 pts/0 S+ 21:42 0:00 grep redis
[root@matrix-cache02 ~]# ps aux | grep redis
root 1087 0.0 0.0 5976 768 pts/0 S+ 21:43 0:00 grep redis
[root@matrix-cache03 ~]# ps aux | grep redis
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 ~]# mkdir -p /etc/redis-cluster
[root@matrix-cache01 ~]# mkdir -p /var/log/redis
[root@matrix-cache01 ~]# mkdir -p /var/redis/7001
[root@matrix-cache01 ~]# mkdir -p /var/redis/7002
1
[root@matrix-cache01 ~]# cd /etc/redis

[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]# ll
总用量 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 ~]# cd /etc/init.d
[root@matrix-cache01 init.d]# cp redis_6379 redis_7001
[root@matrix-cache01 init.d]# cp redis_6379 redis_7002
1
2
[root@matrix-cache01 init.d]# vi redis_7001
REDISPORT 7001

Markdown

1
2
[root@matrix-cache01 init.d]# vi redis_7002
REDISPORT 7002

Markdown

1
2
3
[root@matrix-cache01 ~]# cd /etc/init.d
[root@matrix-cache01 init.d]# ./redis_7001 start
[root@matrix-cache01 init.d]# ./redis_7002 start
1
[root@matrix-cache01 init.d]# cat /var/log/redis/7001.log

Markdown

1
[root@matrix-cache01 init.d]# cat /var/log/redis/7002.log

Markdown

matrix-cache02节点

1
2
3
4
[root@matrix-cache02 ~]# mkdir -p /etc/redis-cluster
[root@matrix-cache02 ~]# mkdir -p /var/log/redis
[root@matrix-cache02 ~]# mkdir -p /var/redis/7003
[root@matrix-cache02 ~]# mkdir -p /var/redis/7004
1
[root@matrix-cache02 ~]# cd /etc/redis

[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]# ll
总用量 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 ~]# cd /etc/init.d
[root@matrix-cache02 init.d]# cp redis_6379 redis_7003
[root@matrix-cache02 init.d]# cp redis_6379 redis_7004
1
2
[root@matrix-cache02 init.d]# vi redis_7003
REDISPORT 7003

Markdown

1
2
[root@matrix-cache02 init.d]# vi redis_7004
REDISPORT 7004

Markdown

1
2
3
[root@matrix-cache02 ~]# cd /etc/init.d
[root@matrix-cache02 init.d]# ./redis_7003 start
[root@matrix-cache02 init.d]# ./redis_7004 start
1
[root@matrix-cache02 init.d]# cat /var/log/redis/7003.log

Markdown

1
[root@matrix-cache02 init.d]# cat /var/log/redis/7004.log

Markdown

matrix-cache03节点

1
2
3
4
[root@matrix-cache03 ~]# mkdir -p /etc/redis-cluster
[root@matrix-cache03 ~]# mkdir -p /var/log/redis
[root@matrix-cache03 ~]# mkdir -p /var/redis/7005
[root@matrix-cache03 ~]# mkdir -p /var/redis/7006
1
[root@matrix-cache03 ~]# cd /etc/redis

[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]# ll
总用量 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 ~]# cd /etc/init.d
[root@matrix-cache03 init.d]# cp redis_6379 redis_7005
[root@matrix-cache03 init.d]# cp redis_6379 redis_7006
1
2
[root@matrix-cache03 init.d]# vi redis_7005
REDISPORT=7005

Markdown

1
2
[root@matrix-cache03 init.d]# vi redis_7006
REDISPORT=7006

Markdown

1
2
3
[root@matrix-cache03 ~]# cd /etc/init.d
[root@matrix-cache03 init.d]# ./redis_7005 start
[root@matrix-cache03 init.d]# ./redis_7006 start
1
[root@matrix-cache03 init.d]# cat /var/log/redis/7005.log

Markdown

1
[root@matrix-cache03 init.d]# cat /var/log/redis/7006.log

Markdown

创建集群

1
2
3
[root@matrix-cache01 ~]#yum install -y ruby
[root@matrix-cache01 ~]#yum install -y rubygems
[root@matrix-cache01 ~]#gem install redis
1
[root@matrix-cache01 ~]# cp redis-3.2.8/src/redis-trib.rb /usr/local/bin
1
2
[root@matrix-cache01 ~]# cd /usr/local
[root@matrix-cache01 local]# redis-trib.rb create --replicas 1 192.168.31.231:7001 192.168.31.231:7002 192.168.31.232:7003 192.168.31.232:7004 192.168.31.233:7005 192.168.31.233:7006

Markdown

Markdown

1
[root@matrix-cache01 local]# redis-trib.rb check 192.168.31.231:7001

Markdown

1
--replicas: 每个master有几个slave
1
6台机器,3个master,3个slave,尽量自己让master和slave不在一台机器上

集群效果

1
2
3
读写分离:每个master都有一个slave
高可用:master宕机,slave自动被切换过去
多master:横向扩容支持更大数据量
本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2017/06/24/Redis搭建主从复制&高可用&多master的cluster集群/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我投食

微信扫一扫, 向我投食

支付宝扫一扫, 向我投食

支付宝扫一扫, 向我投食

留下足迹