redis-sentinel 主从复制高可用
?
Redis-Sentinel
Redis-Sentinel是redis官方推薦的高可用性解決方案,當(dāng)用redis作master-slave的高可用時(shí),如果master本身宕機(jī),redis本身或者客戶端都沒有實(shí)現(xiàn)主從切換的功能。而redis-sentinel就是一個(gè)獨(dú)立運(yùn)行的進(jìn)程,用于監(jiān)控多個(gè)master-slave集群,
自動(dòng)發(fā)現(xiàn)master宕機(jī),進(jìn)行自動(dòng)切換slave > master。
sentinel主要功能如下:
- 不時(shí)的監(jiān)控redis是否良好運(yùn)行,如果節(jié)點(diǎn)不可達(dá)就會(huì)對(duì)節(jié)點(diǎn)進(jìn)行下線標(biāo)識(shí)
- 如果被標(biāo)識(shí)的是主節(jié)點(diǎn),sentinel就會(huì)和其他的sentinel節(jié)點(diǎn)“協(xié)商”,如果其他節(jié)點(diǎn)也人為主節(jié)點(diǎn)不可達(dá),就會(huì)選舉一個(gè)sentinel節(jié)點(diǎn)來(lái)完成自動(dòng)故障轉(zhuǎn)義
- 在master-slave進(jìn)行切換后,master_redis.conf、slave_redis.conf和sentinel.conf的內(nèi)容都會(huì)發(fā)生改變,即master_redis.conf中會(huì)多一行slaveof的配置,sentinel.conf的監(jiān)控目標(biāo)會(huì)隨之調(diào)換
Sentinel的工作方式:
每個(gè)Sentinel以每秒鐘一次的頻率向它所知的Master,Slave以及其他 Sentinel 實(shí)例發(fā)送一個(gè) PING 命令如果一個(gè)實(shí)例(instance)距離最后一次有效回復(fù) PING 命令的時(shí)間超過(guò) down-after-milliseconds 選項(xiàng)所指定的值, 則這個(gè)實(shí)例會(huì)被 Sentinel 標(biāo)記為主觀下線。如果一個(gè)Master被標(biāo)記為主觀下線,則正在監(jiān)視這個(gè)Master的所有 Sentinel 要以每秒一次的頻率確認(rèn)Master的確進(jìn)入了主觀下線狀態(tài)。當(dāng)有足夠數(shù)量的 Sentinel(大于等于配置文件指定的值)在指定的時(shí)間范圍內(nèi)確認(rèn)Master的確進(jìn)入了主觀下線狀態(tài), 則Master會(huì)被標(biāo)記為客觀下線在一般情況下, 每個(gè) Sentinel 會(huì)以每 10 秒一次的頻率向它已知的所有Master,Slave發(fā)送 INFO 命令當(dāng)Master被 Sentinel 標(biāo)記為客觀下線時(shí),Sentinel 向下線的 Master 的所有 Slave 發(fā)送 INFO 命令的頻率會(huì)從 10 秒一次改為每秒一次若沒有足夠數(shù)量的 Sentinel 同意 Master 已經(jīng)下線, Master 的客觀下線狀態(tài)就會(huì)被移除。若 Master 重新向 Sentinel 的 PING 命令返回有效回復(fù), Master 的主觀下線狀態(tài)就會(huì)被移除。主觀下線和客觀下線主觀下線:Subjectively Down,簡(jiǎn)稱 SDOWN,指的是當(dāng)前 Sentinel 實(shí)例對(duì)某個(gè)redis服務(wù)器做出的下線判斷。 客觀下線:Objectively Down, 簡(jiǎn)稱 ODOWN,指的是多個(gè) Sentinel 實(shí)例在對(duì)Master Server做出 SDOWN 判斷,并且通過(guò) SENTINEL is-master-down-by-addr 命令互相交流之后,得出的Master Server下線判斷,然后開啟failover.SDOWN適合于Master和Slave,只要一個(gè) Sentinel 發(fā)現(xiàn)Master進(jìn)入了ODOWN, 這個(gè) Sentinel 就可能會(huì)被其他 Sentinel 推選出, 并對(duì)下線的主服務(wù)器執(zhí)行自動(dòng)故障遷移操作。ODOWN只適用于Master,對(duì)于Slave的 Redis 實(shí)例,Sentinel 在將它們判斷為下線前不需要進(jìn)行協(xié)商, 所以Slave的 Sentinel 永遠(yuǎn)不會(huì)達(dá)到ODOWN。 sentinel工作方式redis主從復(fù)制背景問(wèn)題
Redis主從復(fù)制可將主節(jié)點(diǎn)數(shù)據(jù)同步給從節(jié)點(diǎn),從節(jié)點(diǎn)此時(shí)有兩個(gè)作用:
- 一旦主節(jié)點(diǎn)宕機(jī),從節(jié)點(diǎn)作為主節(jié)點(diǎn)的備份可以隨時(shí)頂上來(lái)。
- 擴(kuò)展主節(jié)點(diǎn)的讀能力,分擔(dān)主節(jié)點(diǎn)讀壓力。
但是問(wèn)題是:
- 一旦主節(jié)點(diǎn)宕機(jī),從節(jié)點(diǎn)上位,那么需要人為修改所有應(yīng)用方的主節(jié)點(diǎn)地址(改為新的master地址),還需要命令所有從節(jié)點(diǎn)復(fù)制新的主節(jié)點(diǎn)
那么這個(gè)問(wèn)題,redis-sentinel就可以解決了
主從復(fù)制架構(gòu)
Redis Sentinel架構(gòu)
redis的一個(gè)進(jìn)程,但是不存儲(chǔ)數(shù)據(jù),只是監(jiān)控redis
?
redis命令整理
官網(wǎng)地址:http://redisdoc.com/redis-cli info #查看redis數(shù)據(jù)庫(kù)信息redis-cli info replication #查看redis的復(fù)制授權(quán)信息redis-cli info sentinel #查看redis的哨兵信息
?
安裝與配置
本實(shí)驗(yàn)是在測(cè)試環(huán)境下,考慮到學(xué)生機(jī)器較弱,因此只準(zhǔn)備一臺(tái)linux服務(wù)器用作環(huán)境!!
服務(wù)器環(huán)境,一臺(tái)即可完成操作
master 192.168.119.10所有配置文件如下
主節(jié)點(diǎn)master的redis-6379.conf
port 6379 daemonize yes logfile "6379.log" dbfilename "dump-6379.rdb" dir "/var/redis/data/"從節(jié)點(diǎn)slave的redis-6380.conf
port 6380 daemonize yes logfile "6380.log" dbfilename "dump-6380.rdb" dir "/var/redis/data/" slaveof 127.0.0.1 6379 // 從屬主節(jié)點(diǎn)從節(jié)點(diǎn)slave的redis-6381.conf
port 6381 daemonize yes logfile "6380.log" dbfilename "dump-6380.rdb" dir "/var/redis/data/" slaveof 127.0.0.1 6379 // 從屬主節(jié)點(diǎn)啟動(dòng)redis主節(jié)點(diǎn)
redis-server /etc/redis-6379.conf測(cè)試redis主節(jié)點(diǎn)是否通信
redis-cli ping啟動(dòng)兩slave節(jié)點(diǎn)
還記得上面超哥的截圖嗎?總體redis配置文件如下,6379為master,6380和6381為slave
-rw-r--r-- 1 root root 145 Nov 7 17:44 /etc/redis-6379.conf #這個(gè)為主,port是6379 -rw-r--r-- 1 root root 93 Nov 7 17:42 /etc/redis-6380.conf # 這個(gè)是從,port6380,并且得加上新的參數(shù)slaveof -rw-r--r-- 1 root root 115 Nov 7 17:42 /etc/redis-6381.conf # 這個(gè)是從,port6381,并且得加上新的參數(shù)slaveofredis-6380.conf? slave配置文件詳解,6381端口的配置文件,僅僅和6380端口不一樣
port 6380 daemonize yes logfile "6379.log" dbfilename "dump-6379.rdb" dir "/var/redis/data" # Generated by CONFIG REWRITE slaveof 127.0.0.1 6379 # Redis 配置文件# 當(dāng)配置中需要配置內(nèi)存大小時(shí),可以使用 1k, 5GB, 4M 等類似的格式,其轉(zhuǎn)換方式如下(不區(qū)分大小寫) # # 1k => 1000 bytes # 1kb => 1024 bytes # 1m => 1000000 bytes # 1mb => 1024*1024 bytes # 1g => 1000000000 bytes # 1gb => 1024*1024*1024 bytes # # 內(nèi)存配置大小寫是一樣的.比如 1gb 1Gb 1GB 1gB# daemonize no 默認(rèn)情況下,redis不是在后臺(tái)運(yùn)行的,如果需要在后臺(tái)運(yùn)行,把該項(xiàng)的值更改為yes daemonize yes# 當(dāng)redis在后臺(tái)運(yùn)行的時(shí)候,Redis默認(rèn)會(huì)把pid文件放在/var/run/redis.pid,你可以配置到其他地址。 # 當(dāng)運(yùn)行多個(gè)redis服務(wù)時(shí),需要指定不同的pid文件和端口 pidfile /var/run/redis.pid# 指定redis運(yùn)行的端口,默認(rèn)是6379 port 6379# 指定redis只接收來(lái)自于該IP地址的請(qǐng)求,如果不進(jìn)行設(shè)置,那么將處理所有請(qǐng)求, # 在生產(chǎn)環(huán)境中最好設(shè)置該項(xiàng) # bind 127.0.0.1# Specify the path for the unix socket that will be used to listen for # incoming connections. There is no default, so Redis will not listen # on a unix socket when not specified. # # unixsocket /tmp/redis.sock # unixsocketperm 755# 設(shè)置客戶端連接時(shí)的超時(shí)時(shí)間,單位為秒。當(dāng)客戶端在這段時(shí)間內(nèi)沒有發(fā)出任何指令,那么關(guān)閉該連接 # 0是關(guān)閉此設(shè)置 timeout 0# 指定日志記錄級(jí)別 # Redis總共支持四個(gè)級(jí)別:debug、verbose、notice、warning,默認(rèn)為verbose # debug 記錄很多信息,用于開發(fā)和測(cè)試 # varbose 有用的信息,不像debug會(huì)記錄那么多 # notice 普通的verbose,常用于生產(chǎn)環(huán)境 # warning 只有非常重要或者嚴(yán)重的信息會(huì)記錄到日志 loglevel debug# 配置log文件地址 # 默認(rèn)值為stdout,標(biāo)準(zhǔn)輸出,若后臺(tái)模式會(huì)輸出到/dev/null #logfile stdout logfile /var/log/redis/redis.log# To enable logging to the system logger, just set 'syslog-enabled' to yes, # and optionally update the other syslog parameters to suit your needs. # syslog-enabled no# Specify the syslog identity. # syslog-ident redis# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7. # syslog-facility local0# 可用數(shù)據(jù)庫(kù)數(shù) # 默認(rèn)值為16,默認(rèn)數(shù)據(jù)庫(kù)為0,數(shù)據(jù)庫(kù)范圍在0-(database-1)之間 databases 16################################ 快照 ################################# # # 保存數(shù)據(jù)到磁盤,格式如下: # # save <seconds> <changes> # # 指出在多長(zhǎng)時(shí)間內(nèi),有多少次更新操作,就將數(shù)據(jù)同步到數(shù)據(jù)文件rdb。 # 相當(dāng)于條件觸發(fā)抓取快照,這個(gè)可以多個(gè)條件配合 # # 比如默認(rèn)配置文件中的設(shè)置,就設(shè)置了三個(gè)條件 # # save 900 1 900秒內(nèi)至少有1個(gè)key被改變 # save 300 10 300秒內(nèi)至少有300個(gè)key被改變 # save 60 10000 60秒內(nèi)至少有10000個(gè)key被改變save 900 1 save 300 10 save 60 10000# 存儲(chǔ)至本地?cái)?shù)據(jù)庫(kù)時(shí)(持久化到rdb文件)是否壓縮數(shù)據(jù),默認(rèn)為yes rdbcompression yes# 本地持久化數(shù)據(jù)庫(kù)文件名,默認(rèn)值為dump.rdb dbfilename dump.rdb# 工作目錄 # # 數(shù)據(jù)庫(kù)鏡像備份的文件放置的路徑。 # 這里的路徑跟文件名要分開配置是因?yàn)閞edis在進(jìn)行備份時(shí),先會(huì)將當(dāng)前數(shù)據(jù)庫(kù)的狀態(tài)寫入到一個(gè)臨時(shí)文件中,等備份完成時(shí), # 再把該該臨時(shí)文件替換為上面所指定的文件,而這里的臨時(shí)文件和上面所配置的備份文件都會(huì)放在這個(gè)指定的路徑當(dāng)中。 # # AOF文件也會(huì)存放在這個(gè)目錄下面 # # 注意這里必須制定一個(gè)目錄而不是文件 dir ./################################# 復(fù)制 ################################## 主從復(fù)制. 設(shè)置該數(shù)據(jù)庫(kù)為其他數(shù)據(jù)庫(kù)的從數(shù)據(jù)庫(kù). # 設(shè)置當(dāng)本機(jī)為slav服務(wù)時(shí),設(shè)置master服務(wù)的IP地址及端口,在Redis啟動(dòng)時(shí),它會(huì)自動(dòng)從master進(jìn)行數(shù)據(jù)同步 # # slaveof <masterip> <masterport># 當(dāng)master服務(wù)設(shè)置了密碼保護(hù)時(shí)(用requirepass制定的密碼) # slav服務(wù)連接master的密碼 # # masterauth <master-password># 當(dāng)從庫(kù)同主機(jī)失去連接或者復(fù)制正在進(jìn)行,從機(jī)庫(kù)有兩種運(yùn)行方式: # # 1) 如果slave-serve-stale-data設(shè)置為yes(默認(rèn)設(shè)置),從庫(kù)會(huì)繼續(xù)相應(yīng)客戶端的請(qǐng)求 # # 2) 如果slave-serve-stale-data是指為no,出去INFO和SLAVOF命令之外的任何請(qǐng)求都會(huì)返回一個(gè) # 錯(cuò)誤"SYNC with master in progress" # slave-serve-stale-data yes# 從庫(kù)會(huì)按照一個(gè)時(shí)間間隔向主庫(kù)發(fā)送PINGs.可以通過(guò)repl-ping-slave-period設(shè)置這個(gè)時(shí)間間隔,默認(rèn)是10秒 # # repl-ping-slave-period 10# repl-timeout 設(shè)置主庫(kù)批量數(shù)據(jù)傳輸時(shí)間或者ping回復(fù)時(shí)間間隔,默認(rèn)值是60秒 # 一定要確保repl-timeout大于repl-ping-slave-period # repl-timeout 60################################## 安全 #################################### 設(shè)置客戶端連接后進(jìn)行任何其他指定前需要使用的密碼。 # 警告:因?yàn)閞edis速度相當(dāng)快,所以在一臺(tái)比較好的服務(wù)器下,一個(gè)外部的用戶可以在一秒鐘進(jìn)行150K次的密碼嘗試,這意味著你需要指定非常非常強(qiáng)大的密碼來(lái)防止暴力破解 # # requirepass foobared# 命令重命名. # # 在一個(gè)共享環(huán)境下可以重命名相對(duì)危險(xiǎn)的命令。比如把CONFIG重名為一個(gè)不容易猜測(cè)的字符。 # # 舉例: # # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52 # # 如果想刪除一個(gè)命令,直接把它重命名為一個(gè)空字符""即可,如下: # # rename-command CONFIG ""################################### 約束 ##################################### 設(shè)置同一時(shí)間最大客戶端連接數(shù),默認(rèn)無(wú)限制,Redis可以同時(shí)打開的客戶端連接數(shù)為Redis進(jìn)程可以打開的最大文件描述符數(shù), # 如果設(shè)置 maxclients 0,表示不作限制。 # 當(dāng)客戶端連接數(shù)到達(dá)限制時(shí),Redis會(huì)關(guān)閉新的連接并向客戶端返回max number of clients reached錯(cuò)誤信息 # # maxclients 128# 指定Redis最大內(nèi)存限制,Redis在啟動(dòng)時(shí)會(huì)把數(shù)據(jù)加載到內(nèi)存中,達(dá)到最大內(nèi)存后,Redis會(huì)先嘗試清除已到期或即將到期的Key # Redis同時(shí)也會(huì)移除空的list對(duì)象 # # 當(dāng)此方法處理后,仍然到達(dá)最大內(nèi)存設(shè)置,將無(wú)法再進(jìn)行寫入操作,但仍然可以進(jìn)行讀取操作 # # 注意:Redis新的vm機(jī)制,會(huì)把Key存放內(nèi)存,Value會(huì)存放在swap區(qū) # # maxmemory的設(shè)置比較適合于把redis當(dāng)作于類似memcached的緩存來(lái)使用,而不適合當(dāng)做一個(gè)真實(shí)的DB。 # 當(dāng)把Redis當(dāng)做一個(gè)真實(shí)的數(shù)據(jù)庫(kù)使用的時(shí)候,內(nèi)存使用將是一個(gè)很大的開銷 # maxmemory <bytes># 當(dāng)內(nèi)存達(dá)到最大值的時(shí)候Redis會(huì)選擇刪除哪些數(shù)據(jù)?有五種方式可供選擇 # # volatile-lru -> 利用LRU算法移除設(shè)置過(guò)過(guò)期時(shí)間的key (LRU:最近使用 Least Recently Used ) # allkeys-lru -> 利用LRU算法移除任何key # volatile-random -> 移除設(shè)置過(guò)過(guò)期時(shí)間的隨機(jī)key # allkeys->random -> remove a random key, any key # volatile-ttl -> 移除即將過(guò)期的key(minor TTL) # noeviction -> 不移除任何可以,只是返回一個(gè)寫錯(cuò)誤 # # 注意:對(duì)于上面的策略,如果沒有合適的key可以移除,當(dāng)寫的時(shí)候Redis會(huì)返回一個(gè)錯(cuò)誤 # # 寫命令包括: set setnx setex append # incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd # sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby # zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby # getset mset msetnx exec sort # # 默認(rèn)是: # # maxmemory-policy volatile-lru# LRU 和 minimal TTL 算法都不是精準(zhǔn)的算法,但是相對(duì)精確的算法(為了節(jié)省內(nèi)存),隨意你可以選擇樣本大小進(jìn)行檢測(cè)。 # Redis默認(rèn)的灰選擇3個(gè)樣本進(jìn)行檢測(cè),你可以通過(guò)maxmemory-samples進(jìn)行設(shè)置 # # maxmemory-samples 3############################## AOF ################################ 默認(rèn)情況下,redis會(huì)在后臺(tái)異步的把數(shù)據(jù)庫(kù)鏡像備份到磁盤,但是該備份是非常耗時(shí)的,而且備份也不能很頻繁,如果發(fā)生諸如拉閘限電、拔插頭等狀況,那么將造成比較大范圍的數(shù)據(jù)丟失。 # 所以redis提供了另外一種更加高效的數(shù)據(jù)庫(kù)備份及災(zāi)難恢復(fù)方式。 # 開啟append only模式之后,redis會(huì)把所接收到的每一次寫操作請(qǐng)求都追加到appendonly.aof文件中,當(dāng)redis重新啟動(dòng)時(shí),會(huì)從該文件恢復(fù)出之前的狀態(tài)。 # 但是這樣會(huì)造成appendonly.aof文件過(guò)大,所以redis還支持了BGREWRITEAOF指令,對(duì)appendonly.aof 進(jìn)行重新整理。 # 你可以同時(shí)開啟asynchronous dumps 和 AOFappendonly no# AOF文件名稱 (默認(rèn): "appendonly.aof") # appendfilename appendonly.aof# Redis支持三種同步AOF文件的策略: # # no: 不進(jìn)行同步,系統(tǒng)去操作 . Faster. # always: always表示每次有寫操作都進(jìn)行同步. Slow, Safest. # everysec: 表示對(duì)寫操作進(jìn)行累積,每秒同步一次. Compromise. # # 默認(rèn)是"everysec",按照速度和安全折中這是最好的。 # 如果想讓Redis能更高效的運(yùn)行,你也可以設(shè)置為"no",讓操作系統(tǒng)決定什么時(shí)候去執(zhí)行 # 或者相反想讓數(shù)據(jù)更安全你也可以設(shè)置為"always" # # 如果不確定就用 "everysec".# appendfsync always appendfsync everysec # appendfsync no# AOF策略設(shè)置為always或者everysec時(shí),后臺(tái)處理進(jìn)程(后臺(tái)保存或者AOF日志重寫)會(huì)執(zhí)行大量的I/O操作 # 在某些Linux配置中會(huì)阻止過(guò)長(zhǎng)的fsync()請(qǐng)求。注意現(xiàn)在沒有任何修復(fù),即使fsync在另外一個(gè)線程進(jìn)行處理 # # 為了減緩這個(gè)問(wèn)題,可以設(shè)置下面這個(gè)參數(shù)no-appendfsync-on-rewrite # # This means that while another child is saving the durability of Redis is # the same as "appendfsync none", that in pratical terms means that it is # possible to lost up to 30 seconds of log in the worst scenario (with the # default Linux settings). # # If you have latency problems turn this to "yes". Otherwise leave it as # "no" that is the safest pick from the point of view of durability. no-appendfsync-on-rewrite no# Automatic rewrite of the append only file. # AOF 自動(dòng)重寫 # 當(dāng)AOF文件增長(zhǎng)到一定大小的時(shí)候Redis能夠調(diào)用 BGREWRITEAOF 對(duì)日志文件進(jìn)行重寫 # # 它是這樣工作的:Redis會(huì)記住上次進(jìn)行些日志后文件的大小(如果從開機(jī)以來(lái)還沒進(jìn)行過(guò)重寫,那日子大小在開機(jī)的時(shí)候確定) # # 基礎(chǔ)大小會(huì)同現(xiàn)在的大小進(jìn)行比較。如果現(xiàn)在的大小比基礎(chǔ)大小大制定的百分比,重寫功能將啟動(dòng) # 同時(shí)需要指定一個(gè)最小大小用于AOF重寫,這個(gè)用于阻止即使文件很小但是增長(zhǎng)幅度很大也去重寫AOF文件的情況 # 設(shè)置 percentage 為0就關(guān)閉這個(gè)特性auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb################################## SLOW LOG #################################### Redis Slow Log 記錄超過(guò)特定執(zhí)行時(shí)間的命令。執(zhí)行時(shí)間不包括I/O計(jì)算比如連接客戶端,返回結(jié)果等,只是命令執(zhí)行時(shí)間 # # 可以通過(guò)兩個(gè)參數(shù)設(shè)置slow log:一個(gè)是告訴Redis執(zhí)行超過(guò)多少時(shí)間被記錄的參數(shù)slowlog-log-slower-than(微妙), # 另一個(gè)是slow log 的長(zhǎng)度。當(dāng)一個(gè)新命令被記錄的時(shí)候最早的命令將被從隊(duì)列中移除# 下面的時(shí)間以微妙微單位,因此1000000代表一分鐘。 # 注意制定一個(gè)負(fù)數(shù)將關(guān)閉慢日志,而設(shè)置為0將強(qiáng)制每個(gè)命令都會(huì)記錄 slowlog-log-slower-than 10000# 對(duì)日志長(zhǎng)度沒有限制,只是要注意它會(huì)消耗內(nèi)存 # 可以通過(guò) SLOWLOG RESET 回收被慢日志消耗的內(nèi)存 slowlog-max-len 1024################################ VM ################################## WARNING! Virtual Memory is deprecated in Redis 2.4 ### The use of Virtual Memory is strongly discouraged.# Virtual Memory allows Redis to work with datasets bigger than the actual # amount of RAM needed to hold the whole dataset in memory. # In order to do so very used keys are taken in memory while the other keys # are swapped into a swap file, similarly to what operating systems do # with memory pages. # # To enable VM just set 'vm-enabled' to yes, and set the following three # VM parameters accordingly to your needs.vm-enabled no # vm-enabled yes# This is the path of the Redis swap file. As you can guess, swap files # can't be shared by different Redis instances, so make sure to use a swap # file for every redis process you are running. Redis will complain if the # swap file is already in use. # # The best kind of storage for the Redis swap file (that's accessed at random) # is a Solid State Disk (SSD). # # *** WARNING *** if you are using a shared hosting the default of putting # the swap file under /tmp is not secure. Create a dir with access granted # only to Redis user and configure Redis to create the swap file there. vm-swap-file /tmp/redis.swap# vm-max-memory configures the VM to use at max the specified amount of # RAM. Everything that deos not fit will be swapped on disk *if* possible, that # is, if there is still enough contiguous space in the swap file. # # With vm-max-memory 0 the system will swap everything it can. Not a good # default, just specify the max amount of RAM you can in bytes, but it's # better to leave some margin. For instance specify an amount of RAM # that's more or less between 60 and 80% of your free RAM. vm-max-memory 0# Redis swap files is split into pages. An object can be saved using multiple # contiguous pages, but pages can't be shared between different objects. # So if your page is too big, small objects swapped out on disk will waste # a lot of space. If you page is too small, there is less space in the swap # file (assuming you configured the same number of total swap file pages). # # If you use a lot of small objects, use a page size of 64 or 32 bytes. # If you use a lot of big objects, use a bigger page size. # If unsure, use the default :) vm-page-size 32# Number of total memory pages in the swap file. # Given that the page table (a bitmap of free/used pages) is taken in memory, # every 8 pages on disk will consume 1 byte of RAM. # # The total swap size is vm-page-size * vm-pages # # With the default of 32-bytes memory pages and 134217728 pages Redis will # use a 4 GB swap file, that will use 16 MB of RAM for the page table. # # It's better to use the smallest acceptable value for your application, # but the default is large in order to work in most conditions. vm-pages 134217728# Max number of VM I/O threads running at the same time. # This threads are used to read/write data from/to swap file, since they # also encode and decode objects from disk to memory or the reverse, a bigger # number of threads can help with big objects even if they can't help with # I/O itself as the physical device may not be able to couple with many # reads/writes operations at the same time. # # The special value of 0 turn off threaded I/O and enables the blocking # Virtual Memory implementation. vm-max-threads 4############################### ADVANCED CONFIG ################################ 當(dāng)hash中包含超過(guò)指定元素個(gè)數(shù)并且最大的元素沒有超過(guò)臨界時(shí), # hash將以一種特殊的編碼方式(大大減少內(nèi)存使用)來(lái)存儲(chǔ),這里可以設(shè)置這兩個(gè)臨界值 # Redis Hash對(duì)應(yīng)Value內(nèi)部實(shí)際就是一個(gè)HashMap,實(shí)際這里會(huì)有2種不同實(shí)現(xiàn), # 這個(gè)Hash的成員比較少時(shí)Redis為了節(jié)省內(nèi)存會(huì)采用類似一維數(shù)組的方式來(lái)緊湊存儲(chǔ),而不會(huì)采用真正的HashMap結(jié)構(gòu),對(duì)應(yīng)的value redisObject的encoding為zipmap, # 當(dāng)成員數(shù)量增大時(shí)會(huì)自動(dòng)轉(zhuǎn)成真正的HashMap,此時(shí)encoding為ht。 hash-max-zipmap-entries 512 hash-max-zipmap-value 64# list數(shù)據(jù)類型多少節(jié)點(diǎn)以下會(huì)采用去指針的緊湊存儲(chǔ)格式。 # list數(shù)據(jù)類型節(jié)點(diǎn)值大小小于多少字節(jié)會(huì)采用緊湊存儲(chǔ)格式。 list-max-ziplist-entries 512 list-max-ziplist-value 64# set數(shù)據(jù)類型內(nèi)部數(shù)據(jù)如果全部是數(shù)值型,且包含多少節(jié)點(diǎn)以下會(huì)采用緊湊格式存儲(chǔ)。 set-max-intset-entries 512# zsort數(shù)據(jù)類型多少節(jié)點(diǎn)以下會(huì)采用去指針的緊湊存儲(chǔ)格式。 # zsort數(shù)據(jù)類型節(jié)點(diǎn)值大小小于多少字節(jié)會(huì)采用緊湊存儲(chǔ)格式。 zset-max-ziplist-entries 128 zset-max-ziplist-value 64# Redis將在每100毫秒時(shí)使用1毫秒的CPU時(shí)間來(lái)對(duì)redis的hash表進(jìn)行重新hash,可以降低內(nèi)存的使用 # # 當(dāng)你的使用場(chǎng)景中,有非常嚴(yán)格的實(shí)時(shí)性需要,不能夠接受Redis時(shí)不時(shí)的對(duì)請(qǐng)求有2毫秒的延遲的話,把這項(xiàng)配置為no。 # # 如果沒有這么嚴(yán)格的實(shí)時(shí)性要求,可以設(shè)置為yes,以便能夠盡可能快的釋放內(nèi)存 activerehashing yes################################## INCLUDES #################################### 指定包含其它的配置文件,可以在同一主機(jī)上多個(gè)Redis實(shí)例之間使用同一份配置文件,而同時(shí)各個(gè)實(shí)例又擁有自己的特定配置文件# include /path/to/local.conf # include /path/to/other.conf redis.conf詳解啟動(dòng)slave從節(jié)點(diǎn)的redis服務(wù)
[root@master 192.168.119.10 ~]$redis-server /etc/redis-6380.conf [root@master 192.168.119.10 ~]$redis-server /etc/redis-6381.conf驗(yàn)證從節(jié)點(diǎn)的redis服務(wù)
[root@master ~]$redis-cli -p 6380 ping PONG [root@master ~]$redis-cli -p 6381 ping PONG確定主從關(guān)系
在主節(jié)點(diǎn)上查看主從通信關(guān)系
[root@master ~]# redis-cli -p 6379 info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.119.10,port=6380,state=online,offset=407,lag=0 slave1:ip=192.168.119.10,port=6381,state=online,offset=407,lag=0 master_repl_offset:407 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:406在從節(jié)點(diǎn)上查看主從關(guān)系(6380、6379)
[root@slave 192.168.119.11 ~]$redis-cli -p 6380 info replication # Replication role:slave master_host:192.168.119.10 master_port:6379 master_link_status:up master_last_io_seconds_ago:3 master_sync_in_progress:0 slave_repl_offset:505 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此時(shí)可以在master上寫入數(shù)據(jù),在slave上查看數(shù)據(jù),此時(shí)主從復(fù)制配置完成
開始配置Redis Sentinel
超哥實(shí)驗(yàn)的環(huán)境是單獨(dú)一臺(tái)linux,192.168.119.10
[root@master tmp]# ll /etc/redis-* -rw-r--r-- 1 root root 145 Nov 7 17:44 /etc/redis-6379.conf -rw-r--r-- 1 root root 93 Nov 7 17:42 /etc/redis-6380.conf -rw-r--r-- 1 root root 115 Nov 7 17:42 /etc/redis-6381.conf -rw-r--r-- 1 root root 556 Nov 7 17:42 /etc/redis-sentinel-26379.conf -rw-r--r-- 1 root root 556 Nov 7 17:42 /etc/redis-sentinel-26380.conf -rw-r--r-- 1 root root 556 Nov 7 17:42 /etc/redis-sentinel-26381.confredis-sentinel-26379.conf配置文件寫入如下信息
// Sentinel節(jié)點(diǎn)的端口 port 26379 dir /var/redis/data/ logfile "26379.log"// 當(dāng)前Sentinel節(jié)點(diǎn)監(jiān)控 192.168.119.10:6379 這個(gè)主節(jié)點(diǎn) // 2代表判斷主節(jié)點(diǎn)失敗至少需要2個(gè)Sentinel節(jié)點(diǎn)節(jié)點(diǎn)同意 // mymaster是主節(jié)點(diǎn)的別名 sentinel monitor mymaster 192.168.119.10 6379 2//每個(gè)Sentinel節(jié)點(diǎn)都要定期PING命令來(lái)判斷Redis數(shù)據(jù)節(jié)點(diǎn)和其余Sentinel節(jié)點(diǎn)是否可達(dá),如果超過(guò)30000毫秒30s且沒有回復(fù),則判定不可達(dá) sentinel down-after-milliseconds mymaster 30000//當(dāng)Sentinel節(jié)點(diǎn)集合對(duì)主節(jié)點(diǎn)故障判定達(dá)成一致時(shí),Sentinel領(lǐng)導(dǎo)者節(jié)點(diǎn)會(huì)做故障轉(zhuǎn)移操作,選出新的主節(jié)點(diǎn),原來(lái)的從節(jié)點(diǎn)會(huì)向新的主節(jié)點(diǎn)發(fā)起復(fù)制操作,限制每次向新的主節(jié)點(diǎn)發(fā)起復(fù)制操作的從節(jié)點(diǎn)個(gè)數(shù)為1 sentinel parallel-syncs mymaster 1//故障轉(zhuǎn)移超時(shí)時(shí)間為180000毫秒 sentinel failover-timeout mymaster 180000 redis-sentinel-26380.conf和redis-sentinel-26381.conf的配置僅僅差異是port(端口)的不同。
然后啟動(dòng)三個(gè)sentinel哨兵 redis-sentinel /etc/redis-sentinel-26379.conf redis-sentinel /etc/redis-sentinel-26380.conf redis-sentinel /etc/redis-sentinel-26381.conf
?
監(jiān)控拓?fù)鋱D
?
此時(shí)查看哨兵是否成功通信
[root@master ~]# 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=192.168.119.10:6379,slaves=2,sentinels=3#看到最后一條信息正確即成功了哨兵,哨兵主節(jié)點(diǎn)名字叫做mymaster,狀態(tài)ok,監(jiān)控地址是192.168.119.10:6379,有兩個(gè)從節(jié)點(diǎn),3個(gè)哨兵
redis高可用故障實(shí)驗(yàn)
大致思路
- 殺掉主節(jié)點(diǎn)的redis進(jìn)程6379端口,觀察從節(jié)點(diǎn)是否會(huì)進(jìn)行新的master選舉,進(jìn)行切換
- 重新恢復(fù)舊的“master”節(jié)點(diǎn),查看此時(shí)的redis身份
?首先查看三個(gè)redis的進(jìn)程狀態(tài)
ps -ef|grep redis檢查三個(gè)節(jié)點(diǎn)的復(fù)制身份狀態(tài)
第一個(gè)
[root@master tmp]# redis-cli -p 6381 info replication # Replication role:slave master_host:127.0.0.1 master_port:6380第二個(gè)
[root@master tmp]# redis-cli -p 6380 info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6381,state=online,offset=54386,lag=0 slave1:ip=127.0.0.1,port=6379,state=online,offset=54253,lag=0第三個(gè)
[root@master tmp]# redis-cli -p 6379 info replication # Replication role:slave master_host:127.0.0.1 master_port:6380此時(shí),干掉master!!!然后等待其他兩個(gè)節(jié)點(diǎn)是否能自動(dòng)被哨兵sentienl,切換為master節(jié)點(diǎn)
?
ps -ef|grep 6380 #干掉master進(jìn)程?
此時(shí)查看兩個(gè)slave的狀態(tài)
精髓就是查看一個(gè)參數(shù)
master_link_down_since_seconds:13?
稍等片刻之后,發(fā)現(xiàn)slave節(jié)點(diǎn)成為master節(jié)點(diǎn)!!
[root@master tmp]# redis-cli -p 6379 info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6381,state=online,offset=41814,lag=1大功告成!!開心!!!
轉(zhuǎn)載于:https://www.cnblogs.com/lingcai/p/10447275.html
總結(jié)
以上是生活随笔為你收集整理的redis-sentinel 主从复制高可用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 关于读构建之法后的疑惑
- 下一篇: js获取浏览器宽度和高度值