準備環境:
master主機:192.168.52.139
slave主機1:192.168.52.141
slave主機2:192.168.52.142
實驗準備:
yum -y install redis
iptables -F
setenforce 0
修改每個主機的配置文件:
vim /etc/redis.conf
bind 0.0.0.0
requirepass kbq
啟動服務
systemctl start redis
確認各端口正常之后在slave節點上配置
slaveof 192.168.52.139 6379
auth kbq
連接至主節點查看復制信息
127.0.0.1:6379> info replication
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.52.142,port=6379,state=online,offset=407,lag=0
slave1:ip=192.168.52.141,port=6379,state=online,offset=407,lag=0
master_repl_offset:407
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:406
實驗:
在主節點上:
127.0.0.1:6379> set name hhhhh
OK
在從節點上:
127.0.0.1:6379> get name
"hhhhh
配置Sentinel系統
sentinel1
[root@sentinel ~]# vim /etc/redis-sentinel.conf
bind 192.168.52.139
sentinel auth-pass mymaster kbq
sentinel down-after-milliseconds mymaster 5000
[root@sentinel ~]# systemctl start redis-sentinel
[root@sentinel ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:26379 *:*
sentinel2
配置sentinel節點2:
[root@sentine2 ~]# vim /etc/redis-sentinel.conf
bind 192.168.52.141
sentinel monitor mymaster 192.168.52.139 6379 2
sentinel auth-pass mymaster kbq
sentinel down-after-milliseconds mymaster 5000
[root@sentine2 ~]# systemctl start redis-sentinel
[root@sentine2 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:26379 *:*
sentinel3
[root@sentine3 ~]# vim /etc/redis-sentinel.conf
bind 192.168.52.142
sentinel monitor mymaster 192.168.52.139 6379 2
sentinel auth-pass mymaster kbq
sentinel down-after-milliseconds mymaster 5000
[root@sentine3 ~]# systemctl start redis-sentinel
[root@sentine3 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:26379 *:*
sentinel查看主從節點信息并測試主從節點切換
[root@sentinel ~]# redis-cli -h 192.168.52.139 -p 26379
192.168.52.139:26379> SENTINEL masters
1) "name"
2) "mymaster" 主節點名稱
3) "ip"
4) "192.168.52.139"
5) "port"
6) "6379"
7) "runid"
8) "2847d8ceda8d3a03f0df814091fc5174921f57ea"
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) "198"
19) "last-ping-reply"
20) "198"
21) "down-after-milliseconds"
22) "3000"
23) "info-refresh"
24) "4299"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "4305"
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"
192.168.52.139:26379> SENTINEL slaves mymaster
1) "name"
2) "172.16.252.184:6379"
3) "ip"
4) "172.16.252.184"
5) "port"
6) "6379"
7) "runid"
8) ""
9) "flags"
10) "s_down,slave"
11) "link-pending-commands"
12) "14"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "233250"
17) "last-ok-ping-reply"
18) "233250"
19) "last-ping-reply"
20) "233250"
21) "s-down-time"
22) "230233"
23) "down-after-milliseconds"
24) "3000"
25) "info-refresh"
26) "1505553054438"
27) "role-reported"
28) "slave"
29) "role-reported-time"
30) "233250"
31) "master-link-down-time"
32) "0"
33) "master-link-status"
34) "err"
35) "master-host"
36) "?"
37) "master-port"
38) "0"
39) "slave-priority"
40) "100"
41) "slave-repl-offset"
42) "0"
2) 1) "name"
2) "172.16.252.67:6379"
3) "ip"
4) "172.16.252.67"
5) "port"
6) "6379"
7) "runid"
8) "fc91077ac51c954817a85922908320f7f70e2d61"
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) "374"
19) "last-ping-reply"
20) "374"
21) "down-after-milliseconds"
22) "3000"
23) "info-refresh"
24) "2509"
25) "role-reported"
26) "slave"
27) "role-reported-time"
28) "233250"
29) "master-link-down-time"
30) "0"
31) "master-link-status"
32) "ok"
33) "master-host"
34) "172.16.252.82"
35) "master-port"
36) "6379"
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "186072"
192.168.52.139:26379> SENTINEL failover mymaster
192.168.52.139:26379> SENTINEL masters 主節點由192.168.52.139切換到了192.168.52.141
1) 1) "name"
2) "mymaster"
3) "ip"
4) "192.168.52.141"
5) "port"
6) "6379"
7) "runid"
8) "fc91077ac51c954817a85922908320f7f70e2d61"
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) "37"
19) "last-ping-reply"
20) "37"
21) "down-after-milliseconds"
22) "3000"
23) "info-refresh"
24) "8118"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "27140"
29) "config-epoch"
30) "1"
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"
192.168.52.139:26379> SENTINEL slaves mymaster 顯示主節點名稱為mymaster的從節點屬性信息
1) 1) "name"
2) "192.168.52.142:6379"
3) "ip"
4) "192.168.52.142"
5) "port"
6) "6379"
7) "runid"
8) ""
9) "flags"
10) "s_down,slave"
11) "link-pending-commands"
12) "9"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "138676"
17) "last-ok-ping-reply"
18) "138676"
19) "last-ping-reply"
20) "138676"
21) "s-down-time"
22) "135668"
23) "down-after-milliseconds"
24) "3000"
25) "info-refresh"
26) "1505553462755"
27) "role-reported"
28) "slave"
29) "role-reported-time"
30) "138676"
31) "master-link-down-time"
32) "0"
33) "master-link-status"
34) "err"
35) "master-host"
36) "?"
37) "master-port"
38) "0"
39) "slave-priority"
40) "100"
41) "slave-repl-offset"
42) "0"
2) 1) "name"
2) "192.168.52.142:6379"
3) "ip"
4) "192.168.52.142"
5) "port"
6) "6379"
7) "runid"
8) "2847d8ceda8d3a03f0df814091fc5174921f57ea"
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) "363"
19) "last-ping-reply"
20) "363"
21) "down-after-milliseconds"
22) "3000"
23) "info-refresh"
24) "65"
25) "role-reported"
26) "slave"
27) "role-reported-time"
28) "118575"
29) "master-link-down-time"
30) "1505553462000"
31) "master-link-status"
32) "err"
33) "master-host"
34) "192.168.52.141"
35) "master-port"
36) "6379"
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "1"