Redis叢集一主二從三哨兵
阿新 • • 發佈:2021-07-22
Redis叢集一主二從三哨兵
redis叢集docker拓撲圖
搭建步驟
硬體分配
採用容器化部署
伺服器ip 10.0.43.61
容器名稱 | 埠 | 角色 |
---|---|---|
redis-6379 | 6379 | master |
redis-6380 | 6380 | slave |
redis-6381 | 6381 | slave |
redis-26379 | 26379 | sentinel |
redis-26380 | 26380 | sentinel |
redis-26381 | 26381 | sentinel |
準備工作
1.docker安裝步驟省略
2.pull映象
docker pull redis
3.映象拉下來的redis版本為
redis_version:6.2.4
主節點配置
vim /opt/redis/conf/6379.conf
#bind 0.0.0.0 protected-mode yes #埠 port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile "6379.log" dbfilename dump-6379.rdb #密碼 requirepass 1q2w3e4r masterauth 1q2w3e4r #物理主機ip,容器化部署必須填寫,否則預設為容器ip。 replica-announce-ip 10.0.43.61 databases 16 always-show-logo yes save "" stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dir ./ replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly yes appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 aof-rewrite-incremental-fsync yes
主節點啟動檔案
PORT=6379
docker stop redis-${PORT}
docker rm redis-${PORT}
docker run --name redis-${PORT} \
-p ${PORT}:${PORT} \
-v /opt/redis/conf/${PORT}.conf:/etc/redis/redis.conf \
-v /opt/redis/data:/data \
-d redis \
redis-server /etc/redis/redis.conf
從節點配置-slave1
vim /opt/redis/conf/6380.conf
#bind 0.0.0.0
protected-mode yes
#埠
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6380.pid
loglevel notice
logfile "6380.log"
dbfilename dump-6380.rdb
#master節點ip和埠
replicaof 10.0.43.61 6379
#密碼
requirepass 1q2w3e4r
masterauth 1q2w3e4r
#物理主機ip,容器化部署必須填寫,否則預設為容器ip。
replica-announce-ip 10.0.43.61
databases 16
always-show-logo yes
save ""
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
從節點啟動檔案-slave1
PORT=6380
docker stop redis-${PORT}
docker rm redis-${PORT}
docker run --name redis-${PORT} \
-p ${PORT}:${PORT} \
-v /opt/redis/conf/${PORT}.conf:/etc/redis/redis.conf \
-v /opt/redis/data:/data \
-d redis \
redis-server /etc/redis/redis.conf
從節點配置-slave2
vim /opt/redis/conf/6381.conf
#bind 0.0.0.0
protected-mode yes
#埠
port 6381
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6381.pid
loglevel notice
logfile "6381.log"
dbfilename dump-6381.rdb
#master節點ip埠
replicaof 10.0.43.61 6379
#密碼
requirepass 1q2w3e4r
masterauth 1q2w3e4r
#物理主機ip,容器化部署必須填寫,否則預設為容器ip。
replica-announce-ip 10.0.43.61
databases 16
always-show-logo yes
save ""
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
從節點啟動檔案-slave2
PORT=6381
docker stop redis-${PORT}
docker rm redis-${PORT}
docker run --name redis-${PORT} \
-p ${PORT}:${PORT} \
-v /opt/redis/conf/${PORT}.conf:/etc/redis/redis.conf \
-v /opt/redis/data:/data \
-d redis \
redis-server /etc/redis/redis.conf
檢視主從節點狀態
1.進入主節點容器
docker exec -it redis-6379 bash
2.通過密碼登入redis
redis-cli -a 1q2w3e4r
3.檢視主從資訊
info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.43.61,port=6380,state=online,offset=11653302,lag=1
slave1:ip=10.0.43.61,port=6381,state=online,offset=11653302,lag=0
master_failover_state:no-failover
master_replid:b9b77348370cf6a17b308271d0ab991813a169c8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:11653302
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:10604727
repl_backlog_histlen:1048576
4..進入從節點容器
docker exec -it redis-6380 bash
5.通過密碼登入redis
redis-cli -p 6380 -a 1q2w3e4r
6.檢視主從資訊
info replication
# Replication
role:slave
master_host:10.0.43.61
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:11698444
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:b9b77348370cf6a17b308271d0ab991813a169c8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:11698444
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:10649869
repl_backlog_histlen:1048576
上述一主二從就搭建成功了,接下來搭建三哨兵
主節點配置
vim /opt/redis/conf/sentinel-26379.conf
port 26379
logfile "26379.log"
sentinel myid 5fe8c1456f5080df3e485d44447e812e97ecd4d1
sentinel deny-scripts-reconfig yes
#物理主機ip
sentinel announce-ip 10.0.43.61
daemonize no
pidfile "/var/run/redis-sentinel.pid"
dir "/tmp"
#主節點ip埠,2表示2個節點投票認為掉線,才算掉線
sentinel monitor mymaster 10.0.43.61 6379 2
sentinel down-after-milliseconds mymaster 5000
#密碼
sentinel auth-pass mymaster 1q2w3e4r
主節點啟動檔案
vim start-sentinel-6379.sh
PORT=26379
docker stop redis-sentinel-${PORT}
docker rm redis-sentinel-${PORT}
docker run --name redis-sentinel-${PORT} \
-p ${PORT}:${PORT} \
-v /opt/redis/conf/sentinel-${PORT}.conf:/etc/redis/sentinel.conf \
-v /opt/redis/data:/data \
-d redis \
redis-sentinel /etc/redis/sentinel.conf
從節點配置-slave1
vim /opt/redis/conf/sentinel-26380.conf
port 26380
logfile "26380.log"
sentinel myid 5fe8c1456f5080df3e485d44447e812e97ecd4d2
sentinel deny-scripts-reconfig yes
sentinel announce-ip 10.0.43.61
daemonize no
pidfile "/var/run/redis-sentinel.pid"
dir "/tmp"
sentinel monitor mymaster 10.0.43.61 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel auth-pass mymaster 1q2w3e4r
從節點啟動檔案-slave1
vim start-sentinel-6380.sh
PORT=26380
docker stop redis-sentinel-${PORT}
docker rm redis-sentinel-${PORT}
docker run --name redis-sentinel-${PORT} \
-p ${PORT}:${PORT} \
-v /opt/redis/conf/sentinel-${PORT}.conf:/etc/redis/sentinel.conf \
-v /opt/redis/data:/data \
-d redis \
redis-sentinel /etc/redis/sentinel.conf
從節點配置-slave2
vim /opt/redis/conf/sentinel-26381.conf
port 26381
logfile "26381.log"
sentinel myid 5fe8c1456f5080df3e485d44447e812e97ecd4d3
sentinel deny-scripts-reconfig yes
sentinel announce-ip 10.0.43.61
daemonize no
pidfile "/var/run/redis-sentinel.pid"
dir "/tmp"
sentinel monitor mymaster 10.0.43.61 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel auth-pass mymaster 1q2w3e4r
從節點啟動檔案-slave2
vim start-sentinel-6381.sh
PORT=26381
docker stop redis-sentinel-${PORT}
docker rm redis-sentinel-${PORT}
docker run --name redis-sentinel-${PORT} \
-p ${PORT}:${PORT} \
-v /opt/redis/conf/sentinel-${PORT}.conf:/etc/redis/sentinel.conf \
-v /opt/redis/data:/data \
-d redis \
redis-sentinel /etc/redis/sentinel.conf
檢視哨兵狀態
1.進入主節點容器
docker exec -it redis-sentinel-26379 bash
2.登入redis
redis-cli -p 26379
3.檢視sentinel資訊
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=10.0.43.61:6379,slaves=2,sentinels=3
4.在主節點上獲取從節點資訊
sentinel slaves mymaster
1) 1) "name"
2) "10.0.43.61:6380"
3) "ip"
4) "10.0.43.61"
5) "port"
6) "6380"
7) "runid"
8) "7f0f5c18d4e5d4a00e21deb0a42266519c5451db"
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) "945"
19) "last-ping-reply"
20) "945"
21) "down-after-milliseconds"
22) "5000"
23) "info-refresh"
24) "426"
25) "role-reported"
26) "slave"
27) "role-reported-time"
28) "59392424"
29) "master-link-down-time"
30) "0"
31) "master-link-status"
32) "ok"
33) "master-host"
34) "10.0.43.61"
35) "master-port"
36) "6379"
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "11893527"
41) "replica-announced"
42) "1"
2) 1) "name"
2) "10.0.43.61:6381"
3) "ip"
4) "10.0.43.61"
5) "port"
6) "6381"
7) "runid"
8) "a5773e5a621f23754f2cc8a310ebbee3e79d83be"
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) "945"
19) "last-ping-reply"
20) "945"
21) "down-after-milliseconds"
22) "5000"
23) "info-refresh"
24) "1584"
25) "role-reported"
26) "slave"
27) "role-reported-time"
28) "59392420"
29) "master-link-down-time"
30) "0"
31) "master-link-status"
32) "ok"
33) "master-host"
34) "10.0.43.61"
35) "master-port"
36) "6379"
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "11893392"
41) "replica-announced"
42) "1"
5.進入從節點容器
docker exec -it redis-sentinel-26380 bash
2.登入redis
redis-cli -p 26380
3.檢視sentinel資訊
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=10.0.43.61:6379,slaves=2,sentinels=3
三哨兵搭建完成
spring boot 工程整合哨兵叢集
把單機的spring.redis.host和spring.redis.port配置改成以下
##### redis配置
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=10.0.43.61:26379,10.0.43.61:26380,10.0.43.61:26381
spring.redis.password=1q2w3e4r