redis:RDB AOF -master &slave -sentinel-cluster

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis:RDB AOF -master &slave -sentinel-cluster相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/replicate' title='replicate'>replicate   fsync   鏀寔   鍝ㄥ叺   run   閰嶇疆鏂囦欢   cpu   蹇収   鍐呭瓨鏁版嵁   

1銆丷DB鍜孉OF鐨勪紭缂虹偣

2銆乵aster鍜宻lave鍚屾杩囩▼

3銆佸摠鍏电殑浣跨敤鍜屽疄鐜版満鍒?br>4銆乺edis cluster闆嗙兢鍒涘缓鍜屼娇鐢?/p>

绗竴涓鐩€丷DB鍜孉OF鐨勪紭缂虹偣
涓€銆?RDB鐨勪紭鐐?br>1銆佷紭鐐筧锛歊DB 蹇収淇濆瓨浜嗘煇涓椂闂寸偣鐨勬暟鎹紝鍙互閫氳繃鑴氭湰鎵цredis鎸囦护bgsave锛堥潪闃诲锛屽悗鍙版墽琛岋級鎴栬€卻ave锛堜細闃诲鍐欐搷浣滐紝涓嶆帹鑽愶級鍛戒护鑷畾涔夋椂闂寸偣澶囦唤锛屽彲浠ヤ繚澶氫釜澶囦唤锛屽綋鍑虹幇闂鍙互鎭㈠鍒颁笉鍚屾椂闂寸偣鐨勭増鏈紝寰堥€傚悎澶囦唤骞朵笖姝ゆ枃浠舵牸寮忎篃鏈夋敮鎸佹湁涓嶅皯绗笁鏂瑰伐鍏峰彲浠ヨ繘琛屽悗缁殑鏁版嵁鍒嗘瀽銆?br> 姣斿锛氬彲浠ュ湪鏈€杩戠殑24灏忔椂鍐呮瘡灏忔椂澶囦唤涓€娆DB鏂囦欢锛屽苟涓斿湪姣忎竴澶╋紝涔熷浠戒竴涓猂DB鏂囦欢锛岃繖鏍风殑璇濆嵆浣块亣涓婇棶棰橈紝涔熷彲浠ラ殢鏃跺皢鏁版嵁闆嗚繕鍘熷埌涓嶅悓鐨勭増鏈€?br> 浼樼偣b锛歊DB鍙互鏈€澶у寲Redis 鐨勬€ц兘锛岀埗杩涚▼鍦ㄤ繚瀛楻DB鏂囦欢鏃跺敮涓€瑕佸仛鐨勫氨鏄痜ork鍑轰竴涓瓙杩涚▼锛岀劧鍚庤繖涓瓙杩涚▼灏变細澶勭悊鎺ヤ笅鏉ョ殑鎵€鏈変繚瀛樺伐浣滐紝鐖惰繘绋嬫棤闇€鎵ц浠讳綍纾佺洏I/O鎿嶄綔銆?br> 浼樼偣c锛歊DB鍦ㄥぇ閲忔暟鎹紝姣斿鍑犱釜G鐨勬暟鎹紝鎭㈠鐨勯€熷害姣擜OF蹇?br> 2銆丷DB鐨勭己鐐?br> 缂虹偣a锛氫笉鑳藉疄鏃朵繚瀛樻暟鎹紝鍙兘浼氫涪澶辫嚜涓婁竴娆℃墽琛孯DB澶囦唤鍒板綋鍓嶇殑鍐呭瓨鏁版嵁
濡傛灉浣犻渶瑕佸敖閲忛伩鍏嶅湪鏈嶅姟鍣ㄦ晠闅滄椂涓㈠け鏁版嵁锛岄偅涔圧DB涓嶉€傚悎浣犮€傝櫧鐒禦edis鍏佽浣犺缃笉鍚岀殑淇濆瓨鐐癸紙save point ) 鏉ユ帶鍒朵繚瀛楻DB鏂囦欢鐨勯鐜囷紝浣嗘槸锛屽洜涓篟D鏂囦欢闇€瑕佷繚瀛樻暣涓暟鎹泦鐨勭姸鎬侊紝鎵€浠ュ畠骞朵笉鏄竴涓交鏉剧殑鎿嶄綔銆傚洜姝や綘鍙兘浼氳嚦灏?鍒嗛挓鎵嶄繚瀛樹竴娆DB鏂囦欢銆傚湪杩欑鎯呭喌涓嬶紝涓€鏃﹀彂鐢熸晠闅滃仠鏈猴紝浣犲氨鍙兘浼氫涪澶卞ソ鍑犲垎閽熺殑鏁版嵁銆?br> 缂虹偣b锛氬綋鏁版嵁閲忛潪甯稿ぇ鐨勬椂鍊欙紝浠庣埗杩涚▼fork瀛愯繘绋嬭繘琛屼繚瀛樿嚦RDB鏂囦欢鏃堕渶瑕佷竴鐐规椂闂达紝鍙兘鏄绉掓垨绉掞紝鍙栧喅浜庣鐩樼殑IO鎬ц兘銆?br> 鍦ㄦ暟鎹泦姣旇緝搴炲ぇ鏃讹紝fork锛堬級鍙兘浼氶潪甯歌€楁椂锛岄€犳垚鏈嶅姟鍣ㄥ湪涓€瀹氭椂闂村唴鍋滄澶勭悊瀹㈡埛绔細濡傛灉鏁版嵁闆嗛潪甯稿法澶э紝骞朵笖CPU鏃堕棿闈炲父绱у紶鐨勮瘽锛岄偅涔堣繖绉嶅仠姝㈡椂闂寸敋鑷冲彲鑳戒細闀胯揪鏁存暣涓€绉掓垨鏇翠箙銆傝櫧鐒禔OF閲嶅啓涔熼渶瑕佽繘琛宖ork锛堬級锛屼絾鏃犺AOF閲嶅啓鐨勬墽琛岄棿闅旀湁澶氶暱锛屾暟鎹殑鎸佷箙鎬ч兘涓嶄細鏈変换浣曟崯澶便€?/p>


浜屻€丄OF鐨勪紭缂虹偣
1銆佷紭鐐筧 鏁版嵁瀹夊叏鎬х浉瀵硅緝楂橈紝鏍规嵁鎵€浣跨敤鐨刦snc绛栫暐锛坒sync鏄悓姝ュ唴瀛樹腑redis鎵€鏈夊凡缁忎慨鏀圭殑鏂囦欢鍒板瓨鍌ㄨ澶囷級锛岄粯璁ゆ槸appendfsync everysec锛屽嵆姣忕鎵ц涓€娆sync锛屽湪杩欑閰嶇疆涓嬶紝Redis浠嶇劧鍙互淇濇寔鑹ソ鐨勬€ц兘锛屽苟涓斿氨绠楀彂鐢熸晠闅滃仠鏈猴紝涔熸渶澶氬彧浼氫涪澶变竴绉掗挓鐨勬暟鎹紙fsync浼氬湪鍚庡彴绾跨▼鎵ц锛屾墍浠ヤ富绾跨▼鍙互缁х画鍔姏鍦板鐞嗗懡浠よ姹傦級
浼樼偣b锛氱敱浜庤鏈哄埗瀵规棩蹇楁枃浠剁殑鍐欏叆鎿嶄綔閲囩敤鐨勬槸append妯″紡锛屽洜姝ゅ湪鍐欏叆杩囩▼涓笉闇€瑕乻eek锛屽嵆浣垮嚭鐜板畷鏈虹幇璞★紝涔熶笉浼氱牬鍧忔棫鏃ュ織鏂囦欢涓凡缁忓瓨鍦ㄧ殑鍐呭銆傜劧鑰屽鏋滄湰娆℃搷浣滃彧鏄啓鍏ヤ簡涓€鍗婃暟鎹氨鍑虹幇浜嗗穿婧冮棶棰橈紝涓嶇敤鎷呭績锛屽湪Redis涓嬩竴娆″惎鍔ㄥ墠锛屽彲浠ラ€氳繃redis-check-aof宸ュ叿鏉ヨВ鍐虫暟鎹竴鑷存€х殑闂銆?br> 浼樼偣c: Redis 鍙互鍦ˋOF鏂囦欢浣撶Н鍙樺緱杩囧ぇ鏃讹紝鑷姩鍦板湪鍚庡彴瀵笰OF杩涜閲嶅啓锛氶噸鍐欏悗鐨勬柊AOF鏂囦欢鍖呭惈浜嗘仮澶嶅綋鍓嶆暟鎹泦鎵€闇€瑕佺殑鏈€灏忓懡浠ら泦鍚堛€傛暣涓噸鍐欐搷浣滄椂缁濆瀹夊叏鐨勫洜涓篟edis鍦ㄥ垱寤烘柊AOF鏂囦欢鐨勮繃绋嬩腑锛宎ppend妯″紡涓嶆柇鐨勫皢淇敼鏁版嵁杩藉姞鍒扮幇鏈夌殑AOF鏂囦欢閲岄潰锛屽嵆浣块噸鍐欒繃绋嬩腑鍙戠敓鍋滄満锛岀幇鏈夌殑AOF鏂囦欢涔熶笉浼氫涪澶便€傝€屼竴鏃︽柊鐨凙OF鏂囦欢鍒涘缓瀹屾瘯锛孯edis灏变細浠庢棫AOF鏂囦欢鍒囨崲鍒?鏂扮殑AOF鏂囦欢锛屽苟寮€濮嬪鏂癆OF鏂囦欢杩涜杩藉姞鎿嶄綔銆?br> 浼樼偣d锛欰OF鍖呭惈涓€涓牸寮忔竻鏅般€佹槗浜庣悊瑙g殑鏃ュ織鏂囦欢鐢ㄤ簬璁板綍鎵€鏈夌殑淇敼鎿嶄綔銆備簨瀹炰笂锛屼篃鍙互閫氳繃璇ユ枃浠跺畬鎴愭暟鎹殑閲嶅缓銆侫OF鏂囦欢鏈夊簭鍦颁繚瀛樹簡瀵规暟鎹簱鎵ц鐨勬墍鏈夊啓鍏ユ搷浣滐紝杩欎簺鍐欏叆鎿嶄綔Redis鍗忚鐨勬牸寮忎繚瀛橈紝鍥犳AOF鏂囦欢鐨勫唴瀹归潪甯稿鏄撹浜鸿鎳傦紝瀵规枃浠惰繘琛屽垎鏋愶紙parse)涔熷緢杞绘澗銆傚鍑猴紙export锛堿OF鏂囦欢涔熼潪甯哥畝鍗曪細涓句釜渚嬪瓙锛屽鏋滀綘涓嶅皬蹇冩墽琛屼簡FLUSHALL.鍛戒护锛屼絾鍙AOF鏂囦欢鏈閲嶅啓锛岄偅涔堝彧瑕佸仠姝㈡湇鍔″櫒锛岀Щ闄OF鏂囦欢鏈熬鐨凢ULUSHALL鍛戒护锛屽苟閲嶅惎Redis锛屽氨鍙互灏嗘暟鎹泦鎭㈠鍒癋LUSHALL鎵ц涔嬪墠鐨勭姸鎬?/p>


2銆佺己鐐筧: 鍗充娇鏈変簺鎿嶄綔鏃堕噸澶嶇殑涔熶細鍏ㄩ儴璁板綍锛孉OF鐨勬枃浠跺ぇ灏忚澶т簬RDB鏍煎紡鐨勬枃浠?br> 缂虹偣b:AOF鍦ㄦ仮澶嶅ぇ鏁版嵁闆嗘椂鐨勯€熷害姣擱DBB鐨勬仮澶嶉€熷害瑕佹參
缂虹偣c锛氭牴鎹甪sync绛栫暐涓嶅悓锛孉OF閫熷害鍙兘浼氭參浜嶳DB
缂虹偣d锛沚ug鍑虹幇鐨勫彲鑳芥€ф洿澶?/p>

RDB鍜孉OF鐨勯€夋嫨
濡傛灉涓昏鍏呭綋缂撳啿鍔熻兘锛屾垨鑰呭彲浠ユ壙鍙楁暟鍒嗛挓鏁版嵁鐨刬涓㈠け锛岄€氬父鐢熶骇鐜涓€鑸彧闇€鍚敤RDB鍗冲彲锛屾涔熸槸榛樿鍊笺€?br> 濡傛灉鏁版嵁闇€瑕佹寔涔呬繚瀛橈紝涓€鐐逛笉鑳戒涪澶憋紝鍙互閫夋嫨鍚屾椂寮€鍚疪DB鍜孉OF锛屼竴鑸笉寤鸿鍙紑鍚疉OF

绗簩涓鐩甿aster鍜宻lave鍚屾杩囩▼
瀹為獙濡備笅
鍑嗗锛氫富鑺傜偣MASTER-IP 10.0.0.128
浠庤妭鐐筍LAVE-IP 10.0.0.137
涓€銆佸湪涓昏妭鐐逛笂淇敼閰嶇疆鏂囦欢
[root@master ~]#vim /etc/redis.config
bind 0.0.0.0
requirepass 123456
configset masterauth 123456
閲嶅惎鏈嶅姟
systemclt restart redis

浜屻€佸湪浠庤妭鐐逛笂淇敼閰嶇疆鏂囦欢
[root@slave ~]#vim /etc/redis.config
bind 0.0.0.0
requirepass 123456
configset masterauth 123456
replicaof 10.0.0.110 6379

閲嶅惎鏈嶅姟
systemclt restart redis

涓夈€侀獙璇佷富浠庢槸鍚﹀悓姝?br>[root@slave ~]#redis-cli -a 123456 info replication
# Replication
role:slave 鏈満瑙掕壊鏄痵lave
master_host:10.0.0.128 涓昏妭鐐逛负10.0.0.128鍚屾鎴愬姛
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:593
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

绗笁涓鐩€佸摠鍏电殑浣跨敤鍜屽疄鐜版満鍒?/em>
闃叉鑴戣锛屽噯澶囧鏁板彴涓绘満瀹為獙
master: 10.0.0.110
slave1: 10.0.0.120
slave2: 10.0.0.123
涓€銆乵aster閰嶇疆
1銆佺敤sed瀹炵幇master閰嶇疆淇敼
bind 0.0.0.0
masterauth 123456
requirepass 12345
[root@10.0.0.128]#sed -i -e 鈥榮/bind 127.0.0.1/bind 0.0.0.0/鈥? -e 鈥榮/^# masterauth .*/masterauth 123456/鈥? -e 鈥榮/^# requirepass .*/requirepass 123456/鈥?/etc/redis.conf
妫€鏌ラ厤缃槸鍚︽湁鏁?br>grep -Ev 鈥榐#|^$鈥? /etc/redis.conf

2銆佷慨鏀瑰摠鍏祍entinel閰嶇疆鏂囦欢
[root@10.0.0.128]#vim /etc/redis-sentinel.conf
sentinel monitor mymaster 10.0.0.8 6379 2 锛坢ymaster鏄泦缇ょ殑鍛藉悕锛岄泦缇ゆ€昏妭鐐规暟/2鍙栨暣鏁帮紝灏忔暟鍒欏洓鑸嶄簲鍏?/2+1=2锛?br>sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 3000
grep -Ev 鈥榐#|^$鈥? /etc/redis.conf
grep -Ev 鈥榐#|^$鈥? /etc/redis-sentinel.conf
cat /etc/redis-sentinel.conf | grep sentinel
閲嶅惎鏈嶅姟
systemctl restart redis redis-sentinel

浜屻€佷慨鏀箂lave1閰嶇疆鏂囦欢
1銆乻lave1淇敼閰嶇疆 鐢╯ed瀹炵幇浠ヤ笅閰嶇疆 骞舵坊鍔爉aster10.0.0.110涓轰富鑺傜偣
[root@10.0.0.120]#sed -i -e 鈥榮/bind 127.0.0.1/bind 0.0.0.0/鈥? -e 鈥榮/^# masterauth .*/masterauth 123456/鈥? -e 鈥榮/^# requirepass .*/requirepass 123456/鈥?/etc/redis.conf
[root@10.0.0.120]#echo replicaof 10.0.0.110 6379
妫€鏌ラ厤缃槸鍚︽湁鏁?br>grep -Ev 鈥榐#|^$鈥? /etc/redis.conf

2銆佷慨鏀瑰摠鍏祍entinel閰嶇疆鏂囦欢
[root@10.0.0.120]#vim /etc/redis-sentinel.conf
sentinel monitor mymaster 10.0.0.110 6379 2 锛坢ymaster鏄泦缇ょ殑鍛藉悕锛岄泦缇ゆ€昏妭鐐规暟/2鍙栨暣鏁帮紝灏忔暟鍒欏洓鑸嶄簲鍏?/2+1=2锛?br>sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 3000
妫€鏌ラ厤缃槸鍚︽湁鏁?br>grep -Ev 鈥榐#|^$鈥? /etc/redis-sentinel.conf

涓夈€佷慨鏀箂lave2閰嶇疆鏂囦欢
1銆乻lave2淇敼閰嶇疆 鐢╯ed瀹炵幇浠ヤ笅閰嶇疆 骞舵坊鍔爉aster10.0.0.110涓轰富鑺傜偣
[root@10.0.0.123]#sed -i -e 鈥榮/bind 127.0.0.1/bind 0.0.0.0/鈥? -e 鈥榮/^# masterauth .*/masterauth 123456/鈥? -e 鈥榮/^# requirepass .*/requirepass 123456/鈥?/etc/redis.conf
[root@10.0.0.123]#echo replicaof 10.0.0.110 6379
妫€鏌ラ厤缃槸鍚︽湁鏁?br>grep -Ev 鈥榐#|^$鈥? /etc/redis.conf

2銆佷慨鏀瑰摠鍏祍entinel閰嶇疆鏂囦欢
[root@10.0.0.123]#vim /etc/redis-sentinel.conf
sentinel monitor mymaster 10.0.0.110 6379 2 锛坢ymaster鏄泦缇ょ殑鍛藉悕锛岄泦缇ゆ€昏妭鐐规暟/2鍙栨暣鏁帮紝灏忔暟鍒欏洓鑸嶄簲鍏?/2+1=2锛?br>sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 3000
妫€鏌ラ厤缃槸鍚︽湁鏁?br>grep -Ev 鈥榐#|^$鈥? /etc/redis-sentinel.conf

鍥涖€佹鏌ュ悇鑺傜偣瑙掕壊鏄惁1涓讳袱浠庯紵鍚勮妭鐐圭殑myid鏄惁涓嶅悓锛屽摠鍏祍entinel鎬绘暟鏄惁涓?
1銆佹鏌ヤ富浠庤鑹?br>[root@master ~]#redis-cli -a 123456 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=10.0.0.120,port=6379,state=online,offset=4087,lag=1
slave1:ip=10.0.0.123,port=6379,state=online,offset=4087,lag=1
master_replid:2be1cf7dfeed95e22c139873b62a3cf6e153b41d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:4101
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:4101

2銆佹鏌ュ摠鍏垫€绘暟
[root@centos ~]#redis-cli -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=10.0.0.110:6379,slaves=2,sentinels=3

3銆佹瘮杈冧笁涓妭鐐圭殑myid锛屼笉鍚屽垯閰嶇疆姝g‘銆?br>[root@master ~]#grep -Ev 鈥榐#|^$鈥? /etc/redis-sentinel.conf |grep myid
sentinel myid e9112c856e07f57b42156fff06aa9e0524bb5395
[root@slave1 ~]#grep -Ev 鈥榐#|^$鈥? /etc/redis-sentinel.conf |grep myid
sentinel myid a0d3ffed61130e96a384649d0538b082ae8137cd
[root@slav2 ~]#grep -Ev 鈥榐#|^$鈥? /etc/redis-sentinel.conf |grep myid
sentinel myid 3c81969730106b45a8988a859360535f682d197e

妯℃嫙涓昏妭鐐筸aster鍑虹幇鏁呴殰
[root@master ~]#systemctl stop redis

鏃ュ織鏌ョ湅鏁呴殰杞Щ杩囩▼(浠绘剰鑺傜偣鐨嗗彲鏌ヨ锛?br>tail -f /var/log/redis/sentinel.log
3092:X 06 Dec 2020 10:22:11.468 * +slave slave 10.0.0.120:6379 10.0.0.120 6379 @ mymaster 10.0.0.110 6379
3092:X 06 Dec 2020 10:22:11.470 * +slave slave 10.0.0.123:6379 10.0.0.123 6379 @ mymaster 10.0.0.110 6379
3092:X 06 Dec 2020 10:22:11.534 # -sdown master mymaster 10.0.0.110 6379
3092:X 06 Dec 2020 10:22:11.535 # -odown master mymaster 10.0.0.110 6379
3092:X 06 Dec 2020 10:55:30.121 # +sdown master mymaster 10.0.0.110 6379
3092:X 06 Dec 2020 10:55:30.177 # +new-epoch 4
3092:X 06 Dec 2020 10:55:30.177 # +vote-for-leader a0d3ffed61130e96a384649d0538b082ae8137cd 4
3092:X 06 Dec 2020 10:55:30.191 # +odown master mymaster 10.0.0.110 6379 #quorum 3/2
3092:X 06 Dec 2020 10:55:30.191 # Next failover delay: I will not start a failover before Sun Dec 6 11:01:30 2020
3092:X 06 Dec 2020 10:55:31.260 # +config-update-from sentinel a0d3ffed61130e96a384649d0538b082ae8137cd 10.0.0.120 26379 @ mymaster 10.0.0.110 6379
3092:X 06 Dec 2020 10:55:31.260 # +switch-master mymaster 10.0.0.110 6379 10.0.0.120 6379
3092:X 06 Dec 2020 10:55:31.261 * +slave slave 10.0.0.123:6379 10.0.0.123 6379 @ mymaster 10.0.0.120 6379 鍙湅鍒皊lave1 10.0.0.120琚€変妇涓轰富鑺傜偣master
3092:X 06 Dec 2020 10:55:31.261 * +slave slave 10.0.0.110:6379 10.0.0.110 6379 @ mymaster 10.0.0.120 6379
3092:X 06 Dec 2020 10:55:34.291 # +sdown slave 10.0.0.110:6379 10.0.0.110 6379 @ mymaster 10.0.0.120 6379
[root@slave1 ~]#redis-cli -a 123456 info replication
Warning: Using a password with 鈥?a鈥?or 鈥?u鈥?option on the command line interface may not be safe.
# Replication
role:master 宸茬粡涓簃aster
connected_slaves:1 鐢变簬鍘熸潵涓昏妭鐐瑰畷鏈猴紝鎵€浠ョ幇鍦ㄦ槸鏂癿aster鍜宻lave2
slave0:ip=10.0.0.123,port=6379,state=online,offset=98579,lag=0
master_replid:e7114ed4c2fee39cf0463a573d4b90fc15d245f5
master_replid2:85631e9b1fb9d1790e8966d05ccd50356d6d9cb0
master_repl_offset:98579
second_repl_offset:969
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:98579
妫€鏌lave2鐨勯厤缃枃浠?etc/redis.conf宸茬粡鑷姩鏇存柊涓?0.0.0.120
[root@slave2 ~]#grep -Ev 鈥榐#|^$鈥?/etc/redis.conf | grep replicaof
replicaof 10.0.0.120 63

妫€鏌lave2鐨勯厤缃枃浠?/etc/redis-sentinel.conf 鏄惁鑷姩鏇存柊鎴愬姛
[root@slave2 ~]#grep -Ev 鈥榐#|^$鈥?/etc/redis-sentinel.conf | grep mymaster
sentinel monitor mymaster 10.0.0.120 6379 2 宸叉洿鏂?br>sentinel down-after-milliseconds mymaster 3000
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 4
sentinel leader-epoch mymaster 4
sentinel known-replica mymaster 10.0.0.110 6379
sentinel known-replica mymaster 10.0.0.123 6379
sentinel known-sentinel mymaster 10.0.0.120 26379 a0d3ffed61130e96a384649d0538b082ae8137cd
sentinel known-sentinel mymaster 10.0.0.110 26379 e9112c856e07f57b42156fff06aa9e0524bb5395

绗洓涓鐩畆edis cluster闆嗙兢鍒涘缓鍜屼娇鐢?/strong>
鍑嗗涓夊彴涓绘満
maser1:10.0.0.110
master2:10.0.0.120
master3:10.0.0.123
鏂板姞鍏ョ殑master4:10.0.0.130
鏂板姞鍏ョ殑slave1:10.0.0.138
涓€銆乺edis cluster闆嗙兢鐨勫垱寤?br>1鍦ㄤ笁鍙颁富鏈轰慨鏀瑰悓鏍风殑鎿嶄綔锛氬畨瑁卹edis鏈嶅姟
[root@master1/2/3~]#yum -y install redis

2銆佸湪涓夊彴涓绘満淇敼鍚屾牱鐨勬搷浣滐細淇敼閰嶇疆鏂囦欢/etc/redis.conf
鏂规硶a vim
[root@master1~]#vim /etc/redis.conf
bind 0.0.0.0
masterauth 123456
requirepass 123456
cluster-enabled yes 榛樿鍏抽棴
cluster-config-file nodes-6379.conf 鍘绘帀娉ㄩ噴
cluster-require-full-coverage no

鏂规硶b 鐢╯ed鍛戒护淇敼涓婚厤缃枃浠?br>[root@master1 ~]#sed -i.bak -e 鈥榮/bind 127.0.0.1/bind 0.0.0.0/鈥? -e 鈥?masterauth/a masterauth 123456鈥? -e 鈥?# requirepass/a requirepass 123456鈥? -e 鈥?# cluster-enabled yes/a cluster-enabled yes鈥? -e 鈥?# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf鈥? -e 鈥?cluster-require-full-coverage yes/c cluster-require-full-coverage no鈥? /etc/redis.conf

3銆侀噸鍚痳edis鏈嶅姟
[root@master1 ~]#systemctl enable --now redis

4銆佸娉細mastter2 鍜宮aster3閰嶇疆鍚屼笂闈㈢殑master1

5銆佸悇涓妭鐐逛箣闂村缓绔嬪弸璋婂叧绯籱eet
[root@mster1 ~]#redis-cli -a 123456 --no-auth-warning cluster meet 10.0.0.120 6379
OK
[root@master1 ~]#redis-cli -a 123456 --no-auth-warning cluster meet 10.0.0.123 6379
OK
[root@master2~]#redis-cli -a 123456 --no-auth-warning cluster meet 10.0.0.123 6379
OK

6銆佷换鎰忚妭鐐规煡鐪嬭闆嗙兢褰撳墠鍑犱釜涓昏妭鐐?br>[root@master1 ~]#redis-cli -a 123456 cluster nodes
Warning: Using a password with 鈥?a鈥?or 鈥?u鈥?option on the command line interface may not be safe.
c1fdf4469f7e5047caf930326e742da0adfbcd73 10.0.0.123:6379@16379 master - 0 1607226490256 2 connected
d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379@16379 myself,master - 0 1607226488000 0 connected
01f1dd9c14ea7f886456a7758f4198d6280ae390 10.0.0.120:6379@16379 master - 0 1607226491279 1 connected
*******璇ラ泦缇ゆ湁涓変釜涓昏妭鐐瑰凡缁忓垱寤哄ソ銆?*******

浜屻€乺edis cluster闆嗙兢鐨勪娇鐢?锛堣瀹為獙浠edis5鐗堟湰涓轰緥锛岃嫢3/4鐗堟湰锛岄渶瑕佺敤ruby宸ュ叿瀹夎渚濊禆鍖呮潵鎼缓ruby鐨勭幆澧冿級
涓嬮潰浠庢墿瀹癸紝缂╁鏉ュ垎鍒疄楠?br>A鎵╁瀹為獙
1銆佸湪鍏朵腑涓€鍙颁富鑺傜偣鍒嗛厤妲戒綅鍗冲彲锛氱敱浜庢湁16834涓猻lots(妲戒綅锛夛紝鎵€浠ラ渶瑕?缂栬緫鑴氭湰鍒嗗埆鍦ㄥ悇鑺傜偣鍒嗛厤妲戒綅
[root@master1 ~]#vim addslot.sh
host=$1
port=$2
start=$3
end=$4
pass=123456

for slot in `seq ${start} ${end}` ;do
echo slot:$slot
redis-cli -h ${host} -p $port -a ${pass} --no-auth-warning cluster addslots ${slot}
done
2銆佹墽琛岃剼鏈敓鎴愰鍏堣鍒掑ソ鐨勬Ы浣?br>[root@master1 ~]#bash addslot.sh 10.0.0.110 6379 0 5461
[root@master1 ~]#bash addslot.sh 10.0.0.120 6379 5462 10922
[root@master1 ~]#bash addslot.sh 10.0.0.123 6379 10923 16383

鏌ョ湅妲戒綅鏄惁鍒嗛厤濂?br>[root@centos ~]#redis-cli -a 123456 --no-auth-warning cluster nodes
c1fdf4469f7e5047caf930326e742da0adfbcd73 10.0.0.123:6379@16379 master - 0 1607239685000 2 connected 10923-16383
d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379@16379 myself,master - 0 1607239683000 0 connected 0-5461
01f1dd9c14ea7f886456a7758f4198d6280ae390 10.0.0.120:6379@16379 master - 0 1607239685330 1 connected 5462-10922

3銆佹坊鍔犱粠鑺傜偣骞舵寚瀹氫粠鑺傜偣鍜屼富鑺傜偣
[root@master1 ~]#redis-cli -a 123456 --cluster add-node 10.0.0.130:6379 10.0.0.110:6379
鐢变簬鏂癿aster鍔犲叆锛岄渶瑕侀噸鏂板垎閰嶆Ы浣嶏紝鎬濊矾锛氬皢鍘熸潵鐨勮妭鐐圭殑鎵€鏈夋Ы浣嶅钩鍧囧垎閰嶇粰瀹冨嵆鍙€?br>鍦ㄤ换鎰忎竴鍙颁富鏈哄嵆鍙垎閰嶆Ы浣?br>[root@master1 ~]#redis-cli -a 123456 --cluster reshard 10.0.0.130:6379
Warning: Using a password with 鈥?a鈥?or 鈥?u鈥?option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 10.0.0.130:6379)
M: 8683d12e877291842b2dadb428fd8b72ab294425 10.0.0.130:6379
slots: (0 slots) master
M: d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379
slots:[0-5461] (5462 slots) master
M: c1fdf4469f7e5047caf930326e742da0adfbcd73 10.0.0.123:6379
slots:[10923-16383] (5461 slots) master
M: 01f1dd9c14ea7f886456a7758f4198d6280ae390 10.0.0.120:6379
slots:[5462-10922] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096 4涓猰aster骞冲潎鍒嗛厤妲戒綅= 锛?6384/4=4096锛?br>What is the receiving node ID? 8683d12e877291842b2dadb428fd8b72ab294425 鏂癿aster鐨処D鍙?br>Please enter all the source node IDs.
Type 鈥榓ll鈥?to use all the nodes as source nodes for the hash slots.
Type 鈥榙one鈥?once you entered all the source nodes IDs.
Source node #1: all 璇存槑灏嗘墍鏈夎妭鐐圭殑妲戒綅閮戒細骞冲潎鍒嗛厤鍒版柊master
...........................................................................
Moving slot 6824 from 01f1dd9c14ea7f886456a7758f4198d6280ae390
Moving slot 6825 from 01f1dd9c14ea7f886456a7758f4198d6280ae390
Moving slot 6826 from 01f1dd9c14ea7f886456a7758f4198d6280ae390
Do you want to proceed with the proposed reshard plan (yes/no)? yes

4銆佷换鎰忎竴鍙颁富鏈烘煡鐪嬫Ы浣?br>[root@master1~]#redis-cli -a 123456 cluster nodes
Warning: Using a password with 鈥?a鈥?or 鈥?u鈥?option on the command line interface may not be safe.
d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379@16379 master - 0 1607241112000 0 connected 1366-5461
8683d12e877291842b2dadb428fd8b72ab294425 10.0.0.130:6379@16379 myself,master - 0 1607241110000 4 connected 0-1365 5462-6826 10923-12287
c1fdf4469f7e5047caf930326e742da0adfbcd73 10.0.0.123:6379@16379 master - 0 1607241111000 2 connected 12288-16383
01f1dd9c14ea7f886456a7758f4198d6280ae390 10.0.0.120:6379@16379 master - 0 1607241111779 1 connected 6827-10922

5銆佺幇鍦ㄥ姞鍏ヤ竴鍙颁粠鑺傜偣鍔犲叆鎴愪负10.0.0.110鐨勪粠鑺傜偣
淇敼閰嶇疆鏂囦欢 /etc/redis.conf
[root@10.0.0.138 ~]#sed -i.bak -e 鈥榮/bind 127.0.0.1/bind 0.0.0.0/鈥? -e 鈥?masterauth/a masterauth 123456鈥? -e 鈥?# requirepass/a requirepass 123456鈥? -e 鈥?# cluster-enabled yes/a cluster-enabled yes鈥? -e 鈥?# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf鈥? -e 鈥?cluster-require-full-coverage yes/c cluster-require-full-coverage no鈥? /etc/redis.conf

6銆侀噸鍚湇鍔?br>[root@10.0.0.138 ~]#systemctl enable --now redis
灏嗘柊鑺傜偣138鍔犲叆闆嗙兢锛岄粯璁ゆ槸鍔犲叆鍚庝负涓昏妭鐐筸aster
[root@10.0.0.138~]#redis-cli -a 123456 --cluster add-node 10.0.0.138:6379 10.0.0.120:6379 --cluster-slave --cluster-master-id d7fdc965f3d5c6f6c22d049233f90fa97594e0a8
Warning: Using a password with 鈥?a鈥?or 鈥?u鈥?option on the command line interface may not be safe.
>>> Adding node 10.0.0.138:6379 to cluster 10.0.0.120:6379
>>> Performing Cluster Check (using node 10.0.0.120:6379)
M: 01f1dd9c14ea7f886456a7758f4198d6280ae390 10.0.0.120:6379
slots:[6827-10922] (4096 slots) master
M: 8683d12e877291842b2dadb428fd8b72ab294425 10.0.0.130:6379
slots:[0-1365],[5462-6826],[10923-12287] (4096 slots) master
M: c1fdf4469f7e5047caf930326e742da0adfbcd73 10.0.0.123:6379
slots:[12288-16383] (4096 slots) master
M: d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379
slots:[1366-5461] (4096 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 10.0.0.138:6379 to make it join the cluster.
Waiting for the cluster to join
...
>>> Configure node as replica of 10.0.0.110:6379.
[OK] New node added correctly. 娣诲姞鎴愬姛

7銆佹煡鐪?0.0.0.110鐨勪粠鑺傜偣鏄惁鏄?38鐨勪富鑺傜偣
[root@localhost ~]#redis-cli -a 123456 --no-auth-warning cluster nodes
901c99dbad1fbbd86bc469015ac99469341bead0 10.0.0.138:6379@16379 slave d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 0 1607242075152 5 connected 宸叉洿鏂颁负110鐨勪粠
d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379@16379 master - 0 1607242076159 0 connected 1366-5461

B缂╁瀹為獙 锛堝垹闄ゅ啑浣欑殑涓昏妭鐐?0.0.0.130锛夊疄楠屽涓?br>1銆佸厛杞Щ璇ヨ妭鐐圭殑妲戒綅鍒板叾浠栧悇鑺傜偣
[root@localhost ~]#redis-cli -a 123456 --cluster reshard 10.0.0.130:6379
[root@localhost ~]#redis-cli -a 123456 --cluster reshard 10.0.0.130:6379
Warning: Using a password with 鈥?a鈥?or 鈥?u鈥?option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 10.0.0.130:6379)
M: 8683d12e877291842b2dadb428fd8b72ab294425 10.0.0.130:6379
slots:[0-1365],[5462-6826],[10923-12287] (4096 slots) master 鏈?涓Ы浣?锛屽鏋滆浆绉绘暟鎹紝闇€瑕佽浆3娆°€?br>S: 901c99dbad1fbbd86bc469015ac99469341bead0 10.0.0.138:6379
slots: (0 slots) slave
replicates d7fdc965f3d5c6f6c22d049233f90fa97594e0a8
M: 01f1dd9c14ea7f886456a7758f4198d6280ae390 10.0.0.120:6379
slots:[6827-10922] (4096 slots) master
M: d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379
slots:[1366-5461] (4096 slots) master
1 additional replica(s)
M: c1fdf4469f7e5047caf930326e742da0adfbcd73 10.0.0.123:6379
slots:[12288-16383] (4096 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 1366 绗?涓Ы浣?br>What is the receiving node ID? 01f1dd9c14ea7f886456a7758f4198d6280ae390
Please enter all the source node IDs.
Type 鈥榓ll鈥?to use all the nodes as source nodes for the hash slots.
Type 鈥榙one鈥?once you entered all the source nodes IDs.
Source node #1: 8683d12e877291842b2dadb428fd8b72ab294425 鍒嗛厤缁?0.0.0.120
Source node #2: done 缁撴潫

2銆佸悓鐞嗗垎鍒浆绉籟5462-6826],[10923-12287]
3銆佹煡鐪?0.0.0.130鏄惁杩樻湁妲戒綅
[root@centos ~]#redis-cli -a 123456 cluster nodes
8683d12e877291842b2dadb428fd8b72ab294425 10.0.0.130:6379@16379 master - 0 1607243239000 4 connected 宸叉棤妲戒綅

4銆佸垹闄や富鑺傜偣10.0.0.130
[root@centos ~]#redis-cli -a 123456 --cluster del-node 10.0.0.20锛?379 8683d12e877291842b2dadb428fd8b72ab294425
Warning: Using a password with 鈥?a鈥?or 鈥?u鈥?option on the command line interface may not be safe.
>>> Removing node 8683d12e877291842b2dadb428fd8b72ab294425 from cluster 10.0.0.20:6379
Could not connect to Redis at 10.0.0.20:6379: No route to host

5鏌ョ湅涓昏妭鐐?0.0.0.130鏄惁瀛樺湪
[root@centos ~]#redis-cli -a 123456 cluster nodes 鍙湅鍒?0.0.0.130鍒犻櫎鎴愬姛
Warning: Using a password with 鈥?a鈥?or 鈥?u鈥?option on the command line interface may not be safe.
d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 10.0.0.110:6379@16379 myself,master - 0 1607243608000 8 connected 1366-5461 10923-12287
c1fdf4469f7e5047caf930326e742da0adfbcd73 10.0.0.123:6379@16379 master - 0 1607243608842 7 connected 5462-6826 12288-16383
01f1dd9c14ea7f886456a7758f4198d6280ae390 10.0.0.120:6379@16379 master - 0 1607243607000 6 connected 0-1365 6827-10922
901c99dbad1fbbd86bc469015ac99469341bead0 10.0.0.138:6379@16379 slave d7fdc965f3d5c6f6c22d049233f90fa97594e0a8 0 1607243607833 8 connecte

6銆佸垹闄?0.0.0.130鐨勫弬涓庢暟鎹苟閲嶅惎
[root@10.0.0.130 ~]#rm -f /var/lib/redis/nodes-6379.conf
[root@10.0.0.130 ~]#systemctl restart redis
7
鏌ョ湅鏄惁鏈夐泦缇や俊鎭?br>[root@localhost ~]#redis-cli -a 123456 cluster nodes
Warning: Using a password with 鈥?a鈥?or 鈥?u鈥?option on the command line interface may not be safe.
210560ccd0b2767b73d40dacf8ef7727fdf2c21e :6379@16379 myself,master - 0 0 0 connected

鍒?闄や粠鑺傜偣10.0.0.138锛屼竴鏉″懡浠ゅ嵆鍙?br>[root@localhost ~]#redis-cli -a 123456 --cluster del-node 10.0.0.120:6379 901c99dbad1fbbd86bc469015ac99469341bead0

C灏嗗閮ㄦ暟鎹鍏ラ泦缇や腑
鍋囪锛宺edis鏈嶅姟鍣ㄥ凡杩愯涓€娈垫椂闂达紝鐢变簬涔熻閲忓簽澶э紝闇€瑕佸缓绔媍luster鏉ョ鍚堝叕鍙歌姹傘€?br>濡備綍灏唕edis鏈嶅姟鍣?0.0.0.130鐨勬暟鎹鍏ユ柊寤虹殑闆嗙兢涓?
1銆佸皢130鐨勪富鏈烘仮澶嶅崟鏈簉edis鏈嶅姟鍣ㄧ殑閰嶇疆锛屽彇娑堥泦缇ょ浉鍏抽厤缃€?br>2銆佸垹闄ゆ墍鏈夎妭鐐圭殑瀵嗙爜
[root@localhost ~]#redis-cli -a 123456 --no-auth-warning config set requirepass "" ( ""琛ㄧず涓虹姝㈡墍鏈夛級5鐗堟湰
[root@localhost ~]#redis-cli --cluster import 10.0.0.128:6379 --cluster-from 10.0.0.136:6379 --cluster-replace --cluster-copy 鑻ユ湁鐩稿悓Key鍒欒鐩栧師鏉ョ殑key鍊?
[root@localhost ~]#redis-cli --cluster import 10.0.0.128:6379 --cluster-from 10.0.0.136:6379 5 鑻ユ湁鐩稿悓key 涓嶈鐩栧師鏉ョ殑鍊硷紝澶栭儴鏁版嵁涔熼粯璁ょ姝㈠啓鍏ャ€?br>redis-cli --cluster import 10.0.0.128:6379 --cluster-from 10.0.0.130:6379

 









































































































































































































































































































以上是关于redis:RDB AOF -master &slave -sentinel-cluster的主要内容,如果未能解决你的问题,请参考以下文章

redis RDB 和AOF

redis RDB和AOF

[Redis] RDB & AOF

Redis RDB AOF 两种机制

Redis RDB持久化和AOF持久化详细讲解

redis RDB快照和AOF日志持久化配置