Redis搭建主从复制环境一主二从三哨兵
程序员文章站
2022-07-05 12:29:19
...
1、redis版本为6.2.1,redis安装步骤此处省略,可自行百度。这次搭建使用的是同一台虚拟机,启用多个redis实例。多虚拟机环境配置方法类似,修改端口和防火墙即可。
2、本机redis安装路径为/home/hl/redis,将目录下redis.conf文件复制三份到src目录,分别命名为redis-6379.conf,redis-6380.conf,redis-6381.conf。需要修改的内容如下,三个文件基本相同,主要是端口的区别。
# redis实例端口
port 6379
# 守护进程路径
pidfile "/home/hl/redis/run/redis_6379.pid"
# 日志路径
logfile "/home/hl/redis/logs/redis_6379.log"
# 数据库备份文件名称
dbfilename "dump-6379.rdb"
# 根目录
dir "/home/hl/redis"
# 连接master的密码
masterauth "123123"
# 连接master的用户名
masteruser "default"
# 本节点登录密码
requirepass 123123
# 开启守护进程方式
daemonize yes
3、复制redis目录下sentinel.conf到src目录下,分别命名为sentinel_26379.conf,sentinel_26380.conf,sentinel_26381.conf,需要修改的内容如下,三个文件基本一致,只是端口不同。
# 服务端口
port 26379
# 守护进程模式
daemonize yes
# 进程名称
pidfile "/home/hl/redis/run/redis-sentinel_26379.pid"
# 日志文件路径
logfile "/home/hl/redis/logs/sentinel_26379.log"
# 根目录
dir "/home/hl/redis/tmp"
# 监控的redis master节点名称、地址、端口、节点失效后选举切换master节点所需最少票数
sentinel monitor mymaster 127.0.0.1 6380 2
4、先启动redis三个节点,再启动哨兵,因为哨兵要查询redis节点进行监控。
[[email protected] src]$ ./redis-server ./redis-6379.conf
[[email protected] src]$ ./redis-server ./redis-6380.conf
[[email protected] src]$ ./redis-server ./redis-6381.conf
[[email protected] src]$ ./redis-sentinel ./sentinel_26379.conf
[[email protected] src]$ ./redis-sentinel ./sentinel_26380.conf
[[email protected] src]$ ./redis-sentinel ./sentinel_26381.conf
5、登录redis客户端,查看节点状态,slave节点。
[[email protected] src]$ ./redis-cli -p 6379
127.0.0.1:6379> auth 123123
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:7844
slave_priority:100
slave_read_only:1
connected_slaves:0
master_failover_state:no-failover
master_replid:46a4c92ad6e64aff68f820f17b69b9bdeb394d2c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:7844
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:7844
127.0.0.1:6379>
6、master节点。
[[email protected] src]$ ./redis-cli -p 6380
127.0.0.1:6380> auth 123123
OK
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6379,state=online,offset=26457,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=26723,lag=0
master_failover_state:no-failover
master_replid:46a4c92ad6e64aff68f820f17b69b9bdeb394d2c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:26723
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:26723
127.0.0.1:6380>
7、查看哨兵状态。
[[email protected] src]$ ./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=127.0.0.1:6380,slaves=2,sentinels=3
127.0.0.1:26379>
8、模拟master节点故障,即在master节点客户端执行shutdown命令,验证能否自动切换master,哨兵日志如下。
10404:X 26 Apr 2021 16:13:36.581 # -sdown master mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:13:41.708 * +convert-to-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:13:43.883 # -sdown slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:13:58.396 # -sdown sentinel 0b1b1713e6db1b755f12461385cfa5eff8d7b038 127.0.0.1 26380 @ mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:14:09.722 # -sdown sentinel 1a5ecfc41d7c5ec35ac73b450182bb0f8d504770 127.0.0.1 26381 @ mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:23:03.007 # +sdown master mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:23:03.074 # +odown master mymaster 127.0.0.1 6380 #quorum 2/2
10404:X 26 Apr 2021 16:23:03.074 # +new-epoch 9
10404:X 26 Apr 2021 16:23:03.074 # +try-failover master mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:23:03.077 # +vote-for-leader 7bd3f49f5bd67879b3f6be4192b33adf7c22c561 9
10404:X 26 Apr 2021 16:23:03.080 # 0b1b1713e6db1b755f12461385cfa5eff8d7b038 voted for 7bd3f49f5bd67879b3f6be4192b33adf7c22c561 9
10404:X 26 Apr 2021 16:23:03.080 # 1a5ecfc41d7c5ec35ac73b450182bb0f8d504770 voted for 7bd3f49f5bd67879b3f6be4192b33adf7c22c561 9
10404:X 26 Apr 2021 16:23:03.161 # +elected-leader master mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:23:03.161 # +failover-state-select-slave master mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:23:03.262 # +selected-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:23:03.262 * +failover-state-send-slaveof-noone slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:23:03.329 * +failover-state-wait-promotion slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:23:04.089 # +promoted-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:23:04.089 # +failover-state-reconf-slaves master mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:23:04.176 * +slave-reconf-sent slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:23:04.776 * +slave-reconf-inprog slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:23:04.776 * +slave-reconf-done slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:23:04.865 # +failover-end master mymaster 127.0.0.1 6380
10404:X 26 Apr 2021 16:23:04.865 # +switch-master mymaster 127.0.0.1 6380 127.0.0.1 6379
10404:X 26 Apr 2021 16:23:04.866 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
10404:X 26 Apr 2021 16:23:04.866 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
10404:X 26 Apr 2021 16:23:34.897 # +sdown slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
9、查看6379节点状态,已经切换为master。
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=109661,lag=1
master_failover_state:no-failover
master_replid:c37b0be050befe836cc95327258a8376e59a958d
master_replid2:46a4c92ad6e64aff68f820f17b69b9bdeb394d2c
master_repl_offset:110060
second_repl_offset:101736
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:110060
127.0.0.1:6379>