@
目录
一、先装好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/tcp
- firewall-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 6557
- daemonize yes
- logfile "6557.log"
- dir "/usr/local"
- requirepass "123456"
- masterauth "123456"
- bind 192.168.2.37 127.0.0.1
- appendonly yes
- appendfilename "aof-6557.aof"
- appendfsync everysec
- no-appendfsync-on-rewrite yes
- auto-aof-rewrite-percentage 100
- auto-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.sh
- sh 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.sh
- sh start-all-sentinel.sh
复制代码 待redis和sentinel都运行后,sentinel的配置文件也发生了改变,其中sentinel-6556.conf如下:- sentinel known-slave master 192.168.2.37 6557
- sentinel known-slave master 192.168.2.37 6558
- sentinel known-sentinel master 192.168.2.37 6778 75f0266605bfa79a173076e6394d1c5f0032ebd6
- sentinel 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 6776
- daemonize yes
- logfile "6776.log"
- dir "/usr/local"
- sentinel myid 72ea9a3eb747027d16a2911abe372e7f5a24b620
- sentinel monitor master 192.168.2.37 6558 2
复制代码 四、其他
踩到的坑如下,待下回讲解主从复制及哨兵模式与java的spring项目整合一起分析解答。
这里简单说下:master可读可写,另外一台salve(经三次测试每次都是下一次宕机后不会成为主机的从机)竟然也是可读可写,但是其中的数据并不会同步到master和另一台slave,包括主机切换后也不会。 |