本次搭建
a.普通主从
b.哨兵模式
架构
1 主 2 从
1.普通主从
安装的 redis 的步骤都相同,唯一不同的地方就是 slave 的配置文件要加入 slaveof 参数
vi /data/redis/redis.conf
添加如下内容:
daemonize yes
logfile "/data/redis/log/redis.log"
dir /data/redis/data
requirepass redis
masterauth redis
timeout 300
slaveof 192.168.65.2 6379
2 个 slave 的配置文件都这么配置,然后重启 redis
1.1检查状态
主库查状态
[redis@test2 ~]$ redis-cli -h 192.168.65.2 -a redis info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.65.3,port=6379,state=online,offset=1820,lag=1
slave1:ip=192.168.65.4,port=6379,state=online,offset=1820,lag=1
master_replid:b6e9ffd2f0e6bea54ee0336744bebbbf7601d5ad
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1820
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1820
从库查状态
[redis@test3 ~]$ redis-cli -h 192.168.65.3 -a redis info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.65.2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:1946
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b6e9ffd2f0e6bea54ee0336744bebbbf7601d5ad
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1946
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1946
1.2数据验证
主库添加数据
[redis@test2 ~]$ redis-cli -h 192.168.65.2 -a redis
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.65.2:6379> set t y
OK
192.168.65.2:6379> get t
"y"
192.168.65.2:6379>
从库检查
[redis@test3 ~]$ redis-cli -h 192.168.65.3 -a redis
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.65.3:6379> get t
"y"
192.168.65.3:6379>
到此,普通的 redis 主从搭建完成。
2.哨兵模式
2.1创建 sentinel 的配置文件
vi /data/redis/sentinel.conf
并添加如下内容:
sentinel monitor mymaster 192.168.65.2 6379 2 # 这里写 master 的IP
sentinel down-after-milliseconds mymaster 10000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 15000
bind 192.168.65.3 #本机IP
port 26379
daemonize yes
logfile "/data/redis/log/sentinel.log"
dir "/data/redis/data"
参数解释:
第一行: 2 表示最少需要几个哨兵发现主库down了之后,才会真正认定租库宕了
第二行: 10000 表示 10s,如果 10s 这个主库还没有响应,则表示宕了
第三行: 1 表示当新主库选出来后,允许几个从库同时从主库获取数据,这里是1,表示一次1个从库同步
数据,写太多怕对主库性能产生影响
第四行: 15000 表示 15s,如果 15s master 还没响应,则启动故障切换,选举新的主库
PS:1. 其他 slave 同样这么配置,只是把 bind 改为本机参数即可。
2. 这里 sentienl.conf 有模板,在软件包根目录下,可以参考配置其他参数。
2.2启动 sentinel
redis-sentinel /data/redis/sentinel.conf
检查服务状态
[redis@test2 redis]$ ps -ef|grep redis
root 2215 2044 0 03:48 pts/0 00:00:00 su - redis
redis 2216 2215 0 03:48 pts/0 00:00:00 -bash
redis 2300 1 0 07:07 ? 00:00:07 redis-server 192.168.65.2:6379
redis 2333 1 0 07:39 ? 00:00:04 redis-sentinel 192.168.65.2:26379 [sentinel]
redis 2347 2216 0 07:57 pts/0 00:00:00 ps -ef
redis 2348 2216 0 07:57 pts/0 00:00:00 grep redis
[redis@test2 redis]$ redis-cli -h 192.168.65.2 -p 26379 sentinel masters
1) 1) "name"
2) "mymaster"
3) "ip"
4) "192.168.65.2"
5) "port"
6) "6379"
7) "runid"
8) "6eacb4c0d199def0115501d8b959c6903d6d1ef9"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "448"
19) "last-ping-reply"
20) "448"
21) "down-after-milliseconds"
22) "10000"
23) "info-refresh"
24) "2217"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "1166603"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "15000"
39) "parallel-syncs"
40) "1"
[redis@test2 redis]$ redis-cli -h 192.168.65.2 -p 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=192.168.65.2:6379,slaves=2,sentinels=3
其他节点相同
2.3测试
1.kill 掉 192.168.65.2 上的 redis 及 sentinel,模拟主库宕机
2.在 65.3 上查看日志及 replication 、 sentinel 的状态
部分日志如下:
6530:X 12 May 2020 22:32:18.210 * +sentinel sentinel 6ebe2acac9bc556010e4d3ef2b565883fc4c87de 192.168.65.2 26379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 22:34:56.160 * +sentinel sentinel 84969158ae5a3530993c03a352639a15ab3dfc5b 192.168.65.4 26379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.066 # +sdown master mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.066 # +sdown sentinel 6ebe2acac9bc556010e4d3ef2b565883fc4c87de 192.168.65.2 26379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.122 # +odown master mymaster 192.168.65.2 6379 #quorum 2/2
6530:X 12 May 2020 23:03:50.122 # +new-epoch 1
6530:X 12 May 2020 23:03:50.122 # +try-failover master mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.124 # +vote-for-leader 1365d8a6519c56528bbce24b4d7dc1d5c97aa6ba 1
6530:X 12 May 2020 23:03:50.128 # 84969158ae5a3530993c03a352639a15ab3dfc5b voted for 1365d8a6519c56528bbce24b4d7dc1d5c97aa6ba 1
6530:X 12 May 2020 23:03:50.215 # +elected-leader master mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.215 # +failover-state-select-slave master mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.306 # +selected-slave slave 192.168.65.3:6379 192.168.65.3 6379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.306 * +failover-state-send-slaveof-noone slave 192.168.65.3:6379 192.168.65.3 6379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.359 * +failover-state-wait-promotion slave 192.168.65.3:6379 192.168.65.3 6379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.728 # +promoted-slave slave 192.168.65.3:6379 192.168.65.3 6379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.728 # +failover-state-reconf-slaves master mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.796 * +slave-reconf-sent slave 192.168.65.4:6379 192.168.65.4 6379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:51.226 # -odown master mymaster 192.168.65.2 6379
sentinel 的状态
[redis@test3 ~]$ redis-cli -h 192.168.65.3 -p 26379 sentinel masters
1) 1) "name"
2) "mymaster"
3) "ip"
4) "192.168.65.3"
5) "port"
6) "6379"
7) "runid"
8) "256fd6ada8a242022db7819fbba50a483de81adf"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "364"
19) "last-ping-reply"
20) "364"
21) "down-after-milliseconds"
22) "10000"
23) "info-refresh"
24) "364"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "1019"
29) "config-epoch"
30) "1"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "15000"
39) "parallel-syncs"
40) "1"
replication 的状态
[redis@test3 ~]$ redis-cli -h 192.168.65.3 -a redis info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.65.4,port=6379,state=online,offset=387662,lag=0
master_replid:31fa2c9c947d1b27605da5319642e9a4631ea26c
master_replid2:b6e9ffd2f0e6bea54ee0336744bebbbf7601d5ad
master_repl_offset:387662
second_repl_offset:386097
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:387662
可以看到都指向了新的 master ,192.168.65.3
3.在新 master 节点添加新的数据
[redis@test3 ~]$ redis-cli -h 192.168.65.3 -a redis
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.65.3:6379> set database redis
OK
192.168.65.3:6379> get database
"redis"
在 192.168.65.4 节点上查询
[redis@test4 ~]$ redis-cli -h 192.168.65.4 -p 6379 -a redis
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.65.4:6379> get database
"redis"
说明新的主从关系已经建立
4.重新恢复 192.168.65.2
检查sentinel及replication的装填,及后面新添加的数据是否同步过来
[redis@test2 redis]$ redis-cli -h 192.168.65.2 -p 6379 -a redis info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.65.3
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:460592
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:31fa2c9c947d1b27605da5319642e9a4631ea26c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:460592
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:457206
repl_backlog_histlen:3387
[redis@test2 redis]$ redis-cli -h 192.168.65.2 -p 26379 sentinel masters
1) 1) "name"
2) "mymaster"
3) "ip"
4) "192.168.65.3"
5) "port"
6) "6379"
7) "runid"
8) "256fd6ada8a242022db7819fbba50a483de81adf"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "412"
19) "last-ping-reply"
20) "412"
21) "down-after-milliseconds"
22) "10000"
23) "info-refresh"
24) "4459"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "24485"
29) "config-epoch"
30) "1"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "15000"
39) "parallel-syncs"
40) "1"
[redis@test2 redis]$ redis-cli -h 192.168.65.2 -p 6379 -a redis
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.65.2:6379> get database
"redis"
当 192.168.65.2 重新启动后,重新建立了主从关系,并且数据也同步完成
3. redis + sentinel 环境的启停操作
1.启动
先 redis 再 sentinel
主从都是如此
2.停止
先 redis 再 sentinel
主从都是如此
但 sentinel 的关闭只能用 kill 进程的方式