Redis集群
-
主從復(fù)制
為了避免單臺(tái)機(jī)器故障導(dǎo)致的數(shù)據(jù)丟失,我們需要將數(shù)據(jù)復(fù)制多份部署到多臺(tái)不同的服務(wù)器上潦闲,這樣即使一臺(tái)服務(wù)器出現(xiàn)故障垦沉,別的服務(wù)器依然能提供服務(wù)辈讶。 這也要求一臺(tái)服務(wù)器數(shù)據(jù)更新以后,自動(dòng)同步更新到其他服務(wù)器上犀被。
-
如何實(shí)現(xiàn)
Redis 提供了復(fù)制(Replication)功能來(lái)實(shí)現(xiàn)多臺(tái) Redis 服務(wù)器的數(shù)據(jù)同步
通過(guò)部署多臺(tái) Redis 機(jī)器椅您,并在配置文件中指定者幾臺(tái) Redis 服務(wù)器之間的主從關(guān)系。主負(fù)責(zé)寫入數(shù)據(jù)寡键,同時(shí)將數(shù)據(jù)實(shí)時(shí)同步到從機(jī)器上掀泳。Redis默認(rèn)Master用于寫,Slave用于讀西轩。向 Slave 寫數(shù)據(jù)會(huì)導(dǎo)致錯(cuò)誤员舵。
方式一: 修改配置文件,啟動(dòng)時(shí)遭商,服務(wù)器讀取配置文件固灵,并自動(dòng)成為指定服務(wù)器的從服務(wù)器,從而構(gòu)成主從復(fù)制關(guān)系
啟動(dòng)
redis-sever /etc/redis6380.conf
//啟動(dòng)masterredis-sever /etc/redis6381.conf
//啟動(dòng)slaver1-
redis-sever /etc/redis6382.conf
//啟動(dòng)slaver2Redis Master配置
#導(dǎo)入原始配置文件 include /etc/redis/redis.conf #守護(hù)進(jìn)程 daemonize yes #端口 port 6380 #進(jìn)程文件 pidfile /var/run/redis/redis_6380.pid #日志文件 logfile redis-6380.log #RDB文件 dbfilename redis-dump-6380.rdb
Redis slave 配置
#導(dǎo)入原始配置文件 include /etc/redis/redis.conf #守護(hù)進(jìn)程 daemonize yes #端口 port 6381 #進(jìn)程文件 pidfile /var/run/redis/redis_6381.pid #日志文件 logfile redis-6381.log #RDB文件 dbfilename redis-dump-6381.rdb #配置目前的配置是那一臺(tái)master的slave slaveof 127.0.0.1 6380
查看當(dāng)前機(jī)器的角色
``` #master機(jī)器信息 root@aaron:/home/aaron# redis-cli -p 6380 127.0.0.1:6380> INFO replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6381,state=online,offset=476,lag=1 slave1:ip=127.0.0.1,port=6383,state=online,offset=476,lag=1 master_replid:aa778915d498ad1ea8a5c44cf7aa73e238a95956 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:476 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:476 ----------------------------------------------- #slaver信息 127.0.0.1:6383> info replication # Replication role:slave master_host:127.0.0.1 master_port:6380 master_link_status:up master_last_io_seconds_ago:9 master_sync_in_progress:0 slave_repl_offset:1246 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:aa778915d498ad1ea8a5c44cf7aa73e238a95956 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:1246 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:253 repl_backlog_histlen:994 ```
方式二: redis-server --slaveof <master-ip> <master-port>, 在啟動(dòng) Redis 時(shí)指定當(dāng)前服務(wù)成為某個(gè)主Redis服務(wù)的從Slave
容災(zāi)處理
當(dāng) master 機(jī)器出現(xiàn)故障時(shí)(模擬時(shí)殺掉redis master服務(wù)進(jìn)程)劫流,需要手動(dòng)將Slave中的一個(gè)提升為Master, 剩下的slav配置到新的Master上(冷處理)
將6381端口的 Slave 提升 master
127.0.0.1:6381> SLAVEOF no one OK
將6382的master設(shè)置為6381
127.0.0.1:6382> SLAVEOF 127.0.0.1 6381 OK
-
高可用 Sentinel 哨兵
Sentinel 時(shí)官方提供的用于監(jiān)控多個(gè)Redis服務(wù)實(shí)例運(yùn)行情況的解決方案巫玻。
監(jiān)控
Sentinel配置
#復(fù)制四份sentinel.conf文件
- sentinel26380.conf
- sentinel26381.conf
- sentinel26382.conf
- sentinel26383.conf
#修改sentinel2638*.conf中的兩個(gè)配置項(xiàng)
port 26380 //sentinel26380.conf
port 26381 //sentinel26381.conf
port 26382 //sentinel26382.conf
port 26383 //sentinel26383.conf
#修改sentinel監(jiān)視配置項(xiàng)
<!---->
# name masterIP masterPort 哨兵投票數(shù)
sentinel monitor mymaster 127.0.0.1 6381 2
#創(chuàng)建監(jiān)視主服務(wù)器的Sentinel實(shí)例
redis-sentinel /etc/redis/sentinel26380.conf
redis-sentinel /etc/redis/sentinel26381.conf
redis-sentinel /etc/redis/sentinel26382.conf
redis-sentinel /etc/redis/sentinel26383.conf
安全
- 設(shè)置密碼
redis.conf 文件中設(shè)置 requirepass yourPassword
- 綁定ip
#只允許本機(jī)登陸
bind 127.0.0.1
- 命令禁止或是重命名
#重命名flushall為asdfg
#注意:對(duì)于flushall命令,需要保證appendonly.aof文件沒(méi)有flushall命令祠汇,否則服務(wù)器無(wú)法啟動(dòng)
rename-command flushall asdfg
#禁止使用flushdb命令
rename-command flushdb ""
#禁止使用config命令
rename-command config ""