一、主從復(fù)制

Rdis 的主從復(fù)制特點

1. 配置主從
實現(xiàn)方式同樣有兩種: 命令方式和配置文件方式
命令方式
只需要在從服務(wù)器上執(zhí)行如下命令即可
slaveof 主服務(wù)器的IP 端口號
slaveof命令是異步的,不阻塞。
并且此時,從服務(wù)器現(xiàn)有的數(shù)據(jù)會先被清空,之后再同步主服務(wù)器的數(shù)據(jù)。
停止一臺從服務(wù)器的復(fù)制操作,在此臺服務(wù)器上執(zhí)行如下命令
slaveof no one
配置文件的方式如下
只需要在從服務(wù)器上配置即可
修改配置文件
假如主服務(wù)器 IP 是: 172.16.153.178
端口是: 6379
# slaveof <masterip> <masterport>
slaveof 172.16.153.178 6379
// 配置此服務(wù)器只提供讀取操作
slave-read-only yes
之后重啟從主機(jī)的 Redis 服務(wù)
查看主從信息
127.0.0.1:6379> info replication
二、主從 + Sentinel 哨兵模式
Redis Sentinel是Redis官方的高可用性解決方案。
Redis 的 Sentinel 系統(tǒng)用于管理多個 Redis 服務(wù)器(instance), 該系統(tǒng)執(zhí)行以下三個任務(wù):
監(jiān)控(Monitoring): Sentinel 會不斷地檢查你的主服務(wù)器和從服務(wù)器是否運(yùn)作正常。
提醒(Notification): 當(dāng)被監(jiān)控的某個 Redis 服務(wù)器出現(xiàn)問題時, Sentinel 可以通過 API 向管理員或者其他應(yīng)用程序發(fā)送通知。
自動故障遷移(Automatic failover): 當(dāng)一個主服務(wù)器不能正常工作時, Sentinel 會開始一次自動故障遷移操作, 它會將主從復(fù)制組合中的其中一個從服務(wù)器升級為新的主服務(wù)器, 并讓其他從服務(wù)器指向新的主服務(wù)器; 當(dāng)客戶端試圖連接失效的主服務(wù)器時, Sentinel也會向客戶端返回新主服務(wù)器的地址, 使得新主服務(wù)器代替失效服務(wù)器。
Redis Sentinel 是一個分布式系統(tǒng), 你可以在一個架構(gòu)中運(yùn)行多個 Sentinel 進(jìn)程(progress), 這些進(jìn)程使用流言協(xié)議(gossip protocols)來接收關(guān)于主服務(wù)器是否下線的信息, 并使用投票協(xié)議(agreement protocols)來決定是否執(zhí)行自動故障遷移, 以及選擇哪個從服務(wù)器作為新的主服務(wù)器。
雖然 Redis 為 Sentinel 生成一個單獨(dú)的可執(zhí)行文件 redis-sentinel , 但實際上它只是一個運(yùn)行在特殊模式下的 Redis 服務(wù)器。
此種模式下,客戶端要訪問的 服務(wù) IP 不是主節(jié)點,而是 sentiner 服務(wù)器的 IP。
架構(gòu)圖

Redis Sentinel 故障轉(zhuǎn)移

架構(gòu)的擴(kuò)展應(yīng)用

1. 配置主從
a. 快速生成主節(jié)點的配置文件
編譯全新文件 /etc/redis/redis-6380.conf, 添加如下內(nèi)容
port 6380
daemonize yes
protected-mode no
pidfile /var/run/redis-6380.pid
logfile /var/log/redis-6380.log
dir /redis/data/
假如是多個主機(jī)實現(xiàn)的,就需要更改為
protected-mode yes,
并且添加bind 0.0.0.0
b. 快速生成從節(jié)點的配置文件
[root@s1 ~]# sed 's/6380/6381/g' /etc/redis/redis-6380.conf > /etc/redis/redis-6381.conf
[root@s1 ~]# sed 's/6380/6382/g' /etc/redis/redis-6380.conf > /etc/redis/redis-6382.conf
查看配置文件內(nèi)容,檢驗配置結(jié)果
[root@s1 ~]# cat /etc/redis/redis-6381.conf
port 6381
daemonize yes
pidfile /var/run/redis-6381.pid
logfile /var/log/redis-6381.log
dir /redis/data/
[root@s1 ~]# cat /etc/redis/redis-6382.conf
port 6382
daemonize yes
pidfile /var/run/redis-6382.pid
logfile /var/log/redis-6382.log
dir /redis/data/
[root@s1 ~]#
c. 配置主從關(guān)系
[root@s1 ~]# echo "slaveof 172.16.153.178 6380" >> /etc/redis/redis-6381.conf
[root@s1 ~]# echo "slaveof 172.16.153.178 6380" >> /etc/redis/redis-6382.conf
[root@s1 ~]#
高版本將 slaveof 更換成 replicaof
d. 啟動服務(wù),并驗證進(jìn)程
[root@s1 ~]# /usr/local/bin/redis-server /etc/redis/redis-6380.conf
[root@s1 ~]# /usr/local/bin/redis-server /etc/redis/redis-6381.conf
[root@s1 ~]# /usr/local/bin/redis-server /etc/redis/redis-6382.conf
[root@s1 ~]# ps -ef |grep redis
root 4335 1 0 19:30 ? 00:00:03 /usr/local/bin/redis-server *:6380
root 4490 1 0 20:17 ? 00:00:00 /usr/local/bin/redis-server *:6381
root 4495 1 0 20:17 ? 00:00:00 /usr/local/bin/redis-server *:6382
root 4500 3755 0 20:17 pts/0 00:00:00 grep --color=auto redis
[root@s1 ~]#
假如日志中出現(xiàn)如下警告信息
4668:S 17 Feb 20:28:42.107 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4668:S 17 Feb 20:28:42.107 # Server initialized
4668:S 17 Feb 20:28:42.108 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
4668:S 17 Feb 20:28:42.108 * DB loaded from disk: 0.000 seconds
4668:S 17 Feb 20:28:42.110 * Before turning into a slave, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
解決辦法
The TCP backlog...
方法1: 臨時設(shè)置生效:
shell> sysctl -w net.core.somaxconn=1024
方法2: 永久生效:
修改/etc/sysctl.conf文件,增加一行
net.core.somaxconn=1024
然后執(zhí)行命令
sysctl -p
WARNING overcommit_memory ...
方法1: 臨時設(shè)置生效:
shell> sysctl -w vm.overcommit_memory=1
方法2: 永久生效:
修改/etc/sysctl.conf文件,增加一行
vm.overcommit_memory=1
然后執(zhí)行命令
sysctl -p
e. 查看主從復(fù)制信息

2. 配置 sentinel
獲取程序
Sentinel 程序可以在編譯后的 src 文檔中發(fā)現(xiàn), 它是一個命名為 redis-sentinel 的程序。
運(yùn)行一個 Sentinel 所需的最少配置如下所示:
Redis 源碼中包含了一個名為 sentinel.conf 的文件, 這個文件是一個帶有詳細(xì)注釋的 Sentinel 配置文件示例。
運(yùn)行一個 Sentinel 所需的最少配置如下所示:
// 監(jiān)控一個 Redis 服務(wù)器
// 名稱為 mymaster ,IP 為 127.0.0.1 端口為 6380
// 最后的 2 是指最少有 2 給 Sentinel 實例同意一臺 redis 服務(wù)器宕機(jī),才會認(rèn)為 客觀下線。
// sentinel monitor 自定義的主節(jié)點名稱 主節(jié)點的 IP 主節(jié)點端口 票數(shù)
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel down-after-milliseconds mymaster 3000
// 180 秒后開始故障自動裝換
sentinel failover-timeout mymaster 5000
sentinel parallel-syncs mymaster 1
各個選項的功能如下:
down-after-milliseconds 選項指定了 Sentinel 認(rèn)為服務(wù)器已經(jīng)斷線所需的毫秒數(shù)。
如果服務(wù)器在給定的毫秒數(shù)之內(nèi), 沒有返回 Sentinel 發(fā)送的 PING 命令的回復(fù), 或者返回一個錯誤, 那么 Sentinel 將這個服務(wù)器標(biāo)記為主觀下線(subjectively down,簡稱 SDOWN )。不過只有一個 Sentinel 將服務(wù)器標(biāo)記為主觀下線并不一定會引起服務(wù)器的自動故障遷移: 只有在足夠數(shù)量的 Sentinel 都將一個服務(wù)器標(biāo)記為主觀下線之后, 服務(wù)器才會被標(biāo)記為客觀下線(objectively down, 簡稱 ODOWN ), 這時自動故障遷移才會執(zhí)行。
將服務(wù)器標(biāo)記為客觀下線所需的 Sentinel 數(shù)量由對主服務(wù)器的配置決定。
parallel-syncs選項指定了在執(zhí)行故障轉(zhuǎn)移時, 最多可以有多少個從服務(wù)器同時對新的主服務(wù)器進(jìn)行同步, 這個數(shù)字越小, 完成故障轉(zhuǎn)移所需的時間就越長。
最終的配置文件
sentinel-27000.cn
daemonize yes
port 27000
dir "/tmp"
logfile "27000.log"
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 5000
sentinel parallel-syncs mymaster 1
哨兵的領(lǐng)導(dǎo)者選舉
票數(shù)和領(lǐng)導(dǎo)者選舉有關(guān)系
領(lǐng)導(dǎo)者選舉的事件發(fā)生,必須滿足下面的條件
max(票數(shù), (哨兵的個數(shù) / 2) + 1 ) 個哨兵參加選舉
才可以選舉出領(lǐng)導(dǎo)者,從而完成故障轉(zhuǎn)移。
比如有 5 個哨兵, 配置的票數(shù)是 4
max(4, (5 / 2) + 1)
max(4, 3.5)
4 最大
結(jié)果就是需要 4 個哨兵參與選舉才可以。
a. 獲取并修改配置文件
快速創(chuàng)建三個 sentinel 配置文件
進(jìn)入到 Redis 源碼的目錄下,執(zhí)行如下命令

修改監(jiān)聽端口

之后在每個 sentinel 配置文件中添加守護(hù)進(jìn)程方式運(yùn)行,
并修改dir 配置項的目錄,
daemonize yes
dir /redis/data/
logfile "sentinel-${port}.log"
最后別忘了修改監(jiān)控的主服務(wù)器的 IP 和端口正確的 6380
最終其中一個的配置文件應(yīng)該是這樣的

b. 啟動服務(wù)并驗證
啟動服務(wù)的語法:
shell> redis-sentinel sentinel的配置文件


可以使用以下命令查看哨兵的信息
[root@s1 ~]# redis-cli -p 27001 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=127.0.0.1:6380,slaves=2,sentinels=3
[root@s1 ~]#
4. 故障演練
停止 Master 節(jié)點的服務(wù)
[root@s1 ~]# redis-cli -p 6380 shutdown
不斷的刷新其中一個 Sentinel 節(jié)點的信息,觀察最后一行信息的變化
[root@s1 ~]# redis-cli -p 27001 info
...略...
master0:name=mymaster,status=ok,address=127.0.0.1:6382,slaves=2,sentinels=3