Redis主从复制与哨兵模式
文章目录
- 前言
-
- 一、先装好Redis
- 一、主从复制
-
-
- 1.先在主机192.168.2.237的/usr/local目录下新建redisfile文件,并将之前安装好的redis文件复制到该目录下。然后redis目录下创建redis的配置文件redis-6556.conf
- 2. 其次在从机192.168.2.37(我这里将redis的两台从机都部署在同一台虚拟机的不同端口)的/usr/local目录新建redisfile文件夹和redisfile2文件夹,并将之前安装好的redis文件分别复制到以上两个文件夹目录下。以下在各自的redis目录下创建创建redis的配置文件步骤一样,端口及地址做下改变即可。我在创建的两个redis配置文件分别命名为redis-6557.conf和redis-6558,其中redis-6557.conf配置如下:
-
- 二、哨兵模式
-
-
- 1. 先在主机192.168.2.237的redis目录下创建sentinel的配置文件sentinel-6776.conf
- 2.其次在从机192.168.2.37各自的redis目录下创建sentinel的配置文件,与上步骤一样,端口及地址做下改变即可。我在创建的两个sentinel配置文件分别命名为sentinel-6777.conf和sentinel-6778,其中sentinel-6777.conf配置如下:
-
- 三、运行
-
- 1.先运行redis
-
- 1.1 先运行192.168.2.237的主机
- 1.2 再运行192.168.2.37的两台从机
- 2.再运行sentinel
-
- 2.1 先运行192.168.2.237的主机
- 2.2 再运行192.168.2.37的两台从机
- 3.试试故障转移
- 四、其他
-
-
- 踩到的坑如下,待下回讲解主从复制及哨兵模式与java的spring项目整合一起分析解答。
-
- 总结
前言
一、先装好Redis
网上有很多教程,这里不做赘述,只提我之前踩过的坑:
caused by:io.lettuce.core.redisException:connot retrieve initial cluster partitions from initial URIs
原因:因为端口未开放
解决:以开放6556端口为例
#查看开放的端口$ firewall-cmd --list-ports#查询6556端口是否开放$ firewall-cmd --query-port=6556/tcpfirewall-cmd --add-port=6556/tcp --permanent#重新加载生效firewall-cmd --reload
一、主从复制
1.先在主机192.168.2.237的/usr/local目录下新建redisfile文件,并将之前安装好的redis文件复制到该目录下。然后redis目录下创建redis的配置文件redis-6556.conf
daemonize yes #守护线程port 6556#端口logfile "6556.log"#日志文件dir "/usr/local"#持久化文件目录路径requirepass "123456"#需要密码masterauth "123456"#主机密码#绑定虚拟机地址及本地映射地址bind 192.168.2.237 127.0.0.1 appendonly yes#开启持久化aof模式appendfilename "aof-6556.aof"#aof文件夹appendfsync everysec#每秒更新no-appendfsync-on-rewrite yes#同步数据时不重写auto-aof-rewrite-percentage 100 #增加倍数达到100%重写auto-aof-rewrite-min-size 64mb#重写最低文件大小为64mb
2. 其次在从机192.168.2.37(我这里将redis的两台从机都部署在同一台虚拟机的不同端口)的/usr/local目录新建redisfile文件夹和redisfile2文件夹,并将之前安装好的redis文件分别复制到以上两个文件夹目录下。以下在各自的redis目录下创建创建redis的配置文件步骤一样,端口及地址做下改变即可。我在创建的两个redis配置文件分别命名为redis-6557.conf和redis-6558,其中redis-6557.conf配置如下:
port 6557daemonize yeslogfile "6557.log"dir "/usr/local"requirepass "123456"masterauth "123456"bind 192.168.2.37 127.0.0.1appendonly yesappendfilename "aof-6557.aof"appendfsync everysecno-appendfsync-on-rewrite yesauto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mb
二、哨兵模式
1. 先在主机192.168.2.237的redis目录下创建sentinel的配置文件sentinel-6776.conf
port 6776#端口daemonize yes#守护线程logfile "6776.log"#日志dir "/usr/local"#持久化目录#sentinel监听主机为master及其地址和端口#且当有2个sentinel认为master失效后才算真正失效sentinel monitor master 192.168.2.237 6556 2#当达到15000毫秒(默认30s),master失效才被sentinel认为失效sentinel failover-timeout master 15000#连接master和slave的密码,且master和slave的密码须一致sentinel auth-pass master 123456#发生failover时主备切换有1个slave同时对新的master进行同步sentinel parallel-syncs master 1#该sentinel绑定的地址及端口bind 192.168.2.237 127.0.0.1
2.其次在从机192.168.2.37各自的redis目录下创建sentinel的配置文件,与上步骤一样,端口及地址做下改变即可。我在创建的两个sentinel配置文件分别命名为sentinel-6777.conf和sentinel-6778,其中sentinel-6777.conf配置如下:
port 6777#端口daemonize yes#守护线程logfile "6777.log"#日志dir "/usr/local"#持久化目录#sentinel监听主机为master及其地址和端口#且当有2个sentinel认为master失效后才算真正失效sentinel monitor master 192.168.2.237 6556 2#当达到15000毫秒(默认30s),master失效才被sentinel认为失效sentinel failover-timeout master 15000#连接master和slave的密码,且master和slave的密码须一致sentinel auth-pass master 123456#发生failover时主备切换有1个slave同时对新的master进行同步sentinel parallel-syncs master 1#该sentinel绑定的地址及端口bind 192.168.2.37 127.0.0.1
三、运行
1.先运行redis
1.1 先运行192.168.2.237的主机
我直接在usr/local目录下运行:
./redisfile/redis/src/redis-server ./redisfile/redis/redis-6556.conf
1.2 再运行192.168.2.37的两台从机
这里直接在/usr/local目录下编写一个.sh脚本同时启动两个redis服务,节省后面每次启动的时间,脚本start-all.sh
如下:
./redisfile/redis/src/redis-server ./redisfile/redis/redis-6557.conf./redisfile2/redis/src/redis-server ./redisfile2/redis/redis-6558.conf
编写好后要赋予其读写执行的权限,最后启动
chmod +x start-all.shsh start-all.sh
2.再运行sentinel
2.1 先运行192.168.2.237的主机
我直接在usr/local目录下运行:
./redisfile/redis/src/redis-sentinel ./redisfile/redis/sentinel -6776.conf
2.2 再运行192.168.2.37的两台从机
这里直接在/usr/local目录下编写一个.sh脚本同时启动两个sentinel 服务,节省后面每次启动的时间,脚本start-all-sentinel.sh
如下:
./redisfile/redis/src/redis-sentinel ./redisfile/redis/sentinel-6777.conf./redisfile2/redis/src/redis-sentinel ./redisfile2/redis/sentinel-6778.conf
编写好后要赋予其读写执行的权限,最后启动
chmod +x start-all-sentinel.shsh start-all-sentinel.sh
待redis和sentinel都运行后,sentinel的配置文件也发生了改变,其中sentinel-6556.conf如下:
sentinel known-slave master 192.168.2.37 6557sentinel known-slave master 192.168.2.37 6558sentinel known-sentinel master 192.168.2.37 6778 75f0266605bfa79a173076e6394d1c5f0032ebd6sentinel known-sentinel master 192.168.2.37 6777 03e6af6441ffac5c9602c693974e299136ac4632
3.试试故障转移
现在主机是6556,我们把6556的redis服务停止,同时停止6778的sentinel服务,看sentinel是否能够选举出新的主机并切换过去。
## 先在主机192.168.2.237找出6556redis的运行进程ps -ef|grep redis## 再杀死进程kill -9 76529
同上从机192.168.2.37找出6778sentinel的运行进程并杀死
过15秒后看sentinel-6776.conf的主机master已经由6556变成6558了
port 6776daemonize yeslogfile "6776.log"dir "/usr/local"sentinel myid 72ea9a3eb747027d16a2911abe372e7f5a24b620sentinel monitor master 192.168.2.37 6558 2
四、其他
踩到的坑如下,待下回讲解主从复制及哨兵模式与java的spring项目整合一起分析解答。
这里简单说下:master可读可写,另外一台salve(经三次测试每次都是下一次宕机后不会成为主机的从机)竟然也是可读可写,但是其中的数据并不会同步到master和另一台slave,包括主机切换后也不会。
总结
*随心所往,看见未来。Follow your heart,see night!*
欢迎点赞、关注、留言,一起学习、交流!