先了解一下哨兵都 做了什么工作:Redis 的 Sentinel 系統用於管理多個 Redis 服務器(instance), 該系統執行以下三個任務:
* 監控(Monitoring): Sentinel 會不斷地檢查你的主服務器和從服務器是否運作正常。
* 提醒(Notification): 當被監控的某個 Redis 服務器出現問題時, Sentinel 可以通過 API 向管理員或者其他應用程序發送通知。
* 自動故障遷移(Automatic failover): 當一個主服務器不能正常工作時, Sentinel 會開始一次自動故障遷移操作, 它會將失效主服務器的其中一個從服務器升級為新的主服務器, 並讓失效主服務器的其他從服務器改為復制新的主服務器; 當客戶端試圖連接失效的主服務器時, 集群也會向客戶端返回新主服務器的地址, 使得集群可以使用新主服務器代替失效服務器。
Redis Sentinel 是一個分布式系統, 你可以在一個架構中運行多個 Sentinel 進程(progress), 這些進程使用流言協議(gossip protocols)來接收關於主服務器是否下線的信息, 並使用投票協議(agreement protocols)來決定是否執行自動故障遷移, 以及選擇哪個從服務器作為新的主服務器。
Redis Sentinel (哨兵)存在一個單獨的可執行文件 redis-sentinel , 但實際上它只是一個運行在特殊模式下的 Redis 服務器,通過Redis的哨兵去監控一個redis的集群,如果集群出現故障則自動進行故障遷移。
# 對於Redis Sentinel有兩種啟動方式,如下:
對於 redis-sentinel 程序, 你可以用以下命令來啟動 Sentinel 系統:
redis-sentinel /path/to/sentinel.conf
對於 redis-server 程序, 你可以用以下命令來啟動一個運行在 Sentinel 模式下的 Redis 服務器:
redis-server /path/to/sentinel.conf --sentinel
注意: 配置哨兵的前提是主從要先配置完成並運行。
主機名 IP地址 redis端口 哨兵端口
redis-1 10.10.120.113 6379 26379
redis-2 10.10.116.206 6379 26379
redis-3 10.10.48.62 6379 26379
主從配置與維護:
一、配置redis集群
**** 使用哨兵模式 先要搭建redis主從。****
### redis 配置文件修改:
## redis-1 Master 配置文件修改:
#bind 10.10.120.113 # 關閉了保護模式,這行就可以不需要。
daemonize yes # 默認為no 一定要打開
protected-mode no # 默認為yes,這里一定要改成no。關閉保護模式
port 6379
logfile "/data/logs/redis.logs"
databases 16
requirepass 123456 # 從服務密碼設置(訪問本機數據連接的Auth密碼)
masterauth 123456 # 若主服務設置了密碼需要加上,在設置哨兵時主從之間連接需要(變更主從需要連接master 的密碼.建議主從2個選項都設置上)
maxclients 10000
appendonly yes # 本地最好打開AOF模式,不會因為重啟丟失數據。
appendfilename "appendonly.aof"
## redis-2 slave 配置文件修改:
# config:
daemonize yes
protected-mode no
port 6379
logfile "/data/logs/redis.logs"
slaveof 10.10.120.113 6379 #Redis主節點IP 端口
requirepass 123456 # 客戶端連接redis需要用到的密碼
masterauth 123456 # 從庫連接主庫用到的密碼,類似mysql的主從同步賬號密碼。
appendonly yes # 本地最好打開AOF模式,不會因為重啟丟失數據。
appendfilename "appendonly.aof"
*** 一個主從有以下信息既可以成功建立:
slaveof 10.10.120.113 6379 #Redis主節點IP 端口
masterauth 123456
## 驗證主從:
[root@h-1 ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.10.116.206,port=6379,state=online,offset=1932,lag=0
slave1:ip=10.10.48.62,port=6379,state=online,offset=1932,lag=0
master_replid:57609f7b3e89bb5351ade82b965a9a9df0453bba
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1932
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1932
127.0.0.1:6379>
哨兵配置與維護:
如果要做 自動故障轉移,建議所有的 redis.conf 都設置 masterauth。因為 自動故障 只會重寫 主從關系,即 slaveof,不會自動寫入 masterauth。如果 Redis 原本沒有設置密碼,則可以忽略。
redis-1 sentinel 配置:
### sentinel 配置文件修改:
# sentinel-1
port 26379
daemonize yes #
pidfile "/var/run/redis-sentinel.pid"
logfile "/data/logs/sentinel.logs"
dir "/tmp"
sentinel myid 300fcc98aae7579f4f5f687454cfcc4787446e74
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 10.10.120.113 6379 1 # monitor 監控master IP地址和端口,最后的數字1 是有幾個哨兵確認即確認主下線。
sentinel auth-pass mymaster 123456 # 重點改這個選項,連接主的密碼。
sentinel config-epoch mymaster 24
sentinel leader-epoch mymaster 24
protected-mode no
sentinel down-after-milliseconds mymaster 5000 #修改心跳為5000毫秒
sentinel current-epoch 24
redis-2 sentinel 配置:
# sentinel-2
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/data/logs/sentinel.logs"
dir "/tmp"
sentinel myid 300fcc98aae7579f4f5f687454cfcc4787446e74
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 10.10.120.113 6379 1
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 24
sentinel leader-epoch mymaster 24
protected-mode no
sentinel down-after-milliseconds mymaster 5000 #修改心跳為5000毫秒
sentinel current-epoch 24
redis-3 sentinel 配置:
# sentinel-3
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/data/logs/sentinel.logs"
dir "/tmp"
sentinel myid edf0547582d9358fa95c6b4711945265b5ffa8a1
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 10.10.120.113 6379 1
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 24
sentinel leader-epoch mymaster 24
protected-mode no
sentinel current-epoch 24
sentinel down-after-milliseconds mymaster 5000 #修改心跳為5000毫秒
安全性
對於數據比較重要的節點,主節點會通過設置requirepass參數進行密碼 驗證,這時所有的客戶端訪問必須使用auth命令實行校驗。從節點與主節點 的復制連接是通過一個特殊標識的客戶端來完成,因此需要配置從節點的masterauth參數與主節點密碼保持一致,這樣從節點才可以正確地連接到主 節點並發起復制流程。
驗證sentinel 結果:
sentinel info信息:
[root@h-1 ~]# redis-cli -p 26379
127.0.0.1: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=10.10.50.119:6379,slaves=2,sentinels=3
127.0.0.1:26379>
查看monitor 信息,包含publish ping info 等信息:
[root@h-1 ~]# redis-cli -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> monitor
OK
1575018698.113399 [0 10.10.50.119:46434] "PING"
1575018698.243075 [0 10.10.50.119:46434] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26380,300fcc98aae7579f4f5f687454cfcc4787446e74,25,mymaster,10.10.50.119,6380,25"
1575018698.243192 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26380,300fcc98aae7579f4f5f687454cfcc4787446e74,25,mymaster,10.10.50.119,6380,25"
1575018698.539274 [0 10.10.50.119:46436] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26379,6654332ff2807ba9ac514f81f24b7dc261f12658,25,mymaster,10.10.50.119,6380,25"
1575018698.539447 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26379,6654332ff2807ba9ac514f81f24b7dc261f12658,25,mymaster,10.10.50.119,6380,25"
1575018698.572941 [0 10.10.50.119:46422] "PING"
1575018698.572949 [0 10.10.50.119:46422] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26381,edf0547582d9358fa95c6b4711945265b5ffa8a1,25,mymaster,10.10.50.119,6380,25"
1575018698.860119 [0 10.10.50.119:46436] "PING"
1575018699.127153 [0 10.10.50.119:46434] "PING"
1575018699.587644 [0 10.10.50.119:46422] "PING"
1575018699.796480 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26381,edf0547582d9358fa95c6b4711945265b5ffa8a1,25,mymaster,10.10.50.119,6380,25"
1575018699.888122 [0 10.10.50.119:46436] "PING"
1575018700.175129 [0 10.10.50.119:46434] "PING"
1575018700.299961 [0 10.10.50.119:46434] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26380,300fcc98aae7579f4f5f687454cfcc4787446e74,25,mymaster,10.10.50.119,6380,25"
1575018700.299989 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26380,300fcc98aae7579f4f5f687454cfcc4787446e74,25,mymaster,10.10.50.119,6380,25"
1575018700.300194 [0 10.10.50.119:46422] "INFO"
1575018700.569205 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26379,6654332ff2807ba9ac514f81f24b7dc261f12658,25,mymaster,10.10.50.119,6380,25"
1575018700.569235 [0 10.10.50.119:46436] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26379,6654332ff2807ba9ac514f81f24b7dc261f12658,25,mymaster,10.10.50.119,6380,25"
1575018700.622766 [0 10.10.50.119:46422] "PING"
1575018700.622785 [0 10.10.50.119:46422] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26381,edf0547582d9358fa95c6b4711945265b5ffa8a1,25,mymaster,10.10.50.119,6380,25"