百万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  

猜你喜欢

转载自blog.51cto.com/13760226/2312672