redis 包含三种集群策略
- 主从复制
- 哨兵
- 集群
1. 主从配置
在主从复制中,数据库分为俩类,主数据库(master)和从数据库(slave)。其中主从复制有如下特点:
- 主数据库可以进行读写操作,当读写操作导致数据变化时会自动将数据同步给从数据库
- 从数据库一般都是只读的,并且接收主数据库同步过来的数据
- 一个master可以拥有多个slave,但是一个slave只能对应一个master
1.1 主从复制工作机制
当slave启动后,主动向master发送SYNC命令。master接收到SYNC命令后在后台保存快照(RDB持久化)和缓存保存快照这段时间的命令,然后将保存的快照文件和缓存的命令发送给slave。slave接收到快照文件和命令后加载快照文件和缓存的执行命令。
复制初始化后,master每次接收到的写命令都会同步发送给slave,保证主从数据一致性。
当数据量变得庞大的时候,读写分离还是很有必要的。同时避免一个redis服务宕机,导致应用宕机的情况,我们启用sentinel(哨兵)服务,实现主从切换的功能。redis提供了一个master,多个slave的服务。
1.2 主从配置
准备三个redis服务,依次命名文件夹子master,slave1,slave2.这里为在测试机上,不干扰原来的redis服务,我们master使用6000端口。
配置文件(redis.conf):
master配置修改端口:
port 6000
requirepass 123456
slave1修改配置:
port 6001
slaveof 127.0.0.1 6000
masterauth 123456
requirepass 123456
slave2修改配置:
port 6002
slaveof 127.0.0.1 6000
masterauth 123456
requirepass 123456
requirepass:是认证密码,应该之后要作主从切换,所以建议所有的密码都一致。masterauth是从机对主机验证时,所需的密码(即主机的requirepass)。
启动主机:
redis-server redis.conf
启动从机:
redis-server redis1.conf
redis-server redis2.conf
产看启动进程,输入:
ps -ef |grep redis
root 6617 1 0 18:34 ? 00:00:01 redis-server *:6000
root 6647 1 0 18:43 ? 00:00:00 redis-server *:6001
root 6653 1 0 18:43 ? 00:00:00 redis-server *:6002
root 6658 6570 0 18:43 pts/0 00:00:00 grep redis
可以看到主,从机的redis已经相应启动。
1.3. 验证主从复制
master:
[root@localhost master]# redis-cli -p 6000
127.0.0.1:6000> auth 123456
OK
127.0.0.1:6000> set test chenqm
OK
slave1:
[root@localhost slave2]# redis-cli -p 6001
127.0.0.1:6001> auth 123456
OK
127.0.0.1:6001> get test
"chenqm"
slave2:
[root@localhost slave2]# redis-cli -p 6002
127.0.0.1:6002> auth 123456
OK
127.0.0.1:6002> get test
"chenqm"
可以看到主机执行写命令,从机能同步主机的值,主从复制,读写分离就实现了。
但是万一主机挂了怎么办,这是个麻烦事情,所以redis提供了一个sentinel(哨兵),以此来实现主从切换的功能,类似与zookeeper。
2. Redis哨兵(sentinel)实现主从切换:
2.1. 环境
准备两台主机 10.211.55.6 、10.211.55.7 分别作为master、slave
IP | Port | Info |
---|---|---|
10.211.55.6 | 7000 | master |
10.211.55.6 | 26379 | sentinel |
10.211.55.7 | 8000 | slave |
10.211.55.7 | 26379 | sentinel |
2.2. 搭建master主机
2.2.1. master服务
配置文件
$ cat redis_7000.conf
daemonize yes
pidfile "/usr/local/redis/pid/redis_7000.pid"
port 7000
requirepass "123456"
masterauth "123456"
bind 10.211.55.6
logfile "/usr/local/redis/log/redis_7000.log"
dbfilename "dump_7000.rdb"
dir "/usr/local/redis/rdb"
slave-serve-stale-data yes
slave-read-only yes
slave-priority 100
maxmemory 200m
appendonly no
启动服务
$ redis-server ./redis_7000.conf
查看主从信息
$ redis-cli -h 10.211.55.6 -p 7000 -a 123456
10.211.55.6:7000> info replication
# Replication
role:master
connected_slaves:0
master_replid:dd925712190b873337a9117de9936a055cb9cea0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1511636
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
2.2.2. sentinel服务
配置文件
$ cat sentinel.conf
port 26379 # 监听端口
daemonize yes # 后台运行
logfile "/usr/local/redis/log/sentinel.log" # 日志文件
sentinel myid efb887a5aa76ef9dc2d5f84fee22753ab1b2060a
sentinel monitor mymaster 10.211.55.6 7000 2 # 指定master服务,后面的数字表示,当有几个节点认为主节点down时才认为主节点进入ODOWN状态,就是真正挂了
dir "/usr/local/redis"
sentinel down-after-milliseconds mymaster 5000 # 当多久,连接不上节点时,认为被连接节点进入S_DOWN(主观认为它down了);
sentinel failover-timeout mymaster 15000 #这个配置有很多作用。1、重新执行failover的时间是该值的2倍;2、取消一个没更改配置的failover3、failover中等待所有slave更改新的配置的最大时间。
sentinel auth-pass mymaster 123456 # 校验密码
启动服务
$ redis-server ./sentinel.conf --sentinel
查看日志
$ cat sentinel.log
30224:X 15 Mar 12:24:15.930 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30224:X 15 Mar 12:24:15.930 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=30224, just started
30224:X 15 Mar 12:24:15.930 # Configuration loaded
30225:X 15 Mar 12:24:15.932 * Increased maximum number of open files to 10032 (it was originally set to 1024).
30225:X 15 Mar 12:24:15.934 * Running mode=sentinel, port=26379.
30225:X 15 Mar 12:24:15.934 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30225:X 15 Mar 12:24:15.939 # Sentinel ID is efb887a5aa76ef9dc2d5f84fee22753ab1b2060a
30225:X 15 Mar 12:24:15.939 # +monitor master mymaster 10.211.55.6 7000 quorum 2
查看sentinel状态
$ redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.211.55.6:7000,slaves=0,sentinels=1
2.3.搭建slave主机
2.3.1. slave服务
配置文件
$ cat redis_8000.conf
daemonize yes
pidfile "/usr/local/redis/pid/redis_8000.pid"
port 8000
requirepass "123456"
masterauth "123456"
bind 10.211.55.7
logfile "/usr/local/redis/log/redis_8000.log"
dbfilename "dump_8000.rdb"
dir "/usr/local/redis/rdb"
slave-serve-stale-data yes
slave-read-only yes
slave-priority 100
maxmemory 200m
appendonly no
启动服务
$ redis-server ./redis_8000.conf
查看主从信息
$ redis-cli -h 10.211.55.7 -p 8000 -a 123456
10.211.55.7:8000> info replication
# Replication
role:master
connected_slaves:0
master_replid:d3b70185641129751fb336576c33939521226eb1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1511636
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
配置主从
10.211.55.7:8000> SLAVEOF 10.211.55.6 7000
OK
10.211.55.7:8000> info replication
# Replication
role:slave
master_host:10.211.55.6
master_port:7000
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:1511796
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:e046b3fa9a457b1964d3f8993f285bd2c0c629aa
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1511796
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1511637
repl_backlog_histlen:160
10.211.55.7:8000>
2.3.2. sentinel服务
$ cat sentinel.conf
port 26379
bind 10.211.55.7 127.0.0.1
daemonize yes
logfile "/usr/local/redis/log/s.log"
sentinel monitor mymaster 10.211.55.6 7000 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 15000
dir "/usr/local/redis/etc"
sentinel auth-pass mymaster 123456
$ redis-server ./sentinel.conf --sentinel
$ cat sentinel.log
16020:X 14 Mar 19:09:01.817 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
16020:X 14 Mar 19:09:01.817 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=16020, just started
16020:X 14 Mar 19:09:01.817 # Configuration loaded
16021:X 14 Mar 19:09:01.820 * Increased maximum number of open files to 10032 (it was originally set to 1024).
16021:X 14 Mar 19:09:01.821 * Running mode=sentinel, port=26379.
16021:X 14 Mar 19:09:01.821 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
16021:X 14 Mar 19:09:01.823 # Sentinel ID is d10dda888de0bf0cd3e0474ebfe35628562534da
16021:X 14 Mar 19:09:01.823 # +monitor master mymaster 10.211.55.6 7000 quorum 2
16021:X 14 Mar 19:09:01.827 * +slave slave 10.211.55.7:8000 10.211.55.7 8000 @ mymaster 10.211.55.6 7000
16021:X 14 Mar 19:09:02.556 * +sentinel sentinel efb887a5aa76ef9dc2d5f84fee22753ab1b2060a 10.211.55.6 26379 @ mymaster 10.211.55.6 7000
2.4. 验证
-
停掉 master redis服务,查看sentilen日志
30225:X 15 Mar 13:05:32.403 # +sdown master mymaster 10.211.55.6 7000 30225:X 15 Mar 13:05:32.494 # +odown master mymaster 10.211.55.6 7000 #quorum 2/2 30225:X 15 Mar 13:05:32.494 # +new-epoch 1 30225:X 15 Mar 13:05:32.494 # +try-failover master mymaster 10.211.55.6 7000 30225:X 15 Mar 13:05:32.498 # +vote-for-leader efb887a5aa76ef9dc2d5f84fee22753ab1b2060a 1 30225:X 15 Mar 13:05:32.504 # d10dda888de0bf0cd3e0474ebfe35628562534da voted for efb887a5aa76ef9dc2d5f84fee22753ab1b2060a 1 30225:X 15 Mar 13:05:32.572 # +elected-leader master mymaster 10.211.55.6 7000 30225:X 15 Mar 13:05:32.572 # +failover-state-select-slave master mymaster 10.211.55.6 7000 30225:X 15 Mar 13:05:32.644 # +selected-slave slave 10.211.55.7:8000 10.211.55.7 8000 @ mymaster 10.211.55.6 7000 30225:X 15 Mar 13:05:32.644 * +failover-state-send-slaveof-noone slave 10.211.55.7:8000 10.211.55.7 8000 @ mymaster 10.211.55.6 7000 30225:X 15 Mar 13:05:32.703 * +failover-state-wait-promotion slave 10.211.55.7:8000 10.211.55.7 8000 @ mymaster 10.211.55.6 7000 30225:X 15 Mar 13:05:33.521 # +promoted-slave slave 10.211.55.7:8000 10.211.55.7 8000 @ mymaster 10.211.55.6 7000 30225:X 15 Mar 13:05:33.521 # +failover-state-reconf-slaves master mymaster 10.211.55.6 7000 30225:X 15 Mar 13:05:33.571 # +failover-end master mymaster 10.211.55.6 7000 30225:X 15 Mar 13:05:33.572 # +switch-master mymaster 10.211.55.6 7000 10.211.55.7 8000
通过日志可看出来,当主节点挂了后,判断为
sdown
,当满足两台sentilen判断为挂了后,进入odown
,进入重新选择master节点。-
sdown
10.211.55.6 7000 给定的实例现在处于主观下线状态。
-
odown
10.211.55.6 7000 给定的实例现在处于客观下线状态。
-
new-epoch
当前的纪元(epoch)已经被更新。
-
try-failover
一个新的故障迁移操作正在执行中,等待被大多数 Sentinel 选中
-
elected-leader
赢得指定纪元的选举,可以进行故障迁移操作了。
-
failover-state-select-slave
故障转移操作现在处于 select-slave 状态 —— Sentinel 正在寻找可以升级为主服务器的从服务器
-
selected-slave
Sentinel 顺利找到适合进行升级的从服务器。
-
failover-state-send-slaveof-noone
Sentinel 正在将指定的从服务器升级为主服务器,等待升级功能完成。
-
failover-state-reconf-slaves
故障转移状态切换到了 reconf-slaves 状态。
-
failover-end
故障转移操作顺利完成。所有从服务器都开始复制新的主服务器了。
-
switch-master
配置变更,主服务器的 IP 和地址已经改变
-
3. 集群
见集群整理