一,主从复制搭建
#创建多实例配置文件
[root@\ redis~]# cp /usr/local/redis/redis.conf /usr/local/redis/etc/redis6380.conf
[root@\ redis~]# cp /usr/local/redis/redis.conf /usr/local/redis/etc/redis6381.conf
# 都修改各自的端口分别为6379 6380 6381
[root@\ redis~]# vim /usr/local/redis/redis6380.conf
[root@\ redis~]# vim /usr/local/redis/redis6381.conf
daemonize yes
bind 0.0.0.0
port 6381
# 启动多实例
[root@\ redis~]# /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis6380.conf
[root@\ redis~]# /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis6381.conf
[root@\ redis~]# ps -ef | grep redis
root 6374 1 0 21:04 ? 00:00:01 /usr/local/redis/bin/redis-server 0.0.0.1:6379
root 6685 1 0 21:16 ? 00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6380
root 6691 1 0 21:16 ? 00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6381
root 6697 1877 0 21:16 pts/1 00:00:00 grep --color=auto redis
# 设置6380和6381两个节点加入到主节点6379端口
[root@\ redis~]# redis-cli -p 6380
127.0.0.1:6380> SLAVEOF 127.0.0.0 6379
OK
[root@\ redis~]# redis-cli -p 6381
127.0.0.1:6381> SLAVEOF 127.0.0.1 6379
OK
# 查看主节点
[root@\ redis~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=154,lag=0
master_failover_state:no-failover
master_replid:6af1964f199d63c93523cf3b68e217e3294b4d61
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_o
#从节点信息
[root@\ redis~]# redis-cli -p 6381
127.0.0.1:6381> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:42947
slave_priority:100
slave_read_only:1
connected_slaves:0
master_failover_state:no-failover
master_replid:f2f541b09561dc07bc93505297fb1ef9dd3124e0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:42947
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:42947
PS:
主节点宕机,并不能自动选取其他节点作为主节点,需要手动配置,这时需要哨兵
哨兵
# 编辑哨兵配置文件
[root@\ redis~]# vim /usr/local/redis/etc/sentinel26380.conf
port 26380
daemonize yes
pidfile "/usr/local/redis/data/redis-sentinel26380.pid"
logfile "/usr/local/redis/data/sentinel26380.log"
dir "/tmp"
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
[root@\ redis~]# vim /usr/local/redis/etc/sentinel26381.conf
port 26381
daemonize yes
pidfile "/usr/local/redis/data/redis-sentinel26381.pid"
logfile "/usr/local/redis/data/sentinel26381.log"
dir "/tmp"
sentinel monitor mymaster 127.0.0.1 6381 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
[root@\ redis~]# vim /usr/local/redis/etc/sentinel26379.conf
port 26379
daemonize yes
pidfile "/usr/local/redis/data/redis-sentinel26379.pid"
logfile "/usr/local/redis/data/sentinel26379.log"
dir "/tmp"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
#创建配置文件中需要的日志目录
[root@\ redis~]# mkdir /usr/local/redis/data
#启动哨兵
[root@\ redis~]# /usr/local/redis/bin/redis-sentinel /usr/local/redis/etc/sentinel26380.conf
[root@\ redis~]# /usr/local/redis/bin/redis-sentinel /usr/local/redis/etc/sentinel26381.conf
[root@\ redis~]# /usr/local/redis/bin/redis-sentinel /usr/local/redis/etc/sentinel26379.conf
[root@\ redis~]# ps -ef | grep redis
root 6685 1 0 21:16 ? 00:00:03 /usr/local/redis/bin/redis-server 0.0.0.0:6380
root 6897 1 0 21:29 ? 00:00:01 /usr/local/redis/bin/redis-sentinel *:26380 [sentinel]
root 6903 1 0 21:29 ? 00:00:01 /usr/local/redis/bin/redis-sentinel *:26381 [sentinel]
root 6919 1 0 21:30 ? 00:00:01 /usr/local/redis/bin/redis-sentinel *:26379 [sentinel]
root 16912 1 0 21:34 ? 00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root 17019 1 0 21:36 ? 00:00:00 /usr/local/redis/bin/redis-server 0.0.0.0:6381
# 查看主节点信息
[root@\ redis~]# redis-cli -p 26379
127.0.0.1:26379> SENTINEL master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6379"
7) "runid"
8) "6ef85e424d0e0999e2ce259f3e82f9aa43eb9587"
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) "1030"
19) "last-ping-reply"
20) "1030"
... ...
#这时候kill掉主节点,查看日志,主机点跳到了端口6381上
[root@\ redis~]# tail -f /usr/local/redis/data/sentinel26379.log
6919:X 30 Mar 2021 21:30:04.917 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
6919:X 30 Mar 2021 21:30:05.316 * +sentinel sentinel a8811e656c441bbfd51240689d7a4d4f13a8f9fd 127.0.0.1 26381 @ mymaster 127.0.0.1 6379
6919:X 30 Mar 2021 21:33:32.146 # +sdown master mymaster 127.0.0.1 6379
6919:X 30 Mar 2021 21:34:32.491 * +reboot master mymaster 127.0.0.1 6379
6919:X 30 Mar 2021 21:34:32.573 # -sdown master mymaster 127.0.0.1 6379
6919:X 30 Mar 2021 21:35:02.138 # +sdown slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
6919:X 30 Mar 2021 21:36:36.841 * +reboot slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
6919:X 30 Mar 2021 21:36:36.903 # -sdown slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
6919:X 30 Mar 2021 21:36:46.858 * +convert-to-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
6919:X 30 Mar 2021 21:40:22.517 # +sdown sentinel a8811e656c441bbfd51240689d7a4d4f13a8f9fd 127.0.0.1 26381 @ mymaster 127.0.0.1 6379
网友评论