http://www.cnblogs.com/LiZhiW/p/4851631.htmlhtml
1.Linux服務器列表redis
使用4臺CentOS Linux服務器搭建環境,其IP地址以下:數據庫
192.168.110.100
192.168.110.101
192.168.110.102
192.168.110.103
2.Redis服務部署環境服務器
192.168.110.100
app
啓動多個Redis sentinel服務,構成Redis sentinel集羣
async
192.168.110.101
測試
啓動Redis服務,設置成主節點
this
192.168.110.102
spa
啓動Redis服務,設置成192.168.110.101的從節點
.net
192.168.110.103
啓動Redis服務,設置成192.168.110.101的從節點
1.修改redis.conf配置文件
主節點的redis配置文件使用默認的配置文件就能夠了,從節點的redis配置文件修改以下:
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# 1) Redis replication is asynchronous, but you can configure a master to
# stop accepting writes if it appears to be not connected with at least
# a given number of slaves.
# 2) Redis slaves are able to perform a partial resynchronization with the
# master if the replication link is lost for a relatively small amount of
# time. You may want to configure the replication backlog size (see the next
# sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
# network partition slaves automatically try to reconnect to masters
# and resynchronize with them.
#
# 主從同步。經過 slaveof 配置來實現Redis實例的備份。
# 注意,這裏是本地從遠端複製數據。也就是說,本地能夠有不一樣的數據庫文件、綁定不一樣的IP、監聽不一樣的端口。
#
# slaveof <masterip> <masterport>
slaveof 192.168.110.1016379
注意:兩臺從節點都要改。
2.啓動Redis主從集羣
先啓動192.168.110.101主節點,使用默認配置,腳本:
[wch@localhost bin]$ ./redis-server
再啓動192.168.110.102和192.168.110.103從節點,使用剛纔的配置,腳本:
./redis-server redis.conf
3.查看集羣
192.168.110.101主節點Replication信息
[wch@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.110.102,port=6379,state=online,offset=659,lag=1
slave1:ip=192.168.110.103,port=6379,state=online,offset=659,lag=0
master_repl_offset:659
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:658
192.168.110.102從節點Replication信息
[wch@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication
# Replication
role:slave
master_host:192.168.110.101
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:701
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
192.168.110.103從節點Replication信息
[wch@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication
# Replication
role:slave
master_host:192.168.110.101
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:715
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
此時,存儲到192.168.110.101主節點的數據,在從節點中均可以查詢到。從節點會備份主節點的數據。
1.建立sentinel.conf配置文件
#--後臺運行
#daemonize yes
port 26379
# sentinel announce-ip <ip>
# sentinel announce-port <port>
dir /tmp
################################# master001 #################################
sentinel monitor master001 192.168.110.10163792
# sentinel auth-pass <master-name> <password>
sentinel down-after-milliseconds master001 30000
sentinel parallel-syncs master001 1
sentinel failover-timeout master001 180000
# sentinel notification-script <master-name> <script-path>
# sentinel client-reconfig-script <master-name> <script-path>
# 能夠配置多個master節點
################################# master002 #################################
另外一個配置標例
# Example sentinel.conf
#--後臺運行
#daemonize yes
# port <sentinel-port>
# The port that this sentinel instance will run on
port 26305
dir "/tmp"
sentinel monitor master05 192.168.25.110 16306 1
sentinel down-after-milliseconds master05 5000
sentinel failover-timeout master05 5000
sentinel parallel-syncs master05 2
# Generated by CONFIG REWRITE
#sentinel config-epoch master05 0
#sentinel leader-epoch master05 0
#sentinel known-slave master05 192.168.25.110 16307
#sentinel known-slave master05 192.168.25.110 16306
#sentinel known-sentinel master05 192.168.25.110 26308 6d6b069753b48875861aa8f11f1971c131e743ba
#sentinel current-epoch 0
sentinel config-epoch master05 12
sentinel leader-epoch master05 12
sentinel known-slave master05 192.168.25.110 16307
sentinel known-slave master05 192.168.25.110 16305
sentinel known-sentinel master05 192.168.25.110 26308 e032e2baa1bb1d8699f3323f2affe9a734e1ecea
sentinel current-epoch 12
配置文件說明:
1. port :當前Sentinel服務運行的端口
2. dir : Sentinel服務運行時使用的臨時文件夾
3.sentinel monitor master001 192.168.110.10163792:Sentinel去監視一個名爲master001的主redis實例,這個主實例的IP地址爲本機地址192.168.110.101,端口號爲6379,而將這個主實例判斷爲失效至少須要2個 Sentinel進程的贊成,只要贊成Sentinel的數量不達標,自動failover就不會執行
4.sentinel down-after-milliseconds master001 30000:指定了Sentinel認爲Redis實例已經失效所需的毫秒數。當實例超過該時間沒有返回PING,或者直接返回錯誤,那麼Sentinel將這個實例標記爲主觀下線。只有一個 Sentinel進程將實例標記爲主觀下線並不必定會引發實例的自動故障遷移:只有在足夠數量的Sentinel都將一個實例標記爲主觀下線以後,實例纔會被標記爲客觀下線,這時自動故障遷移纔會執行
5.sentinel parallel-syncs master001 1:指定了在執行故障轉移時,最多能夠有多少個從Redis實例在同步新的主實例,在從Redis實例較多的狀況下這個數字越小,同步的時間越長,完成故障轉移所需的時間就越長
6.sentinel failover-timeout master001 180000:若是在該時間(ms)內未能完成failover操做,則認爲該failover失敗
7.sentinel notification-script <master-name> <script-path>:指定sentinel檢測到該監控的redis實例指向的實例異常時,調用的報警腳本。該配置項可選,可是很經常使用
2.啓動sentinel集羣
建立3個sentinel.conf配置文件:sentinel001.conf、sentinel002.conf、sentinel003.conf並修改端口號分別爲:2637九、3637九、46379,並啓動服務:
./redis-sentinel sentinel001.conf
./redis-sentinel sentinel002.conf
./redis-sentinel sentinel003.conf
啓動三個sentinel服務後會在其控制檯看到以下信息:
./redis-sentinel sentinel001.conf,端口:26379
[7743]01Oct06:20:38.162# Sentinel runid is ba6c42e1accc31290e11d5876275e1562564295d
[7743]01Oct06:20:38.162# +monitor master master001 192.168.110.101 6379 quorum 2
[7743]01Oct06:20:39.110*+slave slave 192.168.110.102:6379192.168.110.1026379@ master001 192.168.110.1016379
[7743]01Oct06:20:39.111*+slave slave 192.168.110.103:6379192.168.110.1036379@ master001 192.168.110.1016379
[7743]01Oct06:25:07.595*+sentinel sentinel 192.168.110.100:36379192.168.110.10036379@ master001 192.168.110.1016379
[7743]01Oct06:26:11.170*+sentinel sentinel 192.168.110.100:46379192.168.110.10046379@ master001 192.168.110.1016379
./redis-sentinel sentinel002.conf,端口:36379
[7795]01Oct06:25:05.538# Sentinel runid is 52c14768b15837fb601b26328acf150c6bd30682
[7795]01Oct06:25:05.538# +monitor master master001 192.168.110.101 6379 quorum 2
[7795]01Oct06:25:06.505*+slave slave 192.168.110.102:6379192.168.110.1026379@ master001 192.168.110.1016379
[7795]01Oct06:25:06.515*+slave slave 192.168.110.103:6379192.168.110.1036379@ master001 192.168.110.1016379
[7795]01Oct06:25:07.557*+sentinel sentinel 192.168.110.100:26379192.168.110.10026379@ master001 192.168.110.1016379
[7795]01Oct06:26:11.168*+sentinel sentinel 192.168.110.100:46379192.168.110.10046379@ master001 192.168.110.1016379
./redis-sentinel sentinel003.conf,端口:46379
[7828]01Oct06:26:09.076# Sentinel runid is c8509594be4a36660b2122b3b81f4f74060c9b04
[7828]01Oct06:26:09.076# +monitor master master001 192.168.110.101 6379 quorum 2
[7828]01Oct06:26:10.063*+slave slave 192.168.110.102:6379192.168.110.1026379@ master001 192.168.110.1016379
[7828]01Oct06:26:10.071*+slave slave 192.168.110.103:6379192.168.110.1036379@ master001 192.168.110.1016379
[7828]01Oct06:26:11.516*+sentinel sentinel 192.168.110.100:26379192.168.110.10026379@ master001 192.168.110.1016379
[7828]01Oct06:26:11.674*+sentinel sentinel 192.168.110.100:36379192.168.110.10036379@ master001 192.168.110.1016379
每一個sentinel服務能知道其餘全部的服務!
1.中止192.168.110.101主節點
中止192.168.110.101Redis主節點後,在查看Replication信息以下:
[wch@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication
Could not connect to Redis at 192.168.110.101:6379:Connection refused
[wch@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication
# Replication
role:slave
master_host:192.168.110.103
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:29128
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[wch@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.110.102,port=6379,state=online,offset=30456,lag=1
master_repl_offset:30456
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:30455
[wch@localhost bin]$
發現192.168.110.101Redis主節點已經不能鏈接,192.168.110.103成了主節點!
2.再啓動192.168.110.101主節點
再啓動192.168.110.101Redis主節點後,在查看Replication信息以下:
### 啓動腳本,仍然使用默認配置
[wch@localhost bin]$ ./redis-server
[wch@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication
# Replication
role:slave
master_host:192.168.110.103
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:57657
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[wch@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication
# Replication
role:slave
master_host:192.168.110.103
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:60751
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[wch@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.110.102,port=6379,state=online,offset=63247,lag=1
slave1:ip=192.168.110.101,port=6379,state=online,offset=63247,lag=1
master_repl_offset:63393
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:63392
[wch@localhost bin]$
發現192.168.110.101節點啓動後還再集羣中,只不過成了從節點,192.168.110.103仍然是主節點,可是如今又有兩個從節點了!
3.只留下一個sentinel服務,再中止192.168.110.103主節點,查看Redis集羣是否出現新的主節點
中止sentinel服務,只留下一個sentinel服務,再中止Redis主節點,查看Replication信息以下:
[wch@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication
# Replication
role:slave
master_host:192.168.110.103
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:184231
master_link_down_since_seconds:43
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[wch@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication
# Replication
role:slave
master_host:192.168.110.103
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:184231
master_link_down_since_seconds:52
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[wch@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication
Could not connect to Redis at 192.168.110.103:6379:Connection refused
發現192.168.110.103主節點已經不能鏈接了,也不存在Redis主節點,集羣中無主節點了!!!分析緣由是:sentinel.conf配置的sentinel monitor master001 192.168.110.10163792最後一個參數是2致使,如果但節點此配置的最後一個參數要使用是1。(此緣由我已證明)
注意:在生產環境下建議sentinel節點的數量能在3個以上,而且最好不要在同一臺機器上(使用同一網卡)。
-------------------------------------------------------------------------------------------------------------------------------