redis单机-哨兵-集群模式
单机安装
安装包下载
下载地址:https://redis.io/download
新建用户
# 添加用户组
groupadd redis
# 添加用户
useradd -d /home/redis -g redis -G redis -m redis
# 修改用户密码
passwd redis
上传解压安装包
# redis用户新建软件存放目录
mkdir /home/redis/software
# 使用文件传输工具上传文件
# 解压redis安装包
tar -zxvf redis-5.0.8.tar.gz
# 重命名
mv redis-5.0.8 redis
# 赋权
chmod -R 755 /home/redis/software/
编译安装
# 进入redis安装目录
cd /home/redis/software/redis
# 编译
make MALLOC=libc
# 安装
cd src && make PREFIX=/home/redis/software/redis-install install
启动服务
-
复制配置文件到安装目录
cp /home/redis/software/redis/redis.conf /home/redis/software/redis-install/
-
修改配置文件
修改/home/redis/software/redis-install/redis.conf ,将daemonize no修改为daemonize yes
################################# GENERAL ##################################### # By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. daemonize yes # If you run Redis from upstart or systemd, Redis can interact with your
-
启动命令
# 启动命令 /home/redis/software/redis-install/bin/redis-server /home/redis/software/redis-install/redis.conf
[redis@iZuf62iexj3ztw81eg1cnoZ redis-install]$ /home/redis/software/redis-install/bin/redis-server /home/redis/software/redis-install/redis.conf 8802:C 06 Apr 2020 22:08:38.131 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 8802:C 06 Apr 2020 22:08:38.131 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=8802, just started 8802:C 06 Apr 2020 22:08:38.131 # Configuration loaded [redis@iZuf62iexj3ztw81eg1cnoZ redis-install]$
-
客户端连接
/home/redis/software/redis-install/bin/redis-cli -p 6379
[redis@iZuf62iexj3ztw81eg1cnoZ bin]$ /home/redis/software/redis-install/bin/redis-cli -p 6379 127.0.0.1:6379> set "aa" "bb" OK 127.0.0.1:6379> get "aa" "bb" 127.0.0.1:6379>
-
设置密码
# 查看 127.0.0.1:6379> config get requirepass 1) "requirepass" 2) "" # 设置密码 redis123 127.0.0.1:6379> config set requirepass redis123 OK # 再次查看 127.0.0.1:6379> config get requirepass (error) NOAUTH Authentication required. 127.0.0.1:6379>
-
再次修改配置文件添加密码
# requirepass redis123
注册系统服务
使用root用户操作
-
复制redis配置文件
# 新建/etc/redis mkdir /etc/redis # 复制文件 cp /home/redis/software/redis-install/redis.conf /etc/redis/6379.conf
-
复制redis启动脚本
cp /home/redis/software/redis/utils/redis_init_script /etc/init.d/redisd
-
修改启动脚本参数
# 编辑/etc/init.d/redisd文件,在#!/bin/sh添加如下两行内容 # chkconfig: 2345 10 90 # description: Start and Stop redis # 修改参数,指定redis的安装路径 REDISPORT=6379 EXEC=/home/redis/software/redis-install/bin/redis-server CLIEXEC=/home/redis/software/redis-install/bin/redis-cli # 加入密码 将$CLIEXEC -p $REDISPORT shutdown修改如下 $CLIEXEC -a redis123 -p $REDISPORT shutdown
#!/bin/sh # # chkconfig: 2345 10 90 # description: Start and Stop redis # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. ### BEGIN INIT INFO # Provides: redis_6379 # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Redis data structure server # Description: Redis data structure server. See https://redis.io ### END INIT INFO REDISPORT=6379 EXEC=/home/redis/software/redis-install/bin/redis-server CLIEXEC=/home/redis/software/redis-install/bin/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="/etc/redis/${REDISPORT}.conf" case "$1" in start) if [ -f $PIDFILE ] then echo "$PIDFILE exists, process is already running or crashed" else echo "Starting Redis server..." $EXEC $CONF fi ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -a redis123 -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; *) echo "Please use start or stop as first argument" ;; esac
服务管理
# 设为开机启动
chkconfig redisd on
# 设为开机关闭
chkconfig redisd off
# 打开redis命令
service redisd start
# 关闭redis命令
service redisd stop
redis配置文件说明
# Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程
daemonize no
#当Redis以守护进程方式运行时,Redis默认会把pid写入/var/run/redis.pid文件,可以通过pidfile指定
pidfile /var/run/redis.pid
# 指定Redis监听端口,默认端口为6379
port 6379
# 绑定的主机地址
bind 127.0.0.1
# 客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
timeout 300
# 指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning,默认为verbose
loglevel verbose
# 日志记录方式,默认为标准输出,如果配置Redis为守护进程方式运行,而这里又配置为日志记录方式为标准输出,则日志将会发送给/dev/null
logfile stdout
# 设置数据库的数量,默认数据库为0,可以使用SELECT 命令在连接上指定数据库id
databases 16
# 指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合
save
# Redis默认配置文件中提供了三个条件
# 900秒(15分钟)内有1个更改
save 900 1
# 300秒(5分钟)内有10个更改
save 300 10
# 60秒内有10000个更改
save 60 10000
# 指定存储至本地数据库时是否压缩数据,默认为yes,Redis采用LZF压缩,如果为了节省CPU时间,可以关闭该选项,但会导致数据库文件变的巨大
rdbcompression yes
# 指定本地数据库文件名,默认值为dump.rdb
dbfilename dump.rdb
# 指定本地数据库存放目录
dir ./
# 设置当本机为slave服务时,设置master服务的IP地址及端口,在Redis启动时,它会自动从master进行数据同步
slaveof
# 当master服务设置了密码保护时,slave服务连接master的密码
masterauth
# 设置Redis连接密码,如果配置了连接密码,客户端在连接Redis时需要通过AUTH 命令提供密码,默认关闭
requirepass foobared
# 设置同一时间最大客户端连接数,默认无限制,Redis可以同时打开的客户端连接数为Redis进程可以打开的最大文件描述符数,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis会关闭新的连接并向客户端返回max number of clients reached错误信息
maxclients 128
# 指定Redis最大内存限制,Redis在启动时会把数据加载到内存中,达到最大内存后,Redis会先尝试清除已到期或即将到期的Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。Redis新的vm机制,会把Key存放内存,Value会存放在swap区
maxmemory
# 指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。因为 redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认为no
appendonly no
# 指定更新日志文件名,默认为appendonly.aof
appendfilename appendonly.aof
# 指定更新日志条件,共有3个可选值:
# no:表示等操作系统进行数据缓存同步到磁盘(快)
# always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全)
# everysec:表示每秒同步一次(折衷,默认值)
appendfsync everysec
# 指定是否启用虚拟内存机制,默认值为no,简单的介绍一下,VM机制将数据分页存放,由Redis将访问量较少的页即冷数据swap到磁盘上,访问多的页面由磁盘自动换出到内存中
vm-enabled no
# 虚拟内存文件路径,默认值为/tmp/redis.swap,不可多个Redis实例共享
vm-swap-file /tmp/redis.swap
# 将所有大于vm-max-memory的数据存入虚拟内存,无论vm-max-memory设置多小,所有索引数据都是内存存储的(Redis的索引数据 就是keys),也就是说,当vm-max-memory设置为0的时候,其实是所有value都存在于磁盘。默认值为0
vm-max-memory 0
# Redis swap文件分成了很多的page,一个对象可以保存在多个page上面,但一个page上不能被多个对象共享,vm-page-size是要根据存储的 数据大小来设定的,作者建议如果存储很多小对象,page大小最好设置为32或者64bytes;如果存储很大大对象,则可以使用更大的page,如果不 确定,就使用默认值
vm-page-size 32
# 设置swap文件中的page数量,由于页表(一种表示页面空闲或使用的bitmap)是在放在内存中的,,在磁盘上每8个pages将消耗1byte的内存。
vm-pages 134217728
# 设置访问swap文件的线程数,最好不要超过机器的核数,如果设置为0,那么所有对swap文件的操作都是串行的,可能会造成比较长时间的延迟。默认值为4
vm-max-threads 4
# 设置在向客户端应答时,是否把较小的包合并为一个包发送,默认为开启
glueoutputbuf yes
# 指定在超过一定的数量或者最大的元素超过某一临界值时,采用一种特殊的哈希算法
hash-max-zipmap-entries 64
hash-max-zipmap-value 512
# 指定是否激活重置哈希,默认为开启
activerehashing yes
# 指定包含其它的配置文件,可以在同一主机上多个Redis实例之间使用同一份配置文件,而同时各个实例又拥有自己的特定配置文件
include /path/to/local.conf
#禁止外网访问redis
protected-mode yes
主从模式
编译安装同上,读写分离,一主两从,主负责写,从负责读
项目架构
角色 | ip | port |
---|---|---|
master | 127.0.0.1 | 6379 |
slave | 127.0.0.1 | 6380 |
slave | 127.0.0.1 | 6381 |
前期准备
# 创建6379、6380目录
mkdir -p /home/redis/software/rwseparation/6379
mkdir -p /home/redis/software/rwseparation/6380
mkdir -p /home/redis/software/rwseparation/6381
# 创建日志目录
mkdir /home/redis/software/rwseparation/logs
# 复制redis启动文件
cp /home/redis/software/redis-install/bin/redis-server /home/redis/software/rwseparation/
# 复制配置文件
cp /home/redis/software/redis/redis.conf /home/redis/software/rwseparation/6379
cp /home/redis/software/redis/redis.conf /home/redis/software/rwseparation/6380
cp /home/redis/software/redis/redis.conf /home/redis/software/rwseparation/6381
# 查看
[redis@iZuf62iexj3ztw81eg1cnoZ rwseparation]$ ll /home/redis/software/rwseparation/
total 3972
drwxrwxr-x 2 redis redis 4096 Apr 14 17:40 6379
drwxrwxr-x 2 redis redis 4096 Apr 14 17:40 6380
drwxrwxr-x 2 redis redis 4096 Apr 14 17:40 6381
-rwxr-xr-x 1 redis redis 4058096 Apr 14 17:37 redis-server
[redis@iZuf62iexj3ztw81eg1cnoZ rwseparation]$
master
# 进入目录
cd /home/redis/software/rwseparation/
# 修改6379下的redis.conf文件
daemonize yes
logfile "/home/redis/software/rwseparation/logs/6379.log"
requirepass redis123
masterauth redis123
# bind 127.0.0.1
protected-mode no
# 启动6379
./redis-server ./6379/redis.conf
slave
# 进入目录
cd /home/redis/software/rwseparation/
# 修改6380下的redis.conf文件
daemonize yes
logfile "/home/redis/software/rwseparation/logs/6380.log"
replicaof 127.0.0.1 6379
port 6380
requirepass redis123
masterauth redis123
# bind 127.0.0.1
protected-mode no
# 修改6381下的redis.conf文件
daemonize yes
logfile "/home/redis/software/rwseparation/logs/6381.log"
replicaof 127.0.0.1 6379
port 6381
requirepass redis123
masterauth redis123
# bind 127.0.0.1
protected-mode no
# 启动6380、6381
./redis-server ./6380/redis.conf
./redis-server ./6381/redis.conf
查看
# 查看redis服务
[redis@iZuf62iexj3ztw81eg1cnoZ rwseparation]$ ps -ef|grep redis
redis 9845 1 0 Apr14 ? 00:00:33 ./redis-server 127.0.0.1:6379
redis 9861 1 0 Apr14 ? 00:00:56 ./redis-server 127.0.0.1:6380
root 10758 1032 0 09:36 ? 00:00:00 sshd: redis [priv]
redis 10760 10758 0 09:36 ? 00:00:00 sshd: redis@pts/1
redis 10761 10760 0 09:36 pts/1 00:00:00 -bash
redis 10801 1 0 09:40 ? 00:00:00 ./redis-server 127.0.0.1:6381
redis 10806 10761 0 09:44 pts/1 00:00:00 ps -ef
redis 10807 10761 0 09:44 pts/1 00:00:00 grep --color=auto redis
[redis@iZuf62iexj3ztw81eg1cnoZ rwseparation]$
# 使用redis-cli查看项目主从节点
/home/redis/software/redis-install/bin/redis-cli -a redis123 -p 6379
[redis@iZuf62iexj3ztw81eg1cnoZ rwseparation]$ /home/redis/software/redis-install/bin/redis-cli -p 6379
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=79674,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=79674,lag=1
master_replid:bd94d29dd8f86ea8cd8105643a4ed51c3813d707
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:79674
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:79674
127.0.0.1:6379>
手动模拟主节点宕机,从节点切换为主节点
-
停止主节点服务
/home/redis/software/redis-install/bin/redis-cli -a redis123 -p 6379 shutdown
-
将6380切换为主节点
登录6380客户端命令
/home/redis/software/redis-install/bin/redis-cli -a redis123 -p 6380
-
切换为主节点
replicaof no one
[redis@iZuf62iexj3ztw81eg1cnoZ rwseparation]$ /home/redis/software/redis-install/bin/redis-cli -a redis123 -p 6380 127.0.0.1:6380> replicaof no one OK 127.0.0.1:6380> info replication # Replication role:master connected_slaves:0 master_replid:a230e183f92e65a1916bc428403d3a06a197bb4c master_replid2:bd94d29dd8f86ea8cd8105643a4ed51c3813d707 master_repl_offset:81536 second_repl_offset:81537 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:81536 127.0.0.1:6380>
-
将6381的主节点切换为6380
登录6381客户端命令
/home/redis/software/redis-install/bin/redis-cli -p 6381
-
切换主节点命令
replicaof 127.0.0.1 6380
[redis@iZuf62iexj3ztw81eg1cnoZ rwseparation]$ /home/redis/software/redis-install/bin/redis-cli -p 6381 127.0.0.1:6381> replicaof 127.0.0.1 6380 OK 127.0.0.1:6381> info replication # Replication role:slave master_host:127.0.0.1 master_port:6380 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_repl_offset:81620 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:a230e183f92e65a1916bc428403d3a06a197bb4c master_replid2:bd94d29dd8f86ea8cd8105643a4ed51c3813d707 master_repl_offset:81620 second_repl_offset:81537 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:81620 127.0.0.1:6381>
主从模式数据同步
SYNC
redis2.8版本之前数据同步的方法,此方法是一个非常耗费资源的操作
- 主服务器需要执行BGSAVE命令来生成RDB文件,这个生成操作会耗费主服务器大量的的CPU、内存和磁盘读写资源
- 主服务器将RDB文件发送给从服务器,这个发送操作会耗费主从服务器大量的网络带宽和流量
- 请求的时间产生影响:接收到RDB文件的从服务器在载入文件的过程是阻塞的,无法处理命令请求
PSYNC
redis2.8版本之后,PSYNC命令具有完整重同步(full resynchronization)和部分重同步(partial resynchronization1)两种模式
- 完整重同步
- SYNC
- 部分重同步
- 记录主服务的复制偏移量(replication offset)和从服务器的复制偏移量
- redis的操作命令放在复制积压缓冲区,主服务器的复制积压缓冲区(replication backlog),默认大小为1M
- 服务器的运行ID(run ID),用于存储服务器标识,如从服务器断线重新连接,取到主服务器的运行ID与重接后的主服务器运行ID进行对比,从而判断是执行部分重同步还是执行完整重同步
停止服务
/home/redis/software/redis-install/bin/redis-cli -a redis123 -p 6379 shutdown
/home/redis/software/redis-install/bin/redis-cli -a redis123 -p 6380 shutdown
/home/redis/software/redis-install/bin/redis-cli -a redis123 -p 6381 shutdown
Sentinel模式
Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Redis本身(包括它的很多客户端)都没有实现自动进行主备切换,而Redis-sentinel本身也是一个独立运行的进程,它能监控多个master-slave集群,发现master宕机后能进行自动切换
功能
- 监控(Monitoring): Sentinel 会不断地检查主服务器和从服务器是否运作正常
- 提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知
- 自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器
项目架构
角色 | ip | port |
---|---|---|
master | 139.224.101.91 | 6379 |
slave | 139.224.101.91 | 6380 |
slave | 139.224.101.91 | 6381 |
sentinel | 139.224.101.91 | 26379 |
sentinel | 139.224.101.91 | 26380 |
sentinel | 139.224.101.91 | 26381 |
前期准备
服务器搭建基于上述主从模式,将服务器恢复到6378为主,6380,6381为从
# 创建6379、6380目录
mkdir -p /home/redis/software/rwseparation/26379
mkdir -p /home/redis/software/rwseparation/26380
mkdir -p /home/redis/software/rwseparation/26381
# 复制sentinel启动脚本
cp /home/redis/software/redis-install/bin/redis-sentinel /home/redis/software/rwseparation/
# sentinel配置文件
cp /home/redis/software/redis/sentinel.conf /home/redis/software/rwseparation/26379
cp /home/redis/software/redis/sentinel.conf /home/redis/software/rwseparation/26380
cp /home/redis/software/redis/sentinel.conf /home/redis/software/rwseparation/26381
# 查看
[redis@iZuf62iexj3ztw81eg1cnoZ rwseparation]$ ll /home/redis/software/rwseparation/
total 7960
drwxrwxr-x 2 redis redis 4096 Apr 15 15:11 26379
drwxrwxr-x 2 redis redis 4096 Apr 15 15:11 26380
drwxrwxr-x 2 redis redis 4096 Apr 15 15:11 26381
drwxrwxr-x 2 redis redis 4096 Apr 15 11:39 6379
drwxrwxr-x 2 redis redis 4096 Apr 15 11:39 6380
drwxrwxr-x 2 redis redis 4096 Apr 15 11:40 6381
-rwxr-xr-x 1 redis redis 4058096 Apr 15 10:27 redis-sentinel
-rwxr-xr-x 1 redis redis 4058096 Apr 14 17:37 redis-server
[redis@iZuf62iexj3ztw81eg1cnoZ rwseparation]$
配置文件
-
相同部分
# daemonize # 后台启动 daemonize yes # sentinel monitor
# 告诉sentinel去监听地址为ip:port的一个master,这里的master-name可以自定义,quorum是一个数字,指明当有多少个sentinel认为一个master失效时,master才算真正失效 # 若项目与redis不在同一台机器,此处配置外网Ip sentinel monitor mymaster 139.224.101.91 6379 2 # 设置master和slaves的密码 # 放在sentinel monitor下面 sentinel auth-pass mymaster redis123 # sentinel down-after-milliseconds # 这个配置项指定了需要多少失效时间,一个master才会被这个sentinel主观地认为是不可用的。 单位是毫秒,默认为30秒 sentinel down-after-milliseconds mymaster 30000 # sentinel failover-timeout # failover-timeout 可以用在以下这些方面: # 1. 同一个sentinel对同一个master两次failover之间的间隔时间。 # 2. 当一个slave从一个错误的master那里同步数据开始计算时间。直到slave被纠正为向正确的master那里同步数据时。 # 3. 当想要取消一个正在进行的failover所需要的时间。 # 4. 当进行failover时,配置所有slaves指向新的master所需的最大时间。不过,即使过了这个超时,slaves依然会被正确配置为指向master,但是就不按parallel-syncs所配置的规则来了。 sentinel failover-timeout mymaster 180000 # sentinel parallel-syncs # 这个配置项指定了在发生failover主备切换时最多可以有多少个slave同时对新的master进行 同步,这个数字越小,完成failover所需的时间就越长,但是如果这个数字越大,就意味着越 多的slave因为replication而不可用。可以通过将这个值设为 1 来保证每次只有一个slave 处于不能处理命令请求的状态。 sentinel parallel-syncs mymaster 1 -
不同部分
# 26379 端口 port 26379 logfile "/home/redis/software/rwseparation/logs/26379.log" # 26380 端口 port 26380 logfile "/home/redis/software/rwseparation/logs/26380.log" # 26381 端口 port 26381 logfile "/home/redis/software/rwseparation/logs/26381.log"
启动服务
redis 主从模式先启动
# 26379
/home/redis/software/rwseparation/redis-sentinel /home/redis/software/rwseparation/26379/sentinel.conf
# 26380
/home/redis/software/rwseparation/redis-sentinel /home/redis/software/rwseparation/26380/sentinel.conf
# 26381
/home/redis/software/rwseparation/redis-sentinel /home/redis/software/rwseparation/26381/sentinel.conf
查看
# 查看sentinel服务
[redis@iZuf62iexj3ztw81eg1cnoZ rwseparation]$ ps -ef|grep sentinel
redis 11386 1 0 15:28 ? 00:00:00 /home/redis/software/rwseparation/redis-sentinel *:26379 [sentinel]
redis 11391 1 0 15:28 ? 00:00:00 /home/redis/software/rwseparation/redis-sentinel *:26380 [sentinel]
redis 11396 1 0 15:29 ? 00:00:00 /home/redis/software/rwseparation/redis-sentinel *:26381 [sentinel]
redis 11401 10963 0 15:29 pts/2 00:00:00 grep --color=auto sentinel
[redis@iZuf62iexj3ztw81eg1cnoZ rwseparation]$
# 通过redis-cli命令查看
[redis@iZuf62iexj3ztw81eg1cnoZ rwseparation]$ /home/redis/software/redis-install/bin/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=127.0.0.1:6379,slaves=2,sentinels=3
[redis@iZuf62iexj3ztw81eg1cnoZ rwseparation]$
关闭服务
/home/redis/software/redis-install/bin/redis-cli -p 26379 shutdown
/home/redis/software/redis-install/bin/redis-cli -p 26380 shutdown
/home/redis/software/redis-install/bin/redis-cli -p 26380 shutdown
模拟主节点宕机
手动停止主节点服务
/home/redis/software/redis-install/bin/redis-cli -a redis123 -p 6379 shutdown
-
查看26379的sentinel日志
16328:X 16 Apr 2020 16:23:34.755 # +sdown master mymaster 139.224.101.91 6379 16328:X 16 Apr 2020 16:23:34.774 # +new-epoch 1 16328:X 16 Apr 2020 16:23:34.776 # +vote-for-leader d17803e53d5fca31d92b26118d640f96862460a7 1 16328:X 16 Apr 2020 16:23:34.810 # +odown master mymaster 139.224.101.91 6379 #quorum 2/2 16328:X 16 Apr 2020 16:23:34.810 # Next failover delay: I will not start a failover before Thu Apr 16 16:29:35 2020 16328:X 16 Apr 2020 16:23:34.874 * +sentinel-address-switch master mymaster 139.224.101.91 6379 ip 172.19.206.22 port 26380 for 68c5172ec311965273df471178ecbb4c6374ed9e 16328:X 16 Apr 2020 16:23:34.924 # -odown master mymaster 139.224.101.91 6379 16328:X 16 Apr 2020 16:23:35.073 * +sentinel-address-switch master mymaster 139.224.101.91 6379 ip 127.0.0.1 port 26381 for d17803e53d5fca31d92b26118d640f96862460a7 16328:X 16 Apr 2020 16:23:35.165 * +sentinel-address-switch master mymaster 139.224.101.91 6379 ip 172.19.206.22 port 26381 for d17803e53d5fca31d92b26118d640f96862460a7 16328:X 16 Apr 2020 16:23:35.168 # +config-update-from sentinel d17803e53d5fca31d92b26118d640f96862460a7 172.19.206.22 26381 @ mymaster 139.224.101.91 6379 16328:X 16 Apr 2020 16:23:35.168 # +switch-master mymaster 139.224.101.91 6379 139.224.101.91 6381 16328:X 16 Apr 2020 16:23:35.168 * +slave slave 139.224.101.91:6380 139.224.101.91 6380 @ mymaster 139.224.101.91 6381 16328:X 16 Apr 2020 16:23:35.168 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 139.224.101.91 6381 16328:X 16 Apr 2020 16:23:35.168 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 139.224.101.91 6381 16328:X 16 Apr 2020 16:23:35.168 * +slave slave 139.224.101.91:6379 139.224.101.91 6379 @ mymaster 139.224.101.91 6381 16328:X 16 Apr 2020 16:23:35.451 * +sentinel-address-switch master mymaster 139.224.101.91 6381 ip 127.0.0.1 port 26381 for d17803e53d5fca31d92b26118d640f96862460a7
-
重启6379节点
[redis@iZuf62iexj3ztw81eg1cnoZ rwseparation]$ ./redis-cli -a redid123 -p 6379 127.0.0.1:6379> info replication # Replication role:slave master_host:127.0.0.1 master_port:6380 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_repl_offset:83568 master_link_down_since_seconds:12 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:74d75a1f73442dd635d6d4f67e948e827246bed9 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:83568 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:83569 repl_backlog_histlen:0 127.0.0.1:6379>
主观下线:
主观下线(Subjectively Down, 简称 SDOWN)指的是单个 Sentinel 实例对服务器做出的下线判断;如果一个服务器没有在 master-down-after-milliseconds 选项所指定的时间内, 对向它发送 PING 命令的 Sentinel 返回一个有效回复(valid reply), 那么 Sentinel 就会将这个服务器标记为主观下线
客观下线
指的是多个 Sentinel 实例在对同一个服务器做出 SDOWN 判断, 并且通过 SENTINEL is-master-down-by-addr 命令互相交流之后, 得出的服务器下线判断。 (一个 Sentinel 可以通过向另一个 Sentinel 发送 SENTINEL is-master-down-by-addr 命令来询问对方是否认为给定的服务器已下线。)
集群模式
项目架构
角色 | ip | port |
---|---|---|
c-1 | 139.224.101.91 | 7001 |
c-2 | 139.224.101.91 | 7002 |
c-3 | 139.224.101.91 | 7003 |
c-4 | 139.224.101.91 | 7004 |
c-5 | 139.224.101.91 | 7005 |
c-6 | 139.224.101.91 | 7006 |
前期准备
redis安装同单机安装
# 创建目录
mkdir -p /home/redis/software/redis-cluster/7001
mkdir -p /home/redis/software/redis-cluster/7002
mkdir -p /home/redis/software/redis-cluster/7003
mkdir -p /home/redis/software/redis-cluster/7004
mkdir -p /home/redis/software/redis-cluster/7005
mkdir -p /home/redis/software/redis-cluster/7006
# 复制配置文件
cp /home/redis/software/redis/redis.conf /home/redis/software/redis-cluster/7001
cp /home/redis/software/redis/redis.conf /home/redis/software/redis-cluster/7002
cp /home/redis/software/redis/redis.conf /home/redis/software/redis-cluster/7003
cp /home/redis/software/redis/redis.conf /home/redis/software/redis-cluster/7004
cp /home/redis/software/redis/redis.conf /home/redis/software/redis-cluster/7005
cp /home/redis/software/redis/redis.conf /home/redis/software/redis-cluster/7006
# 复制redis启动脚本
cp /home/redis/software/redis-install/bin/redis-server redis-cluster/
# 复制客户端连接脚本
cp /home/redis/software/redis-install/bin/redis-cli redis-cluster/
配置文件
-
相同部分
# redis后台运行 daemonize yes # 开启集群 cluster-enabled yes # 请求超时 cluster-node-timeout 5000 # 开启aof日志 appendonly yes # 注释掉绑定ip地址(云服务器外网访问) # bind 127.0.0.1 # 开启远程访问 protected-mode no
-
不同部分
# 端口,根据各节点配置端口 7001,7002,7003,7004,7005,7006 port 7001 # 集群配置,根据各节点配置 nodes-7001.conf,nodes-7002.conf,nodes-7003.conf,nodes-7004.conf,nodes-7005.conf,nodes-7006.conf, cluster-config-file nodes-7001.conf # aof文件名称 appendfilename "appendonly-7001.aof"
启动服务
-
各节点启动
/home/redis/software/redis-cluster/redis-server /home/redis/software/redis-cluster/7001/redis.conf /home/redis/software/redis-cluster/redis-server /home/redis/software/redis-cluster/7002/redis.conf /home/redis/software/redis-cluster/redis-server /home/redis/software/redis-cluster/7003/redis.conf /home/redis/software/redis-cluster/redis-server /home/redis/software/redis-cluster/7004/redis.conf /home/redis/software/redis-cluster/redis-server /home/redis/software/redis-cluster/7005/redis.conf /home/redis/software/redis-cluster/redis-server /home/redis/software/redis-cluster/7006/redis.conf
-
查看节点服务
[redis@iZuf62iexj3ztw81eg1cnoZ redis-cluster]$ ps -ef|grep redis root 11860 1032 0 21:41 ? 00:00:00 sshd: redis [priv] redis 11862 11860 0 21:41 ? 00:00:00 sshd: redis@pts/1 redis 11863 11862 0 21:41 pts/1 00:00:00 -bash redis 12003 1 0 21:54 ? 00:00:00 /home/redis/software/redis-cluster/redis-server *:7001 [cluster] redis 12005 1 0 21:54 ? 00:00:00 /home/redis/software/redis-cluster/redis-server *:7002 [cluster] redis 12007 1 0 21:54 ? 00:00:00 /home/redis/software/redis-cluster/redis-server *:7003 [cluster] redis 12012 1 0 21:54 ? 00:00:00 /home/redis/software/redis-cluster/redis-server *:7004 [cluster] redis 12017 1 0 21:54 ? 00:00:00 /home/redis/software/redis-cluster/redis-server *:7005 [cluster] redis 12028 1 0 21:54 ? 00:00:00 /home/redis/software/redis-cluster/redis-server *:7006 [cluster] redis 12034 11863 0 21:54 pts/1 00:00:00 ps -ef redis 12035 11863 0 21:54 pts/1 00:00:00 grep --color=auto redis [redis@iZuf62iexj3ztw81eg1cnoZ redis-cluster]$ [redis@iZuf62iexj3ztw81eg1cnoZ redis-cluster]$ ps -ef|grep redis root 11860 1032 0 21:41 ? 00:00:00 sshd: redis [priv] redis 11862 11860 0 21:41 ? 00:00:00 sshd: redis@pts/1 redis 11863 11862 0 21:41 pts/1 00:00:00 -bash root 12075 1032 0 22:07 ? 00:00:00 sshd: redis [priv] redis 12077 12075 0 22:07 ? 00:00:00 sshd: redis@pts/2 redis 12078 12077 0 22:07 pts/2 00:00:00 -bash redis 12192 1 0 22:09 ? 00:00:00 /home/redis/software/redis-cluster/redis-server 172.19.206.22:7001 [cluster] redis 12194 1 0 22:09 ? 00:00:00 /home/redis/software/redis-cluster/redis-server 172.19.206.22:7002 [cluster] redis 12199 1 0 22:09 ? 00:00:00 /home/redis/software/redis-cluster/redis-server 172.19.206.22:7003 [cluster] redis 12204 1 0 22:09 ? 00:00:00 /home/redis/software/redis-cluster/redis-server 172.19.206.22:7004 [cluster] redis 12206 1 0 22:09 ? 00:00:00 /home/redis/software/redis-cluster/redis-server 172.19.206.22:7005 [cluster] redis 12217 1 0 22:09 ? 00:00:00 /home/redis/software/redis-cluster/redis-server 172.19.206.22:7006 [cluster] redis 12221 12078 0 22:09 pts/2 00:00:00 ps -ef redis 12222 12078 0 22:09 pts/2 00:00:00 grep --color=auto redis [redis@iZuf62iexj3ztw81eg1cnoZ redis-cluster]$
安装ruby
使用root用户
-
安装ruby
yum -y install ruby ruby-devel rubygems rpm-build gem install redis
-
错误1
# 安装完ruby ruby-devel rubygems 执行gem install redis报错如下 # 解决:CentOS7 yum库中ruby的版本支持到 2.0.0,但是gem安装redis需要最低是2.3.0,采用rvm来更新ruby [redis@iZuf62iexj3ztw81eg1cnoZ ~]$ gem install redis ERROR: Error installing redis: redis requires Ruby version >= 2.3.0. [redis@iZuf62iexj3ztw81eg1cnoZ ~]$
-
安装rvm
gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3 curl -L get.rvm.io | bash -s stable
-
错误2
# 执行curl -L get.rvm.io | bash -s stable 报错如下 [root@iZuf62iexj3ztw81eg1cnoZ ~]# curl -L get.rvm.io | bash -s stable % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 194 100 194 0 0 179 0 0:00:01 0:00:01 --:--:-- 179 100 24535 100 24535 0 0 7689 0 0:00:03 0:00:03 --:--:-- 20226 Downloading https://github.com/rvm/rvm/archive/1.29.10.tar.gz Downloading https://github.com/rvm/rvm/releases/download/1.29.10/1.29.10.tar.gz.asc gpg: Signature made Thu 26 Mar 2020 05:58:42 AM CST using RSA key ID 39499BDB gpg: Can't check signature: No public key GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.10.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.10/1.29.10.tar.gz.asc'! Try to install GPG v2 and then fetch the public key: gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB or if it fails: command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import - command curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import - In case of further problems with validation please refer to https://rvm.io/rvm/security [root@iZuf62iexj3ztw81eg1cnoZ ~]# # 根据报错信息执行 gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB [root@iZuf62iexj3ztw81eg1cnoZ ~]# gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB gpg: requesting key D39DC0E3 from hkp server pool.sks-keyservers.net gpg: requesting key 39499BDB from hkp server pool.sks-keyservers.net gpg: key D39DC0E3: "Michal Papis (RVM signing)
" not changed gpg: key 39499BDB: public key "Piotr Kuczynski " imported gpg: no ultimately trusted keys found gpg: Total number processed: 2 gpg: imported: 1 (RSA: 1) gpg: unchanged: 1 [root@iZuf62iexj3ztw81eg1cnoZ ~]# -
再次安装rvm
########################################## # https://blog.csdn.net/Gushiyuta/article/details/90770681 ########################################## curl -L get.rvm.io | bash -s stable
[root@iZuf62iexj3ztw81eg1cnoZ ~]# curl -L get.rvm.io | bash -s stable % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 194 100 194 0 0 64 0 0:00:03 0:00:03 --:--:-- 64 100 24535 100 24535 0 0 6358 0 0:00:03 0:00:03 --:--:-- 11.0M Downloading https://github.com/rvm/rvm/archive/1.29.10.tar.gz Downloading https://github.com/rvm/rvm/releases/download/1.29.10/1.29.10.tar.gz.asc gpg: Signature made Thu 26 Mar 2020 05:58:42 AM CST using RSA key ID 39499BDB gpg: Good signature from "Piotr Kuczynski
" gpg: WARNING: This key is not certified with a trusted signature! gpg: There is no indication that the signature belongs to the owner. Primary key fingerprint: 7D2B AF1C F37B 13E2 069D 6956 105B D0E7 3949 9BDB GPG verified '/usr/local/rvm/archives/rvm-1.29.10.tgz' Creating group 'rvm' Installing RVM to /usr/local/rvm/ Installation of RVM in /usr/local/rvm/ is almost complete: * First you need to add all users that will be using rvm to 'rvm' group, and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`. * To start using RVM you need to run `source /etc/profile.d/rvm.sh` in all your open shell windows, in rare cases you need to reopen all shell windows. * Please do NOT forget to add your users to the rvm group. The installer no longer auto-adds root or users to the rvm group. Admins must do this. Also, please note that group memberships are ONLY evaluated at login time. This means that users must log out then back in before group membership takes effect! Thanks for installing RVM Please consider donating to our open collective to help us maintain RVM. Donate: https://opencollective.com/rvm/donate [root@iZuf62iexj3ztw81eg1cnoZ ~]# -
修改 rvm下载 ruby的源
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
-
rvm操作ruby
# 查看ruby版本 rvm list known # 安装2.3.3版本的ruby rvm install 2.3.3 # 切换ruby版本 rvm use 2.3.3 # 设置默认版本 rvm use 2.3.3 --default # 卸载其他版本 rvm remove 2.0.0 # 查看ruby版本 rvm -v
集群操作
-
开启集群
# 如果使用外网访问云服务器redis集群,启动集群时一定使用外网Ip地址,139.224.101.91是阿里云外网地址 /home/redis/software/redis-cluster/redis-cli --cluster create 139.224.101.91:7001 139.224.101.91:7002 139.224.101.91:7003 139.224.101.91:7004 139.224.101.91:7005 139.224.101.91:7006 --cluster-replicas 1
#######################注意:########################## # Can I set the above configuration? (type 'yes' to accept) # 一定填 yes 不要只填一个 y # 不然无法分配hash槽 [redis@iZuf62iexj3ztw81eg1cnoZ redis-cluster]$ /home/redis/software/redis-cluster/redis-cli --cluster create 139.224.101.91:7001 139.224.101.91:7002 139.224.101.91:7003 139.224.101.91:7004 139.224.101.91:7005 139.224.101.91:7006 --cluster-replicas 1 >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 139.224.101.91:7005 to 139.224.101.91:7001 Adding replica 139.224.101.91:7006 to 139.224.101.91:7002 Adding replica 139.224.101.91:7004 to 139.224.101.91:7003 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: d029faac54da0e0e2080dbd5ca46ad3a83b1df3a 139.224.101.91:7001 slots:[0-5460] (5461 slots) master M: ae1be08b05cb1646086148f33b5d3535d6480a39 139.224.101.91:7002 slots:[5461-10922] (5462 slots) master M: 2e97876867c4025ba66de865cd7901799902aee6 139.224.101.91:7003 slots:[10923-16383] (5461 slots) master S: d32be47e0cafd0b269f6ecadd5ce40ab1040296b 139.224.101.91:7004 replicates d029faac54da0e0e2080dbd5ca46ad3a83b1df3a S: 54f74c4766acea299cc9f15c7a87f1c115bafd36 139.224.101.91:7005 replicates ae1be08b05cb1646086148f33b5d3535d6480a39 S: 458d1641688413a4057208579426b72fbc7cb065 139.224.101.91:7006 replicates 2e97876867c4025ba66de865cd7901799902aee6 Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join ... >>> Performing Cluster Check (using node 139.224.101.91:7001) M: d029faac54da0e0e2080dbd5ca46ad3a83b1df3a 139.224.101.91:7001 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: ae1be08b05cb1646086148f33b5d3535d6480a39 139.224.101.91:7002 slots:[5461-10922] (5462 slots) master 1 additional replica(s) M: 2e97876867c4025ba66de865cd7901799902aee6 139.224.101.91:7003 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: d32be47e0cafd0b269f6ecadd5ce40ab1040296b 139.224.101.91:7004 slots: (0 slots) slave replicates d029faac54da0e0e2080dbd5ca46ad3a83b1df3a S: 54f74c4766acea299cc9f15c7a87f1c115bafd36 139.224.101.91:7005 slots: (0 slots) slave replicates ae1be08b05cb1646086148f33b5d3535d6480a39 S: 458d1641688413a4057208579426b72fbc7cb065 139.224.101.91:7006 slots: (0 slots) slave replicates 2e97876867c4025ba66de865cd7901799902aee6 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. [redis@iZuf62iexj3ztw81eg1cnoZ redis-cluster]$
-
测试集群
/home/redis/software/redis-cluster/redis-cli -c -h 139.224.101.91 -p 7001
-
关闭集群
/home/redis/software/redis-cluster/redis-cli -h 139.224.101.91 -p 7001 shutdown /home/redis/software/redis-cluster/redis-cli -h 139.224.101.91 -p 7002 shutdown /home/redis/software/redis-cluster/redis-cli -h 139.224.101.91 -p 7003 shutdown /home/redis/software/redis-cluster/redis-cli -h 139.224.101.91 -p 7004 shutdown /home/redis/software/redis-cluster/redis-cli -h 139.224.101.91 -p 7005 shutdown /home/redis/software/redis-cluster/redis-cli -h 139.224.101.91 -p 7006 shutdown
-
查看集群
/home/redis/software/redis-cluster/redis-cli -h 139.224.101.91 -p 7001 cluster nodes | grep master
数据分片
Redis 集群有16384个哈希槽,每个key通过CRC16校验后对16384取模来决定放置哪个槽,集群的每个节点负责一部分hash槽
节点操作
- 模拟节点故障
redis-cli -h 139.224.101.91 -p 7001 debug segfault
- 重哈希
/home/redis/software/redis-cluster/redis-cli --cluster reshard 139.224.101.91:7001
- 添加主节点:
redis-cli --cluster add-node 139.224.101.91:7006 139.224.101.91:7001
- 添加子节点
redis-cli --cluster add-node 139.224.101.91:7007 139.224.101.91:7001 --cluster-slave --cluster-master-id 主节点标识
- 删除子节点
redis-cli --cluster del-node 139.224.101.91:7007 子节点标识
- 删除主节点
redis-cli --cluster del-node 139.224.101.91:7006 主节点标识