Centos7+Redis6.0.5集群搭建(哨兵)

Centos7+Redis6.0.5集群搭建(哨兵)

我们已经介绍了Redis的主从复制,想必大家对redis已经有一个概念了,那么问题来了,如果redis主从复制的master服务器挂掉了,那么整体redis就崩溃了,因为master无法进行写数据,导致slave中无法更新数据。

那么为了解决这个问题我们就需要有一种方案让redis宕机后可以自动进行故障转移,还好redis给我们提供一种高可用解决方案 Redis-Sentinel。Redis-sentinel本身也是一个独立运行的进程,它能监控多个master-slave集群,发现master宕机后能进行自动切换。Sentinel可以监视任意多个主服务器
一、环境描述:
Centos7+Redis6.0.5集群搭建(哨兵)_第1张图片

二、Centos7下搭建Redis6.0.5服务
参考:Centos7下编译安装redis6.0.5(一)

三、配置主从
主6.101 redis master配置文件需要添加作为redis从服务器密码认证

masterauth "123456"

否则当master下线重新上线之后无法同步新的master数据,报错日志如下

1523:S 03 Dec 2020 09:27:07.846 * Connecting to MASTER 192.168.6.102:6379
1523:S 03 Dec 2020 09:27:07.846 * MASTER <-> REPLICA sync started
1523:S 03 Dec 2020 09:27:07.847 * Non blocking connect for SYNC fired the event.
1523:S 03 Dec 2020 09:27:07.847 * Master replied to PING, replication can continue...
1523:S 03 Dec 2020 09:27:07.848 * (Non critical) Master does not understand REPLCONF listening-port: -NOAUTH Authentication required.
1523:S 03 Dec 2020 09:27:07.848 * (Non critical) Master does not understand REPLCONF capa: -NOAUTH Authentication required.
1523:S 03 Dec 2020 09:27:07.849 * Partial resynchronization not possible (no cached master)
1523:S 03 Dec 2020 09:27:07.849 # Unexpected reply to PSYNC from master: -NOAUTH Authentication required.
1523:S 03 Dec 2020 09:27:07.849 * Retrying with SYNC...
1523:S 03 Dec 2020 09:27:07.850 # MASTER aborted replication with an error: NOAUTH Authentication required.

从602、603 的redis.conf配置文件增加以下两行

replicaof 192.168.6.101 6379
masterauth 123456

四、sentinel配置文件

port 26379
daemonize no
pidfile /var/run/redis-sentinel.pid
requirepass  123456 #生产环境需配置验证密码
logfile ""
dir /tmp
sentinel monitor mymaster 192.168.6.101 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel auth-pass mymaster glkajlkIWOEI123 

查看哨兵配置

port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/redis-sentinel.log"
dir "/tmp"
sentinel myid 97ee6a6f69806b87612ab5f3fac401bd780817c2
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.6.101 6379 2
sentinel auth-pass mymaster glkajlkIWOEI123
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0

# Generated by CONFIG REWRITE
protected-mode no
user default on nopass ~* +@all
sentinel known-replica mymaster 192.168.6.103 6379
sentinel known-replica mymaster 192.168.6.102 6379
sentinel known-sentinel mymaster 192.168.6.102 26379 5959d6f0bf2681969e10361ba20cd53398bf9913
sentinel known-sentinel mymaster 192.168.6.103 26379 013afc60e2ab962e32505e4bd749a07a99068472
sentinel current-epoch 0

查看哨兵日志:

15335:X 02 Dec 2020 17:22:30.038 # Redis version=6.0.5, bits=64, commit=00000000, modified=0, pid=15335, just started
15335:X 02 Dec 2020 17:22:30.038 # Configuration loaded
15336:X 02 Dec 2020 17:22:30.039 * Increased maximum number of open files to 10032 (it was originally set to 1024).
15336:X 02 Dec 2020 17:22:30.040 * Running mode=sentinel, port=26379.
15336:X 02 Dec 2020 17:22:30.040 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
15336:X 02 Dec 2020 17:22:30.040 # Sentinel ID is 5959d6f0bf2681969e10361ba20cd53398bf9913
15336:X 02 Dec 2020 17:22:30.040 # +monitor master mymaster 192.168.6.101 6379 quorum 2
15336:X 02 Dec 2020 17:22:30.043 * +slave slave 192.168.6.103:6379 192.168.6.103 6379 @ mymaster 192.168.6.101 6379
15336:X 02 Dec 2020 17:22:30.045 * +slave slave 192.168.6.102:6379 192.168.6.102 6379 @ mymaster 192.168.6.101 6379
15336:X 02 Dec 2020 17:22:30.245 * +sentinel sentinel 97ee6a6f69806b87612ab5f3fac401bd780817c2 192.168.6.101 26379 @ mymaster 192.168.6.101 6379
15336:X 02 Dec 2020 17:22:54.886 * +sentinel sentinel 013afc60e2ab962e32505e4bd749a07a99068472 192.168.6.103 26379 @ mymaster 192.168.6.101 6379

查看主节点状态

127.0.0.1:26379> sentinel master mymaster
 1) "name"
 2) "mymaster"
 3) "ip"
 4) "192.168.6.101"
 5) "port"
 6) "6379"
 7) "runid"
 8) "81ebf63566898c944b6dd3da4a89635b05c43bc5"
 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) "947"
19) "last-ping-reply"
20) "947"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "1818"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "142433"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"

查看副本状态

127.0.0.1:26379> SENTINEL replicas mymaster
1)  1) "name"
    2) "192.168.6.102:6379"
    3) "ip"
    4) "192.168.6.102"
    5) "port"
    6) "6379"
    7) "runid"
    8) "7970ae8f7bf3104afe999a4b8e72a24e81ffd989"
    9) "flags"
   10) "slave"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "278"
   19) "last-ping-reply"
   20) "278"
   21) "down-after-milliseconds"
   22) "30000"
   23) "info-refresh"
   24) "5118"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "175845"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "192.168.6.101"
   35) "master-port"
   36) "6379"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "42356"
2)  1) "name"
    2) "192.168.6.103:6379"
    3) "ip"
    4) "192.168.6.103"
    5) "port"
    6) "6379"
    7) "runid"
    8) "b8ee54d4252dfb5804eb8b60ae8dcf493b618f55"
    9) "flags"
   10) "slave"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "278"
   19) "last-ping-reply"
   20) "278"
   21) "down-after-milliseconds"
   22) "30000"
   23) "info-refresh"
   24) "5119"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "175846"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "192.168.6.101"
   35) "master-port"
   36) "6379"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "42356"

查看哨兵状态

127.0.0.1:26379> SENTINEL sentinels mymaster
1)  1) "name"
    2) "97ee6a6f69806b87612ab5f3fac401bd780817c2"
    3) "ip"
    4) "192.168.6.101"
    5) "port"
    6) "26379"
    7) "runid"
    8) "97ee6a6f69806b87612ab5f3fac401bd780817c2"
    9) "flags"
   10) "sentinel"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "913"
   19) "last-ping-reply"
   20) "913"
   21) "down-after-milliseconds"
   22) "30000"
   23) "last-hello-message"
   24) "1051"
   25) "voted-leader"
   26) "?"
   27) "voted-leader-epoch"
   28) "0"
2)  1) "name"
    2) "5959d6f0bf2681969e10361ba20cd53398bf9913"
    3) "ip"
    4) "192.168.6.102"
    5) "port"
    6) "26379"
    7) "runid"
    8) "5959d6f0bf2681969e10361ba20cd53398bf9913"
    9) "flags"
   10) "sentinel"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "913"
   19) "last-ping-reply"
   20) "913"
   21) "down-after-milliseconds"
   22) "30000"
   23) "last-hello-message"
   24) "887"
   25) "voted-leader"
   26) "?"
   27) "voted-leader-epoch"
   28) "0"
127.0.0.1:26379> 

演示故障转移
停掉6.101 3679端口,查看哨兵日志

[root@localhost redis-6.0.5]# tail -f /var/log/redis-sentinel.log 
1510:X 02 Dec 2020 17:22:54.885 * +sentinel sentinel 013afc60e2ab962e32505e4bd749a07a99068472 192.168.6.103 26379 @ mymaster 192.168.6.101 6379
1510:X 02 Dec 2020 17:28:16.207 # +sdown master mymaster 192.168.6.101 6379
1510:X 02 Dec 2020 17:28:16.283 # +new-epoch 1
1510:X 02 Dec 2020 17:28:16.285 # +vote-for-leader 013afc60e2ab962e32505e4bd749a07a99068472 1
1510:X 02 Dec 2020 17:28:16.611 # +config-update-from sentinel 013afc60e2ab962e32505e4bd749a07a99068472 192.168.6.103 26379 @ mymaster 192.168.6.101 6379
1510:X 02 Dec 2020 17:28:16.611 # +switch-master mymaster 192.168.6.101 6379 192.168.6.102 6379
1510:X 02 Dec 2020 17:28:16.612 * +slave slave 192.168.6.103:6379 192.168.6.103 6379 @ mymaster 192.168.6.102 6379
1510:X 02 Dec 2020 17:28:16.612 * +slave slave 192.168.6.101:6379 192.168.6.101 6379 @ mymaster 192.168.6.102 6379
1510:X 02 Dec 2020 17:28:46.692 # +sdown slave 192.168.6.101:6379 192.168.6.101 6379 @ mymaster 192.168.6.102 6379
1510:X 02 Dec 2020 17:32:05.662 # -sdown slave 192.168.6.101:6379 192.168.6.101 6379 @ mymaster 192.168.6.102 6379

日志看见6.101已经Down,6.102切换为master节点!
cat哨兵配置文件,从文件可以看出master已替换为6.101

[root@localhost redis-6.0.5]# cat /etc/redis/sentinel/sentinel.conf 
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/var/log/redis-sentinel.log"
dir "/tmp"
sentinel myid 5959d6f0bf2681969e10361ba20cd53398bf9913
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.6.102 6379 2
sentinel auth-pass mymaster glkajlkIWOEI123
sentinel config-epoch mymaster 1
sentinel leader-epoch mymaster 1

# Generated by CONFIG REWRITE
protected-mode no
user default on nopass ~* +@all
sentinel known-replica mymaster 192.168.6.103 6379
sentinel known-replica mymaster 192.168.6.101 6379
sentinel known-sentinel mymaster 192.168.6.101 26379 97ee6a6f69806b87612ab5f3fac401bd780817c2
sentinel known-sentinel mymaster 192.168.6.103 26379 013afc60e2ab962e32505e4bd749a07a99068472
sentinel current-epoch 1

6.102的redis配置文件

# Set bgsave child process to cpu affinity 1,10,11
# bgsave_cpulist 1,10-11

masterauth "glkajlkIWOEI123"
# Generated by CONFIG REWRITE
user default on #aeaf6fadb2823379ae6cea5ec0fbeeab92a6a5f4c453a94252ec41e663df294c ~* +@all

6.101的redis 配置文件
自动变更为了从服务器

# Generated by CONFIG REWRITE
user default on #aeaf6fadb2823379ae6cea5ec0fbeeab92a6a5f4c453a94252ec41e663df294c ~* +@all
replicaof 192.168.6.102 6379

参考:https://www.cnblogs.com/guolianyu/p/10249687.html#top

你可能感兴趣的:(redis)