redis哨兵/主从复制/读写分离

1.准备redis
将redis压缩安装到/opt/redis/redis-5.0.7

 

 

 2.拷贝redis.conf

将redis.conf文件到two-conf文件夹下,主文件命名为redis-6379.conf,从文件命名为redis-6380.conf  redis-6381.conf

cat redis.conf | grep -v "#" | grep -v "^$" >/opt/redis/redis-5.0.7/two-conf/redis-6379.conf
cat redis.conf | grep -v "#" | grep -v "^$" >/opt/redis/redis-5.0.7/two-conf/redis-6380.conf
cat redis.conf | grep -v "#" | grep -v "^$" >/opt/redis/redis-5.0.7/two-conf/redis-6381.conf

 

 

 3.修改主配置文件redis-6379.conf

将主配置文件redis-6379.conf中的:daemonize值修改为no

daemonize表示:redis守护线程:redis以守护线程的方式在后台启动.NO:当redis退出或者关闭连接工具,redis退出,YES:redis采用的是单进程多线程的模式,在该模式下redis会在后台运行,除非手动kill掉

 

 

 4.修改从配置文件redis-6380.conf  redis-6381.conf

  1.修改redis-6380.conf配置文件中的端口:port为 6380

  2.修改redis-6381.conf配置文件中的端口:port为 6381
  3.daemonize值修改为daemonize no
  4.添加slaveof 127.0.0.1 6379 (slaveof:表示当前机器为从属redis,从属与127.0.0.1 6379这个redis)

 

 

 5.准备哨兵

拷贝sentinel.conf文件到two-conf文件夹下,哨兵文件命名为:sentinel-26379.conf,sentinel-26380.conf ,sentinel-26381.conf
cat sentinel.conf | grep -v "#" | grep -v "^$" >/opt/redis/redis-5.0.7/two-conf/sentinel-26379.conf
cat sentinel.conf | grep -v "#" | grep -v "^$" >/opt/redis/redis-5.0.7/two-conf/sentinel-26380.conf
cat sentinel.conf | grep -v "#" | grep -v "^$" >/opt/redis/redis-5.0.7/two-conf/sentinel-26381.conf

 

 

 

6.修改哨兵配置文件sentinel-26379.conf,sentinel-26380.conf,sentinel-26381.conf
1.修改sentinel-26379.conf配置文件中的端口:port为 26379
2.修改sentinel-26380.conf配置文件中的端口:port为 26380
3.修改sentinel-26381.conf配置文件中的端口:port为 26381

 

 

 

7.启动主redis
./redis-server /opt/redis/redis-5.0.7/two-conf/redis-6379.conf

 

 

 

 

8.启动从redis
./redis-server /opt/redis/redis-5.0.7/two-conf/redis-6380.conf

从服务器变化

 此时slave从服务器6380,从服务器开启成功后,开始连接主服务器,触发同步非阻塞事件,主服务器回应复制可以继续,选择是部分复制还是全量复制,接收主服务器的字节数,刷新旧数据,将新数据加载到内存中,数据同步成功。

 

 主服务器变化

 主服务器同意复制并记录复制过程信息。

 

 

9.启动哨兵
./redis-sentinel /opt/redis/redis-5.0.7/two-conf/sentinel-26379.conf

 

哨兵启动后会生成一个sentinel ID(哨兵唯一标识,用于互相通信),哨兵开始监控主redis6379,并启用选取过半票数,同时开始监听从服务器6380。

 

 

10.启动客户端
./redis-cli -p 26379   #哨兵模式查看     并执行info命令

 

 此时监听了主服务器 6379,一个从服务器,互相通信有一个哨兵。

 

 

 

11.启动哨兵

./redis-sentinel /opt/redis/redis-5.0.7/two-conf/sentinel-26381.conf

 

 哨兵26381生成了字段的唯一ID,开始监听主服务器6379并同时开始监听两台从服务器6380,6381,并与另外两台哨兵建立相互通信。

 

第一个哨兵变化

 

开始同步26381的信息,并建立通信。

 

 11.执行info命令

 

 

.启动哨兵
./redis-sentinel /opt/redis/redis-5.0.7/two-conf/sentinel-26379.conf

 

 

 

 

 

 11.断开从服务器

主服务器变化

 

 

 

 哨兵变化

 

 

12.关闭主服务器6379

两个从服务器不断尝试连接主服务器6379

 

 

 

 

 

 

哨兵变化:一分钟后哨兵开始选举出新的master机器

 

 26380

 

 

26379

 

 

 

 

 

 

26381

 

 

 

选举完成推送6380位master

6380客户端变化

 

 

 

6381客户端变化

 

 

 

再次执行info命令:主master替换为6380

 

 

 

哨兵配置文件变化

 

 

 

6381配置文件变化

 

 

 

 

 

 

posted @ 2022-01-13 14:20  爵士灬  阅读(440)  评论(0编辑  收藏  举报