Redis-5.0.8 主從+ Sentinel 搭建

本次搭建

a.普通主從

b.哨兵模式

架構

1 主 2 從

1.普通主從

安裝的 redis 的步驟都相同,唯一不同的地方就是 slave 的配置文件要加入 slaveof 參數

vi /data/redis/redis.conf
添加如下內容:
daemonize yes
logfile "/data/redis/log/redis.log"
dir /data/redis/data
requirepass redis
masterauth redis
timeout 300
slaveof 192.168.65.2 6379

2 個 slave 的配置文件都這麼配置,然後重啓 redis 

 

1.1檢查狀態

主庫查狀態
[redis@test2 ~]$ redis-cli -h 192.168.65.2 -a redis info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.65.3,port=6379,state=online,offset=1820,lag=1
slave1:ip=192.168.65.4,port=6379,state=online,offset=1820,lag=1
master_replid:b6e9ffd2f0e6bea54ee0336744bebbbf7601d5ad
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1820
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1820


從庫查狀態
[redis@test3 ~]$ redis-cli -h 192.168.65.3 -a redis info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.65.2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:1946
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b6e9ffd2f0e6bea54ee0336744bebbbf7601d5ad
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1946
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1946

1.2數據驗證

主庫添加數據
[redis@test2 ~]$ redis-cli -h 192.168.65.2 -a redis
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.65.2:6379> set t y
OK
192.168.65.2:6379> get t
"y"
192.168.65.2:6379>

從庫檢查
[redis@test3 ~]$ redis-cli -h 192.168.65.3 -a redis 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.65.3:6379> get t
"y"
192.168.65.3:6379>

 到此,普通的 redis 主從搭建完成。
 

2.哨兵模式

2.1創建 sentinel 的配置文件

vi /data/redis/sentinel.conf
並添加如下內容:
sentinel monitor mymaster 192.168.65.2 6379 2    # 這裏寫 master 的IP
sentinel down-after-milliseconds mymaster 10000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 15000
bind 192.168.65.3   #本機IP
port 26379
daemonize yes
logfile "/data/redis/log/sentinel.log"
dir "/data/redis/data"


參數解釋:
第一行: 2 表示最少需要幾個哨兵發現主庫down了之後,纔會真正認定租庫宕了
第二行: 10000 表示 10s,如果 10s 這個主庫還沒有響應,則表示宕了
第三行: 1 表示當新主庫選出來後,允許幾個從庫同時從主庫獲取數據,這裏是1,表示一次1個從庫同步
        數據,寫太多怕對主庫性能產生影響
第四行: 15000 表示 15s,如果 15s master 還沒響應,則啓動故障切換,選舉新的主庫

PS:1. 其他 slave 同樣這麼配置,只是把 bind 改爲本機參數即可。
    2. 這裏 sentienl.conf 有模板,在軟件包根目錄下,可以參考配置其他參數。

2.2啓動 sentinel

redis-sentinel /data/redis/sentinel.conf
檢查服務狀態
[redis@test2 redis]$ ps -ef|grep redis
root       2215   2044  0 03:48 pts/0    00:00:00 su - redis
redis      2216   2215  0 03:48 pts/0    00:00:00 -bash
redis      2300      1  0 07:07 ?        00:00:07 redis-server 192.168.65.2:6379     
redis      2333      1  0 07:39 ?        00:00:04 redis-sentinel 192.168.65.2:26379 [sentinel]
redis      2347   2216  0 07:57 pts/0    00:00:00 ps -ef
redis      2348   2216  0 07:57 pts/0    00:00:00 grep redis



[redis@test2 redis]$ redis-cli -h 192.168.65.2 -p 26379 sentinel masters
1)  1) "name"
    2) "mymaster"
    3) "ip"
    4) "192.168.65.2"
    5) "port"
    6) "6379"
    7) "runid"
    8) "6eacb4c0d199def0115501d8b959c6903d6d1ef9"
    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) "448"
   19) "last-ping-reply"
   20) "448"
   21) "down-after-milliseconds"
   22) "10000"
   23) "info-refresh"
   24) "2217"
   25) "role-reported"
   26) "master"
   27) "role-reported-time"
   28) "1166603"
   29) "config-epoch"
   30) "0"
   31) "num-slaves"
   32) "2"
   33) "num-other-sentinels"
   34) "2"
   35) "quorum"
   36) "2"
   37) "failover-timeout"
   38) "15000"
   39) "parallel-syncs"
   40) "1"


[redis@test2 redis]$ redis-cli -h 192.168.65.2 -p 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=192.168.65.2:6379,slaves=2,sentinels=3


其他節點相同

2.3測試

1.kill 掉 192.168.65.2 上的 redis 及 sentinel,模擬主庫宕機

2.在 65.3 上查看日誌及 replication 、 sentinel 的狀態
部分日誌如下:
6530:X 12 May 2020 22:32:18.210 * +sentinel sentinel 6ebe2acac9bc556010e4d3ef2b565883fc4c87de 192.168.65.2 26379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 22:34:56.160 * +sentinel sentinel 84969158ae5a3530993c03a352639a15ab3dfc5b 192.168.65.4 26379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.066 # +sdown master mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.066 # +sdown sentinel 6ebe2acac9bc556010e4d3ef2b565883fc4c87de 192.168.65.2 26379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.122 # +odown master mymaster 192.168.65.2 6379 #quorum 2/2
6530:X 12 May 2020 23:03:50.122 # +new-epoch 1
6530:X 12 May 2020 23:03:50.122 # +try-failover master mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.124 # +vote-for-leader 1365d8a6519c56528bbce24b4d7dc1d5c97aa6ba 1
6530:X 12 May 2020 23:03:50.128 # 84969158ae5a3530993c03a352639a15ab3dfc5b voted for 1365d8a6519c56528bbce24b4d7dc1d5c97aa6ba 1
6530:X 12 May 2020 23:03:50.215 # +elected-leader master mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.215 # +failover-state-select-slave master mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.306 # +selected-slave slave 192.168.65.3:6379 192.168.65.3 6379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.306 * +failover-state-send-slaveof-noone slave 192.168.65.3:6379 192.168.65.3 6379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.359 * +failover-state-wait-promotion slave 192.168.65.3:6379 192.168.65.3 6379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.728 # +promoted-slave slave 192.168.65.3:6379 192.168.65.3 6379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.728 # +failover-state-reconf-slaves master mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:50.796 * +slave-reconf-sent slave 192.168.65.4:6379 192.168.65.4 6379 @ mymaster 192.168.65.2 6379
6530:X 12 May 2020 23:03:51.226 # -odown master mymaster 192.168.65.2 6379

sentinel 的狀態
[redis@test3 ~]$ redis-cli -h 192.168.65.3 -p 26379 sentinel masters
1)  1) "name"
    2) "mymaster"
    3) "ip"
    4) "192.168.65.3"
    5) "port"
    6) "6379"
    7) "runid"
    8) "256fd6ada8a242022db7819fbba50a483de81adf"
    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) "364"
   19) "last-ping-reply"
   20) "364"
   21) "down-after-milliseconds"
   22) "10000"
   23) "info-refresh"
   24) "364"
   25) "role-reported"
   26) "master"
   27) "role-reported-time"
   28) "1019"
   29) "config-epoch"
   30) "1"
   31) "num-slaves"
   32) "2"
   33) "num-other-sentinels"
   34) "2"
   35) "quorum"
   36) "2"
   37) "failover-timeout"
   38) "15000"
   39) "parallel-syncs"
   40) "1"

replication 的狀態
[redis@test3 ~]$ redis-cli -h 192.168.65.3 -a redis info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.65.4,port=6379,state=online,offset=387662,lag=0
master_replid:31fa2c9c947d1b27605da5319642e9a4631ea26c
master_replid2:b6e9ffd2f0e6bea54ee0336744bebbbf7601d5ad
master_repl_offset:387662
second_repl_offset:386097
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:387662

可以看到都指向了新的 master ,192.168.65.3

3.在新 master 節點添加新的數據
[redis@test3 ~]$ redis-cli -h 192.168.65.3 -a redis
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.65.3:6379> set database redis
OK
192.168.65.3:6379> get database
"redis"

在 192.168.65.4 節點上查詢
[redis@test4 ~]$ redis-cli -h 192.168.65.4 -p 6379 -a redis
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.65.4:6379> get database
"redis"

說明新的主從關係已經建立

4.重新恢復 192.168.65.2
檢查sentinel及replication的裝填,及後面新添加的數據是否同步過來
[redis@test2 redis]$ redis-cli -h 192.168.65.2 -p 6379 -a redis info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.65.3
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:460592
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:31fa2c9c947d1b27605da5319642e9a4631ea26c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:460592
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:457206
repl_backlog_histlen:3387
[redis@test2 redis]$ redis-cli -h 192.168.65.2 -p 26379 sentinel masters
1)  1) "name"
    2) "mymaster"
    3) "ip"
    4) "192.168.65.3"
    5) "port"
    6) "6379"
    7) "runid"
    8) "256fd6ada8a242022db7819fbba50a483de81adf"
    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) "412"
   19) "last-ping-reply"
   20) "412"
   21) "down-after-milliseconds"
   22) "10000"
   23) "info-refresh"
   24) "4459"
   25) "role-reported"
   26) "master"
   27) "role-reported-time"
   28) "24485"
   29) "config-epoch"
   30) "1"
   31) "num-slaves"
   32) "2"
   33) "num-other-sentinels"
   34) "2"
   35) "quorum"
   36) "2"
   37) "failover-timeout"
   38) "15000"
   39) "parallel-syncs"
   40) "1"

[redis@test2 redis]$ redis-cli -h 192.168.65.2 -p 6379 -a redis 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.65.2:6379> get database
"redis"

當 192.168.65.2 重新啓動後,重新建立了主從關係,並且數據也同步完成




3. redis + sentinel 環境的啓停操作

1.啓動
    先 redis 再 sentinel
    主從都是如此

2.停止
    先 redis 再 sentinel
    主從都是如此
    但 sentinel 的關閉只能用 kill 進程的方式

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章