百萬PV架構中redis緩存服務羣集部署

redis簡介

redis是一個key-value存儲系統。和Memcached類似,它支持存儲的value類型相對更多,包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hash(哈希類型)。這些數據類型都支持push/pop、add/remove及取交集並集和差集及更豐富的操作,而且這些操作都是原子性的。在此基礎上,redis支持各種不同方式的排序。與memcached一樣,爲了保證效率,數據都是緩存在內存中。區別的是redis會週期性的把更新的數據寫入磁盤或者把修改操作寫入追加的記錄文件,並且在此基礎上實現了master-slave(主從)同步。

案例環境

主機 IP地址 角色
Centos7 192.168.71.134 redis-master
Centos7 192.168.71.129 redis-slave

搭建過程

1、主從安裝redis
[root@localhost ~]# yum install epel-release -y
[root@localhost ~]# yum install redis -y
2、主服務器修改配置文件

[root@localhost ~]# vim /etc/redis.conf
bind 0.0.0.0           #監聽任意端口

3、從服務器修改配置文件

[root@localhost ~]# vim /etc/redis.conf
bind 0.0.0.0           #監聽任意端口
# slaveof <masterip> <masterport>
slaveof 192.168.71.128 6379    #指向主服務器的ip和端口

4、主從服務器開啓服務

[root@localhost ~]# systemctl start redis

百萬PV架構中redis緩存服務羣集部署
5、驗證同步功能(記得關閉防火牆)

主服務器上登陸並寫入內容
[root@localhost ~]# redis-cli -h 192.168.71.134 -p 6379
192.168.71.134:6379> set abc 111
OK
192.168.71.134:6379> get abc
"111"


從服務器登陸查看內容
[root@localhost ~]# redis-cli -h 192.168.71.129 -p 6379
192.168.71.129:6379> get abc
"111"

同步功能到這步算完成了,接下來搭建主從功能

6、主服務器主從設置

[root@localhost ~]# vim /etc/redis-sentinel.conf
17行 protected-mode no
69行 sentinel monitor mymaster 192.168.71.134 6379 1 #master的地址,端口,從服務器1臺
98行 sentinel down-after-milliseconds mymaster 300 #設置切換時間

7、開啓服務(從服務器不需要做任何配置,只需開啓服務)

[root@localhost ~]# systemctl start redis-sentinel.service

8、主服務器上查看主從狀態
[root@localhost ~]# redis-cli -h 192.168.71.134 -p 26379 info sentinel
百萬PV架構中redis緩存服務羣集部署

9、宕掉一臺redis,再次查看主從狀態
[root@localhost ~]# systemctl stop redis
百萬PV架構中redis緩存服務羣集部署

10、再次開啓服務後master不會回到主服務上,再次宕掉從服務器即可

root@localhost ~]# systemctl stop redis   #從服務器宕掉redis
[root@localhost ~]# systemctl start redis  #主服務器開啓redis
[root@localhost ~]# redis-cli -h 192.168.71.134 -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.71.134:6379,slaves=1,sentinels=3  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章