一、redis集群之主从复制
1.1、集群介绍
在主从模式的Redis系统中,从数据库在整个系统中起到了数据冗余备份和读写分离的作用。
节点类型 | IP地址 | 安装包 |
---|---|---|
master | 10.0.0.10 | redis-5.0.7.tar.gz |
slave1 | 10.0.0.20 | redis-5.0.7.tar.gz |
slave2 | 10.0.0.30 | redis-5.0.7.tar.gz |
1.2、搭建过程
1、先给每个节点安装redis
[root@localhost opt]# tar xzvf redis-5.0.7.tar.gz -C /opt/
[root@localhost opt]# cd /opt/redis-5.0.7/
[root@localhost redis-5.0.7]# make
[root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis install
####如果安装过程中,更改安装路径可以用make PRRFIX=安装路径 install
//设置Redis相关配置文件
[root@localhost redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/
[root@localhost redis-5.0.7]# cd utils/
[root@localhost utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] /usr/local/bin/redis-server #### 未做软连接,可手动输入
Selected config:
Port : 6379 ####端口号
Config file : /etc/redis/6379.conf ####默认配置文件
Log file : /var/log/redis_6379.log ####日志文件
Data dir : /var/lib/redis/6379 ####数据目录
Executable : /usr/local/bin/redis-server ####执行命令
Cli Executable : /usr/local/bin/redis-cli ####客户端命令
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
2、修改主、从节点的配置文件
主节点:
vi /etc//redis/6379.conf
70/bind 0.0.0.0
在研发、测试环境可以考虑bind 0.0.0.0,线上生产环境建议绑定从节点服务器的IP地址
sed -i '69s/127.0.0.1/0.0.0.0/' /etc/redis/6379.conf
137行 daemonize no #开启守护进程yes
sed -i '136s/no/yes/' /etc//redis/6379.conf
172行 logfile "" 改成 logfile /var/log/redis_6379.log
sed -i '171s/""/\/var\/log\/redis_6379.log/' /etc/redis/6379.conf
264行 dir ./ 改成 dir /var/lib/redis/6379 #本地数据库的存放目录
sed -i '263s/.\//\/var\/lib\/redis\/6379/' /etc/redis/6379.conf
700/appendonly no #启用AOF持久化
sed -i '699s/no/yes/' /etc/redis/6379.conf
从节点:
vi /etc//redis/6379.conf
67/bind 0.0.0.0
在研发、测试环境可以考虑bind 0.0.0.0,线上生产环境建议绑定从节点服务器的IP地址
sed -i '69s/127.0.0.1/0.0.0.0/' /etc/redis/6379.conf
137行 daemonize no #开启守护进程yes
sed -i '136s/no/yes/' /etc/redis/6379.conf
172行 logfile "" 改成 logfile /var/log/redis_6379.log
sed -i '171s/""/\/var\/log\/redis_6379.log/' /etc/redis/6379.conf
264行 dir ./ 改成 dir /var/lib/redis/6379
sed -i '263s/.\//\/var\/lib\/redis\/6379/' /etc/redis/6379.conf
700/appendonly no #启用AOF持久化
sed -i '699s/no/yes/' /etc//redis/6379.conf
287/# replicaof <masterip> <masterport> 改成 replicaof 10.0.0.10 6379
sed -i '286creplicaof 10.0.0.10 6379' /etc/redis/6379.conf
3、启动服务,先启动master redis,然后依次启动slave
#关闭防火墙
systemctl stop firewalld.service
setenforce 0
#重启服务
/etc/init.d/redis_6379 restart
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
#检查端口
netstat -anpt | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 9923/redis-server 0
4、 查看日志文件内容,可以看到两个slave节点连接成功的信息!
tail -f /var/log/redis_6379.log
[root@server1 ~]# tail -f /var/log/redis_6379.log
22035:M 12 Nov 2020 11:01:40.860 * Background saving terminated with success
22035:M 12 Nov 2020 11:01:40.860 * Synchronization with replica 10.0.0.30:6379 succeeded
22035:M 12 Nov 2020 11:01:41.112 * Replica 10.0.0.20:6379 asks for synchronization
22035:M 12 Nov 2020 11:01:41.113 * Full resync requested by replica 10.0.0.20:6379
22035:M 12 Nov 2020 11:01:41.113 * Starting BGSAVE for SYNC with target: disk
22035:M 12 Nov 2020 11:01:41.114 * Background saving started by pid 22148
22148:C 12 Nov 2020 11:01:41.119 * DB saved on disk
22148:C 12 Nov 2020 11:01:41.120 * RDB: 0 MB of memory used by copy-on-write
22035:M 12 Nov 2020 11:01:41.163 * Background saving terminated with success
22035:M 12 Nov 2020 11:01:41.163 * Synchronization with replica 10.0.0.20:6379 succeeded
也可以通过登录数据库,查看节点角色
[root@server1 ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.30,port=6379,state=online,offset=1498,lag=1
slave1:ip=10.0.0.20,port=6379,state=online,offset=1498,lag=0
master_replid:388136944386ad3b8b7a1b640fedb19cf9897fa8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1498
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1498
1.3、效果验证
进入主节点的数据库中,创建键值对,在从节点中可以进行查询,但是不能在从节点创建新的键值对。
主节点:
[root@localhost redis]# redis-cli
127.0.0.1:6379> set name lili #创建一个键值对
OK
127.0.0.1:6379> keys * #查看所有键
1) "name"
127.0.0.1:6379> get name #查看name键的值
"lili"
127.0.0.1:6379> exit
从节点服务器:
[root@localhost ~]# redis-cli
127.0.0.1:6379> get name #查看name键的值
"lili"
127.0.0.1:6379> exit
二、redis集群之哨兵模式
2.1、部署过程
redis2.8版本之前是主从复制模式,但是当数据库遇到异常中断服务后,我们只能通过手动的方式选择一个从数据库来升格为主数据库,显然这种方式很麻烦需要人工介入,这时通过哨兵模式可以实现自动化的系统监控和故障恢复。
哨兵模式是在主从复制的基础上加入监控机制,来实现自动化的系统监控和故障恢复功能。
具体功能如下:
序号 | 功能 |
---|---|
1 | 监控主数据库和从数据库是否正常运行 |
2 | 主数据库出现故障时自动将从数据库转换为主数据库 |
哨兵集群的结构图:
1、搭建主从复制集群
2、设置哨兵模式的配置文件
master节点:
vi /opt/redis-5.0.7/sentinel.conf
17/protected-mode no #关闭保护模式
sed -i '17s/^#//' /opt/redis-5.0.7/sentinel.conf
sed -i '17s/^ //' /opt/redis-5.0.7/sentinel.conf
21/port 26379 #无需修改,sentinel的端口号
26/daemonize yes 指定sentinel为后台启动
sed -i '26s/no/yes/' /opt/redis-5.0.7/sentinel.conf
36/logfile /var/log/sentinel.log #日志存放路径
sed -i '36s/""/\/var\/log\/sentinel.log/' /opt/redis-5.0.7/sentinel.conf
65/dir /tmp 改成 dir "/var/lib/redis/6379" #数据库存放路径
sed -i '65s/tmp/var\/lib\/redis\/6379/' /opt/redis-5.0.7/sentinel.conf
84/sentinel monitor mymaster 10.0.0.10 6379 2
sed -i '84s/127.0.0.1/192.168.100.128/' /opt/redis-5.0.7/sentinel.conf
//sentinel monitor mymaster [主服务器节点IP] [主服务器节点port] [几个哨兵检测到主节点失联数量]
-- 至少有几个哨兵检测到主服务器故障了,才进行故障迁移
113/sentinel down-after-milliseconds mymaster 3000 #原来是30000,为了尽快看到效果,将时间缩短
sed -i '113s/30000/3000/' /opt/redis-5.0.7/sentinel.conf
146/sentinel failover-timeout mymaster 120000 #原来是180000
sed -i '146s/180000/120000/' /opt/redis-5.0.7/sentinel.conf
slave节点:
vi /opt/redis-5.0.7/sentinel.conf
17/protected-mode no #关闭保护模式
sed -i '17s/^#//' /opt/redis-5.0.7/sentinel.conf
sed -i '17s/^ //' /opt/redis-5.0.7/sentinel.conf
26/daemonize yes 指定sentinel为后台启动
sed -i '26s/no/yes/' /opt/redis-5.0.7/sentinel.conf
36/logfile /var/log/sentinel.log #日志存放路径
65/dir /tmp 改成 dir "/var/lib/redis/6379" #数据库存放目录
84/sentinel monitor mymaster 10.0.0.10 6379 2 #填写主节点的地址
113/sentinel down-after-milliseconds mymaster 3000 #原来是30000,为了尽快看到效果,将时间缩短
sed -i '113s/30000/3000/' /opt/redis-5.0.7/sentinel.conf
146/sentinel failover-timeout mymaster 120000 #原来是180000
sed -i '146s/180000/120000/' /opt/redis-5.0.7/sentinel.conf
3、启动哨兵服务
依次启动哨兵sentinel,先启动master 然后slave
#放在后台运行
[root@server1 ~]# redis-sentinel /opt/redis-5.0.7/sentinel.conf &
[1] 22804
查看哨兵信息,里面有master节点的IP地址
[root@server1 ~]# redis-cli -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=10.0.0.10:6379,slaves=2,sentinels=3
[1]+ 完成 redis-sentinel /opt/redis-5.0.7/sentinel.conf
2.2、故障模拟
关闭主节点的redis-server服务
ps -ef | grep redis #查看redis-server进程数
kill -9 进程数 #关闭redis-server
#查看日志
tail -99f /var/log/sentinel.log
看到switch-master mymaster 10.0.0.10 6379 变为 10.0.0.30 6379,即主节点down掉,从节点顶替主节点。
#查看哨兵信息,可以看到master节点的IP地址改变
[root@server1 ~]# redis-cli -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=10.0.0.30:6379,slaves=2,sentinels=3
22804:X 12 Nov 2020 11:48:23.846 # Configuration loaded
22805:X 12 Nov 2020 11:48:23.847 * Increased maximum number of open files to 10032 (it was originally set to 1024).
22805:X 12 Nov 2020 11:48:23.848 * Running mode=sentinel, port=26379.
22805:X 12 Nov 2020 11:48:23.848 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
22805:X 12 Nov 2020 11:48:23.850 # Sentinel ID is 6adf7655f16d6cfb58b456be98c9a732509ceac3
22805:X 12 Nov 2020 11:48:23.850 # +monitor master mymaster 10.0.0.10 6379 quorum 2
22805:X 12 Nov 2020 11:48:23.851 * +slave slave 10.0.0.30:6379 10.0.0.30 6379 @ mymaster 10.0.0.10 6379
22805:X 12 Nov 2020 11:48:23.852 * +slave slave 10.0.0.20:6379 10.0.0.20 6379 @ mymaster 10.0.0.10 6379
22805:X 12 Nov 2020 11:48:37.908 * +sentinel sentinel a070582084975c174cfafbc69eb01435cca39415 10.0.0.20 26379 @ mymaster 10.0.0.10 6379
22805:X 12 Nov 2020 11:48:42.632 * +sentinel sentinel 60e5a16fdef37b9f8b204a29a11a2bce4d7dfdab 10.0.0.30 26379 @ mymaster 10.0.0.10 6379
22805:X 12 Nov 2020 11:56:28.410 # +sdown master mymaster 10.0.0.10 6379
22805:X 12 Nov 2020 11:56:28.449 # +new-epoch 1
22805:X 12 Nov 2020 11:56:28.451 # +vote-for-leader a070582084975c174cfafbc69eb01435cca39415 1
22805:X 12 Nov 2020 11:56:28.477 # +odown master mymaster 10.0.0.10 6379 #quorum 3/2
22805:X 12 Nov 2020 11:56:28.477 # Next failover delay: I will not start a failover before Thu Nov 12 12:02:28 2020
22805:X 12 Nov 2020 11:56:28.834 # +config-update-from sentinel a070582084975c174cfafbc69eb01435cca39415 10.0.0.20 26379 @ mymaster 10.0.0.10 6379
22805:X 12 Nov 2020 11:56:28.834 # +switch-master mymaster 10.0.0.10 6379 10.0.0.30 6379
22805:X 12 Nov 2020 11:56:28.835 * +slave slave 10.0.0.20:6379 10.0.0.20 6379 @ mymaster 10.0.0.30 6379
22805:X 12 Nov 2020 11:56:28.835 * +slave slave 10.0.0.10:6379 10.0.0.10 6379 @ mymaster 10.0.0.30 6379
22805:X 12 Nov 2020 11:56:58.844 # +sdown slave 10.0.0.10:6379 10.0.0.10 6379 @ mymaster 10.0.0.30 6379