1. 程式人生 > >redis主從架構與redis+sentinel 哨兵機制架構搭建(生產可用)

redis主從架構與redis+sentinel 哨兵機制架構搭建(生產可用)

服務器 replicat rep 程序 tin dir fail minutes pwd

redis的搭建過程,請參考 https://blog.51cto.com/12445535/2385106

接下來,我們再找一臺服務器,進行安裝redis 實現redis的主從架構

和上面的方法搭建一個redis
只不過
在從redis中的配置文件中寫
[[email protected] conf]# grep slaveof 6379.conf
######## slaveof <masterip> <masterport>
slaveof 192.168.10.183 6379 //直接添加這個一行,然後

啟動redis
[[email protected] conf]# redis-server /ivargo/app/redis/conf/6379.conf

接下來就是看差距

在主redis可以看到
[[email protected] conf]# redis-cli -a XXX
Warning: Using a password with ‘-a‘ option on the command line interface may not be safe.
127.0.0.1:6379> info

Replication

role:master
connected_slaves:1

在從redis上可以看到
[[email protected] conf]# redis-cli -aXXX
Warning: Using a password with ‘-a‘ option on the command line interface may not be safe.

127.0.0.1:6379> info
####### Replication
role:slave
master_host:192.168.10.183
master_port:6379

這樣redis的主從就搭建好了

搭建完redis後,目錄結構

[[email protected] ivargo]# pwd
/ivargo
[[email protected] ivargo]# tree 
.
├── app
│?? ├── redis
│?? │?? ├── 6379.pid
│?? │?? ├── conf
│?? │?? │?? └── 6379.conf
│?? │?? ├── data
│?? │?? │?? ├── 6379.rdb
│?? │?? │?? └── appendonly_6379.aof
│?? │?? └── log
│?? └── sentinel
│??     └── conf
└── log
    └── 6379.log

8 directories, 5 files
實現redis的sentinel 
[[email protected] conf]# pwd
/ivargo/app/sentinel/conf
[[email protected] conf]# cat sentinel.conf  //這個配置文件是prd3上的sentinel的配置文件,我們需要修改的
# Example sentinel.conf
# bind 127.0.0.1 192.168.1.1
bind 0.0.0.0
protected-mode no
port 26379
daemonize yes
loglevel notice
logfile "/ivargo/log/sentinel.log"
dir "/tmp"
# sentinel auth-pass <master-name> <password>
# sentinel down-after-milliseconds <master-name> <milliseconds>
# Default is 30 seconds.
sentinel myid ba794535d3a65e14b266e28462fa7c68de609f39
# sentinel parallel-syncs <master-name> <numslaves>
sentinel deny-scripts-reconfig yes
# sentinel failover-timeout <master-name> <milliseconds>
# Default is 3 minutes = 180000.
sentinel monitor mymaster 10.80.85.20 6379 2
# Generated by CONFIG REWRITE
sentinel down-after-milliseconds mymaster 6000
sentinel auth-pass mymaster xxx
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 1
sentinel known-slave mymaster 10.80.85.21 6379
sentinel known-sentinel mymaster 10.80.85.21 26379 c3002e02c6a2d0041afd2569f3fd1985bc38993e
sentinel current-epoch 1
[[email protected] conf]# cat sentinel.conf  //這裏面都是我們自己指定的,他其他的文件內容,都是他自己生成的
# Example sentinel.conf
# bind 127.0.0.1 192.168.1.1
bind 0.0.0.0
protected-mode no
port 26379
daemonize yes
loglevel notice
logfile "/ivargo/log/sentinel.log"
pidfile "/ivargo/app/sentinel/26379.pid"
dir "/tmp"
sentinel monitor mymaster 192.168.10.183 6379 2
sentinel auth-pass mymaster XXX

184上的sentinel配置文件和183一樣的

[[email protected] conf]# redis-sentinel /ivargo/app/sentinel/conf/sentinel.conf
[[email protected] conf]# redis-sentinel /ivargo/app/sentinel/conf/sentinel.conf
[[email protected] conf]# cat sentinel.conf                                        
# Example sentinel.conf
# bind 127.0.0.1 192.168.1.1
bind 0.0.0.0
protected-mode no
port 26379
daemonize yes
loglevel notice
logfile "/ivargo/log/sentinel.log"
pidfile "/ivargo/app/sentinel/26379.pid"
dir "/tmp"
sentinel myid 6075d58bdc7c2602f98d90c0aa48470c4dbb50d9
sentinel deny-scripts-reconfig yes
# Generated by CONFIG REWRITE
sentinel monitor mymaster 192.168.10.183 6379 2
sentinel auth-pass mymaster XXX
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 192.168.10.184 6379
sentinel known-sentinel mymaster 192.168.10.184 26379 039bd11a572245b6c16c6e204523d781ffb6ba4c
sentinel current-epoch 0

查看不同點

我們檢查
[[email protected] conf]# redis-cli -h 192.168.10.183 -p 26379 info 
# 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.10.183:6379,slaves=1,sentinels=2
驗證sentinel高可用
[[email protected] redis]# kill `cat 6379.pid`
殺死183的進程
然後在184上看
[[email protected] conf]# redis-cli -h 192.168.10.183 -p 26379 info  
# 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.10.184:6379,slaves=1,sentinels=2  //主節點變成了184了

在啟動183
[[email protected] redis]# redis-server /ivargo/app/redis/conf/6379.conf 
[[email protected] redis]# redis-cli -h 192.168.10.183 -p 26379 info
# 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.10.184:6379,slaves=1,sentinels=2

然後查看redis 原來的183是主節點,現在是從節點了
[[email protected] redis]# redis-cli -h 192.168.10.183 -p 6379 -a XXX info 
# Replication
role:slave
master_host:192.168.10.184
master_port:6379

[[email protected] conf]# redis-cli -h 192.168.10.184 -p 6379 -a XXX info  
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.10.183,port=6379,state=online,offset=169470,lag=1

以上實現了redis的sentinel機制 也就是 HA機制

其實對於現上環境的話,當redis發生了主從切換的時候,這個比如程序調用的ip就會改變,寫兩個ip和兩個sentinel地址顯然是不合理的,我們可以實現vip機制自動切換,來實現程序調用只需要一個ip,相關內容,請期待。

redis主從架構與redis+sentinel 哨兵機制架構搭建(生產可用)