操作系统 | 主机IP | 功能 |
---|---|---|
centos 7 | 10.211.55.8 | Master:6379 / Sentinel : 26379 |
centos 7 | 10.211.55.9 | Slave :6379 / Sentinel : 26379 |
centos 7 | 10.211.55.10 | Slave :6379 / Sentinel : 26379 |
1. Linux 下 redis 安装
1.1 安装GCC编译器
// 查看是否安装gcc编译器
rpm -q gcc
// 安装gcc编译器
yum -y install gcc
1.2 安装Redis
cd /xiyou/software/
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar zxvf redis-5.0.5.tar.gz //将下载的源码包进行解压缩
cd redis-5.0.5 //进入解压缩目录
make //编译源程序
cd src && make install //切换到redis的src目录并进行编译安装
1.3 测试下是否安装成功
## 启动redis服务
// The binaries that are now compiled are available in the src directory. Run Redis with:
$ src/redis-server
ps:如果需要将redis服务作为守护进程(daemon)来运行,须将redis.conf启动配置中的daemonize no修改daemonize yes 。
--------------------------------------------------------------------------
## 启动redis客户端
// You can interact with Redis using the built-in client:
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
--------------------------------------------------------------------------
## 停止Redis服务
./redis-cli shutdown
pkill redis-server
2.redis配置
2.1 redis.cnf配置
## redis.cnf文件目录:/xiyou/software/redis-5.0.5/
## grep -Ev "^#|^$" redis.conf
## master:
bind 10.211.55.8
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/var/log/redis/redis-server.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir "/xiyou/software/redis-5.0.5/rdbs"
masterauth "123456"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
requirepass "123456"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
------------------------------------------------------------------------
slave:
## 与master结点的配置相同,最后添加一行
slaveof 10.211.55.8 6379
## bind 根据IP变换一下
2.2 sentinel.cnf 配置
## 3个结点的sentinel.cnf配置相同
protected-mode no
port 26379
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile "/var/log/redis/redis-sentinel.log"
dir "/xiyou/software/redis-5.0.5/sentinel-tmp"
sentinel monitor mymaster 10.211.55.8 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
2.3 创建对应目录
[root@centos-7-node2 log]# mkdir -p /var/log/redis/
[root@centos-7-node2 log]# mkdir -p /xiyou/software/redis-5.0.5/sentinel-tmp
[root@centos-7-node2 log]# mkdir -p /xiyou/software/redis-5.0.5/rdbs
2.4 启动服务
[root@centos-7-node4 redis-5.0.5]# cd /xiyou/software/redis-5.0.5/src/
[root@centos-7-node4 src]# ./redis-server ../redis.conf
[root@centos-7-node4 src]# ./redis-sentinel ../sentinel.conf
2.5 查看集群
## master:
[root@centos-7-node2 src]# redis-cli -h 10.211.55.8 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.211.55.8:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.211.55.9,port=6379,state=online,offset=16850,lag=1
slave1:ip=10.211.55.10,port=6379,state=online,offset=16850,lag=1
master_replid:b47bd7b2524ce8ca3264503eb22a1c06f45016e9
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:16850
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:16850
-----------------------------------------------------------------------------------
## slave
root@centos-7-node3 src]# redis-cli -h 10.211.55.9 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.211.55.9:6379> info replication
# Replication
role:slave
master_host:10.211.55.8
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:26794
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b47bd7b2524ce8ca3264503eb22a1c06f45016e9
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:26794
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:26794
-----------------------------------------------------------------------------------------
## slave
root@centos-7-node4 src]# redis-cli -h 10.211.55.10 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.211.55.10:6379> info replication
# Replication
role:slave
master_host:10.211.55.8
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:34540
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b47bd7b2524ce8ca3264503eb22a1c06f45016e9
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:34540
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:8658
repl_backlog_histlen:25883
3. 测试
3.1 master宕机
在Master 主节点(10.211.55.8)上执行 pkill redis-server 命令来模拟Master宕机的情况:
==> 单个哨兵主观认为 Master 宕机
==> 当主观认为 Master 宕机的哨兵个数达到 quorum 设定值时,哨兵集群客观认为 Master 宕机
==> 开始 failover
==> 收集候选节点列表
==> 根据候选节点的优先级和运行状态综合判断,选择一个 Slave 并将其提拔为新的 Master
==> 将新的 Master 主节点信息通知到其他节点,并更新各节点的配置文件
## 查看10.211.55.8的redis-sentinel.log
10725:X 03 Jul 2019 06:51:32.386 # +odown master mymaster 10.211.55.8 6379 #quorum 3/2
10725:X 03 Jul 2019 06:51:32.386 # Next failover delay: I will not start a failover before Wed Jul 3 06:57:33 2019
10725:X 03 Jul 2019 06:51:33.114 # +config-update-from sentinel f2c13ca3312dd0bc7e796119cbe61d3d2c1ed4b8 10.211.55.9 26379 @ mymaster 10.211.55.8 6379
10725:X 03 Jul 2019 06:51:33.114 # +switch-master mymaster 10.211.55.8 6379 10.211.55.9 6379
10725:X 03 Jul 2019 06:51:33.114 * +slave slave 10.211.55.10:6379 10.211.55.10 6379 @ mymaster 10.211.55.9 6379
10725:X 03 Jul 2019 06:51:33.114 * +slave slave 10.211.55.8:6379 10.211.55.8 6379 @ mymaster 10.211.55.9 6379
//可以看到在选举新的10.211.55.9作为新的主结点
// 验证10.211.55.9及10.211.55.10状态
---------------------------------------------------------------------------
10.211.55.9:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.211.55.10,port=6379,state=online,offset=66360,lag=1
master_replid:639bfe3151ab71dcb95e68e3b76d543415d21ff4
master_replid2:b47bd7b2524ce8ca3264503eb22a1c06f45016e9
master_repl_offset:66360
second_repl_offset:40489
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:66360
-----------------------------------------------------------------------------
10.211.55.10:6379> info replication
# Replication
role:slave
master_host:10.211.55.9
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:69258
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:639bfe3151ab71dcb95e68e3b76d543415d21ff4
master_replid2:b47bd7b2524ce8ca3264503eb22a1c06f45016e9
master_repl_offset:69258
second_repl_offset:40489
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:8658
repl_backlog_histlen:60601
3.2 master 恢复
//10.211.55.8成为了slave结点
## 重启10.211.55.8的redis-server,10.211.55.8成为了集群的slave结点
[root@centos-7-node2 src]# redis-cli -h 10.211.55.8 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.211.55.8:6379> info replication
# Replication
role:slave
master_host:10.211.55.9
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:267575
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:639bfe3151ab71dcb95e68e3b76d543415d21ff4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:267575
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:263268
repl_backlog_histlen:4308
---------------------------------------redis-sentinel.log-------------------
10093:X 03 Jul 2019 07:09:13.821 # -sdown slave 10.211.55.8:6379 10.211.55.8 6379 @ mymaster 10.211.55.9 6379
10093:X 03 Jul 2019 07:09:23.753 * +convert-to-slave slave 10.211.55.8:6379 10.211.55.8 6379 @ mymaster 10.211.55.9 6379
3.3 slave宕机
//此时的集群为
//master: 10.211.55.9
//slave: 10.211.55.8
//slave: 10.211.55.10
--------------------------------------------------------------------------------
## 在slave从节点(10.211.55.10)上执行 pkill redis-server 命令来模拟slave宕机的情况
## 主结点的slave变成了一个结点10.211.55.8
10.211.55.9:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.211.55.8,port=6379,state=online,offset=420227,lag=1
master_replid:639bfe3151ab71dcb95e68e3b76d543415d21ff4
master_replid2:b47bd7b2524ce8ca3264503eb22a1c06f45016e9
master_repl_offset:420241
second_repl_offset:40489
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:420241
3.4 slave恢复
## [root@centos-7-node4 src]# ./redis-server ../redis.conf
## 主结点上又恢复了两个从结点
10.211.55.9:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.211.55.8,port=6379,state=online,offset=449011,lag=0
slave1:ip=10.211.55.10,port=6379,state=online,offset=449300,lag=0
master_replid:639bfe3151ab71dcb95e68e3b76d543415d21ff4
master_replid2:b47bd7b2524ce8ca3264503eb22a1c06f45016e9
master_repl_offset:449300
second_repl_offset:40489
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:449300
4. 异结点备份及恢复
4.1 备份恢复测试
Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理。生产中我们主要用来存储用户的登录信息,设备的详情数据,以及会员签到点赞的信息等等。
提到redis备份,就不得不提及redis的持久化的两种方式:RDB和AOF。
- RDB持久化方式能够在指定的时间间隔能对你的数据进行快照存储.
- AOF持久化方式记录每次对服务器写的操作,当服务器重启的时候会重新执行这些命令来恢复原始的数据,AOF命令以redis协议追加保存每次写的操作到文件末尾.Redis还能对AOF文件进行后台重写,使得AOF文件的体积不至于过大.
## 将10.211.55.5 作为备份恢复结点
## redis配置文件中关于dump.rdb的配置
vim redis.cnf
-------------------------------------------------------------------
save 900 1
save 300 10
save 60 10000
# 将DB同步到磁盘,使用SAVE命令,自动同步的触发条件: save <秒> <更新数>
# save 900 1 代表900秒内有1个key发生改变就触发save
# save 300 10 代表300秒内有10个key发生改变就触发save
# save 60 10000 代表300秒内有10个key发生改变就触发save
# 各条件之间是‘或’的关系,也就是说有一个条件满足就会触发save的操作
stop-writes-on-bgsave-error yes
# 当后台进程执行save出错时,停止redis的写入操作。
rdbcompression yes
# 将rdb文件进行压缩
rdbchecksum yes
# 对rdb文件进行校验
dbfilename dump.rdb
# rdb文件命名
dir "/xiyou/software/redis-5.0.5/rdbs"
# rdb文件存储目录
## 其他配置可暂时不改,只是为了测试下备份恢复
SAVE VS BGSAVE
SAVE ----- 备份redis可以手动使用SAVE命令,执行SAVE命令会使用主进程执行快照操作,这意味着在SAVE的过程中,会阻塞主进程。
BGSAVE ---- 另一种操作是使用BGSAVE,使用BGSAVE的话redis会fork出一个子进程来执行快照操作,而不影响主进程。
1. 往redis的sentinel集群结点中写一堆数据并进行保存,便于后续测试
10.211.55.10:6379> set name18 oo
OK
10.211.55.10:6379> set name19 pp
OK
10.211.55.10:6379> set name20 qq
OK
10.211.55.10:6379> bgsave
Background saving started
2. 关闭10.211.55.5的redis-server
pkill redis-server
3. 将源集群中的dump.rdb复制到目标服务器的对应路径,查看下文件大小是否一致
[root@centos-7-node2 rdbs]# scp dump.rdb root@10.211.55.5:/xiyou/software/redis-5.0.5/rdbs/
4. 启动10.211.55.5的redis-server并用redis-cli登陆测试下
[root@CentOS_node1 src]# ./redis-server ../redis.conf
[root@CentOS_node1 rdbs]# ll
总用量 4
-rw-r--r-- 1 root root 399 7月 3 09:32 dump.rdb
[root@CentOS_node1 rdbs]# redis-cli -h 10.211.55.5 -p 6379
10.211.55.5:6379> get name20
"qq"
## 说明能成功恢复没有问题!!!
4.2 备份脚本
/xiyou/software/redis_bak/bak.sh
vim bak.sh
---------------------------------------------------------------------
#!/bin/bash
source_host="10.211.55.8"
dest_host="10.211.55.5"
port="6379"
pass="123456"
scp_pass="123456"
REDIS_DIR=/xiyou/software/redis-5.0.5/rdbs
REDIS_CMD=/xiyou/software/redis-5.0.5/src/"redis-cli -h $source_host -p $port -a $pass"
now="$(date -d'+0 day' +'%Y%m%d%H%M%S')"
$REDIS_CMD save
[ $? -eq 0 ] && {
cp $REDIS_DIR/dump.rdb $REDIS_DIR/dump_${now}.rdb
sshpass -p $scp_pass scp -r $REDIS_DIR/dump_${now}.rdb root@$dest_host:$REDIS_DIR
}
4.3 定时任务
5 0 * * * /bin/sh /xiyou/software/redis_bak/bak.sh &>/dev/null
那些年我们一起踩过的坑:
1. sshpass: command not found error
apt-get install sshpass ##ubuntu
-------------------------------
wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
yum --enablerepo=epel -y install sshpass ##centos/redhat
##参考:https://stackoverflow.com/questions/38129835/sshpass-command-not-found-error
2. bash命名规范
中间不能有空格,可以使用下划线(_)
##参考:http://www.runoob.com/linux/linux-shell-variable.html
待补充:redis集群连接
参考:
https://redis.io/topics/persistence
https://cloud.tencent.com/info/0420774e51445c7dbc75dcb1ff6fe49c.html
https://blog.csdn.net/pengjunlee/article/details/81429119
网友评论