Redis有三种集群模式,分别是:
主从模式是三种模式中最简单的,在主从复制中,数据库分为两类:主数据库(master)和从数据库(slave)。
其中主从复制有如下特点:
注意: 当master节点设置密码后,客户端访问master需要密码 启动slave需要密码,在配置文件中配置即可 客户端访问slave不需要密码
环境准备:
master节点 192.168.64.132
slave节点 192.168.64.133
slave节点 192.168.64.134
master节点配置
$ mkdir -p /data/redis
$ vim /usr/local/redis/redis.conf
bind 192.168.64.132 #监听ip,多个ip用空格分隔
port 6379
daemonize yes //启动redis后台运行
masterauth 123456 //slave连接master密码,master可省略
requirepass 123456 //设置master连接密码,slave可省略
logfile "/var/log/redis/redis.log" //日志路径
dir /data/redis //数据库备份文件存放目录
slave节点配置
$ mkdir -p /data/redis
$ vim /usr/local/redis/redis.conf
bind 192.168.64.133
port 6379
daemonize yes //启动redis后台运行
replicaof 192.168.64.132 6379
masterauth 123456 //slave连接master密码,master可省略
requirepass 123456
logfile "/var/log/redis/redis.log"
dir /data/redis //数据库备份文件存放目录
指定配置文件启动redis(全部节点都要启动)
$ redis-server /usr/local/redis/redis.conf --supervised systemd
查看集群状态
$ redis-cli -h 192.168.64.132 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.64.132:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.64.133,port=6379,state=online,offset=168,lag=1
slave1:ip=192.168.64.134,port=6379,state=online,offset=168,lag=1
master_replid:fb4941e02d5032ad74c6e2383211fc58963dbe90
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:168
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:168
$ redis-cli -h 192.168.64.133 -a 123456 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:192.168.64.132
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:196
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:fb4941e02d5032ad74c6e2383211fc58963dbe90
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:196
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:196
主从模式的弊端就是不具备高可用性,当master挂掉以后,Redis将不能再对外提供写入操作,因此sentinel应运而生。
sentinel中文含义为哨兵,顾名思义,它的作用就是监控redis集群的运行状况,特点如下:
工作机制:
每个sentinel以每秒钟一次的频率向它所知的master,slave以及其他sentinel实例发送一个 PING 命令
如果一个实例距离最后一次有效回复 PING 命令的时间超过 down-after-milliseconds 选项所指定的值, 则这个实例会被sentinel标记为主观下线。
如果一个master被标记为主观下线,则正在监视这个master的所有sentinel要以每秒一次的频率确认master的确进入了主观下线状态
当有足够数量的sentinel(大于等于配置文件指定的值)在指定的时间范围内确认master的确进入了主观下线状态, 则master会被标记为客观下线
在一般情况下, 每个sentinel会以每 10 秒一次的频率向它已知的所有master,slave发送 INFO 命令
当master被sentinel标记为客观下线时,sentinel向下线的master的所有slave发送 INFO 命令的频率会从 10 秒一次改为 1 秒一次
若没有足够数量的sentinel同意master已经下线,master的客观下线状态就会被移除;
若master重新向sentinel的 PING 命令返回有效回复,master的主观下线状态就会被移
当使用sentinel模式的时候,客户端就不要直接连接Redis,而是连接sentinel的ip和port,由sentinel来提供具体的可提供服务的Redis实现,这样当master节点挂掉以后,sentinel就会感知并将新的master节点提供给使用者。
环境准备:
master节点 192.168.64.132 sentinel端口:26379
slave节点 192.168.64.133 sentinel端口:26379
slave节点 192.168.64.134 sentinel端口:26379
修改配置
根据前面搭建好的主从reids集群后,直接修改sentinel配置
$ vim /usr/local/redis/sentinel.conf
daemonize yes
logfile "/usr/local/redis/sentinel.log"
dir "/usr/local/redis/sentinel" #sentinel工作目录
sentinel monitor mymaster 192.168.64.132 6379 2 #判断master失效至少需要2个sentinel同意,建议设置为n/2+1,n为sentinel个数
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000 #判断master主观下线时间,默认30s
这里需要注意,sentinel auth-pass mymaster 123456
需要配置在sentinel monitor mymaster 192.168.30.128 6379 2
下面,否则启动报错
指定配置文件启动sentinel(全部节点都要启动)
$ mkdir /usr/local/redis/sentinel && chown -R redis:redis /usr/local/redis
$ redis-sentinel /usr/local/redis/sentinel.conf
任一主机查看日志:
$ tail -f /usr/local/redis/sentinel.log
21574:X 09 May 2019 15:32:04.298 # Sentinel ID is 30c417116a8edbab09708037366c4a7471beb770
21574:X 09 May 2019 15:32:04.298 # +monitor master mymaster 192.168.64.132 6379 quorum 2
21574:X 09 May 2019 15:32:04.299 * +slave slave 192.168.64.133:6379 192.168.64.134 6379 @ mymaster 192.168.64.132 6379
21574:X 09 May 2019 15:32:04.300 * +slave slave 192.168.64.133:6379 192.168.64.134 6379 @ mymaster 192.168.64.132 6379
21574:X 09 May 2019 15:32:16.347 * +sentinel sentinel 79b8d61626afd4d059fb5a6a63393e9a1374e78f 192.168.64.133 26379 @ mymaster 192.168.64.132 6379
21574:X 09 May 2019 15:32:31.584 * +sentinel sentinel d7b429dcba792103ef0d80827dd0910bd9284d21 192.168.64.134 26379 @ mymaster 192.168.64.1326379
Sentinel模式下的几个事件:
· +reset-master :主服务器已被重置。
· +slave :一个新的从服务器已经被 Sentinel 识别并关联。
· +failover-state-reconf-slaves :故障转移状态切换到了 reconf-slaves 状态。
· +failover-detected :另一个 Sentinel 开始了一次故障转移操作,或者一个从服务器转换成了主服务器。
· +slave-reconf-sent :领头(leader)的 Sentinel 向实例发送了 [SLAVEOF](/commands/slaveof.html) 命令,为实例设置新的主服务器。
· +slave-reconf-inprog :实例正在将自己设置为指定主服务器的从服务器,但相应的同步过程仍未完成。
· +slave-reconf-done :从服务器已经成功完成对新主服务器的同步。
· -dup-sentinel :对给定主服务器进行监视的一个或多个 Sentinel 已经因为重复出现而被移除 —— 当 Sentinel 实例重启的时候,就会出现这种情况。
· +sentinel :一个监视给定主服务器的新 Sentinel 已经被识别并添加。
· +sdown :给定的实例现在处于主观下线状态。
· -sdown :给定的实例已经不再处于主观下线状态。
· +odown :给定的实例现在处于客观下线状态。
· -odown :给定的实例已经不再处于客观下线状态。
· +new-epoch :当前的纪元(epoch)已经被更新。
· +try-failover :一个新的故障迁移操作正在执行中,等待被大多数 Sentinel 选中(waiting to be elected by the majority)。
· +elected-leader :赢得指定纪元的选举,可以进行故障迁移操作了。
· +failover-state-select-slave :故障转移操作现在处于 select-slave 状态 —— Sentinel 正在寻找可以升级为主服务器的从服务器。
· no-good-slave :Sentinel 操作未能找到适合进行升级的从服务器。Sentinel 会在一段时间之后再次尝试寻找合适的从服务器来进行升级,又或者直接放弃执行故障转移操作。
· selected-slave :Sentinel 顺利找到适合进行升级的从服务器。
· failover-state-send-slaveof-noone :Sentinel 正在将指定的从服务器升级为主服务器,等待升级功能完成。
· failover-end-for-timeout :故障转移因为超时而中止,不过最终所有从服务器都会开始复制新的主服务器(slaves will eventually be configured to replicate with the new master anyway)。
· failover-end :故障转移操作顺利完成。所有从服务器都开始复制新的主服务器了。
· +switch-master :配置变更,主服务器的 IP 和地址已经改变。 这是绝大多数外部用户都关心的信息。
· +tilt :进入 tilt 模式。
· -tilt :退出 tilt 模式。
sentinel模式基本可以满足一般生产的需求,具备高可用性。但是当数据量过大到一台服务器存放不下的情况时,主从模式或sentinel模式就不能满足需求了,这个时候需要对存储的数据进行分片,将数据存储到多个Redis实例中。cluster模式的出现就是为了解决单机Redis容量有限的问题,将Redis的数据根据一定的规则分配到多台机器。
cluster可以说是sentinel和主从模式的结合体,通过cluster可以实现主从和master重选功能,所以如果配置两个副本三个分片的话,就需要六个Redis实例。因为Redis的数据是根据一定规则分配到cluster的不同机器的,当数据量过大时,可以新增机器进行扩容。
使用集群,只需要将redis配置文件中的cluster-enable配置打开即可。每个集群中至少需要三个主数据库才能正常运行,新增节点非常方便。
cluster集群特点:
环境准备
三台机器,分别开启两个redis服务(端口)
192.168.64.132 端口:7001,7002
192.168.64.133 端口:7003,7004
192.168.64.134 端口:7005,7006
修改配置文件
192.168.64.132
$ mkdir /usr/local/redis/cluster
$ cp /usr/local/redis/redis.conf /usr/local/redis/cluster/redis_7001.conf
$ cp /usr/local/redis/redis.conf /usr/local/redis/cluster/redis_7002.conf
$ chown -R redis:redis /usr/local/redis
$ mkdir -p /data/redis/cluster/{redis_7001,redis_7002} && chown -R redis:redis /data/redis
$ vim /usr/local/redis/cluster/redis_7001.conf
bind 192.168.64.132
port 7001
daemonize yes
pidfile "/var/run/redis_7001.pid"
logfile "/usr/local/redis/cluster/redis_7001.log"
dir "/data/redis/cluster/redis_7001"
#replicaof 192.168.64.133 6379
masterauth 123456
requirepass 123456
appendonly yes
cluster-enabled yes
cluster-config-file nodes_7001.conf
cluster-node-timeout 15000
$ vim /usr/local/redis/cluster/redis_7002.conf
bind 192.168.64.132
port 7002
daemonize yes
pidfile "/var/run/redis_7002.pid"
logfile "/usr/local/redis/cluster/redis_7002.log"
dir "/data/redis/cluster/redis_7002"
#replicaof 192.168.64.133 6379
masterauth 123456
requirepass 123456
appendonly yes
cluster-enabled yes
cluster-config-file nodes_7002.conf
cluster-node-timeout 15000
其他两台服务器配置与此一直,此处省略
启动redis服务(各个节点都要启动)
$ redis-server /usr/local/redis/cluster/redis_7001.conf
$ tail -f /usr/local/redis/cluster/redis_7001.log
$ redis-server /usr/local/redis/cluster/redis_7002.conf
$ tail -f /usr/local/redis/cluster/redis_7002.log
安装ruby并创建集群(低版本):
如果redis版本比较低,则需要安装ruby。任选一台机器安装ruby即可
$ yum -y groupinstall "Development Tools"
$ yum install -y gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-devel
$ mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
$ wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P ~/rpmbuild/SOURCES
$ wget http://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P ~/rpmbuild/SPECS
$ rpmbuild -bb ~/rpmbuild/SPECS/ruby22x.spec
$ rpm -ivh ~/rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.x86_64.rpm
$ gem install redis #目的是安装这个,用于配置集群
$ cp /usr/local/redis/src/redis-trib.rb /usr/bin/
$ redis-trib.rb create --replicas 1 192.168.64.132:7001 192.168.64.132:7002 192.168.64.133:7003 192.168.64.133:7004 192.168.64.134:7005 192.168.64.134:7006
创建集群
如果是redis5.0.4,就不需要安装ruby,直接创建集群即可
$ redis-cli -a 123456 --cluster create 192.168.64.132:7001 192.168.64.132:7002 192.168.64.133:7003 192.168.64.133:7004 192.168.64.134:7005 192.168.64.134:7006 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.64.133:7004 to 192.168.64.132:7001
Adding replica 192.168.64.134:7006 to 192.168.64.133:7003
Adding replica 192.168.64.132:7002 to 192.168.64.133:7005
M: 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.64.132:7001
slots:[0-5460] (5461 slots) master
S: b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.64.132:7002
replicates 6788453ee9a8d7f72b1d45a9093838efd0e501f1
M: 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.64.133:7003
slots:[5461-10922] (5462 slots) master
S: b6331cbc986794237c83ed2d5c30777c1551546e 192.168.64.133:7004
replicates 80c80a3f3e33872c047a8328ad579b9bea001ad8
M: 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.64.134:7005
slots:[10923-16383] (5461 slots) master
S: 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.64.134:7006
replicates 4d74ec66e898bf09006dac86d4928f9fad81f373
Can I set the above configuration? (type 'yes' to accept): yes #输入yes,接受上面配置
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
根据上面的信息可以知道:
192.168.64.132:7001是master,它的slave是192.168.64.133:7004;
192.168.64.133:7003是master,它的slave是192.168.64.134:7006;
192.168.64.134:7005是master,它的slave是192.168.64.132:7002
集群操作
$ redis-cli -c -h 192.168.64.132 -p 7001 -a 123456 # -c,使用集群方式登录
192.168.30.128:7001> CLUSTER INFO #集群状态
192.168.30.128:7001> CLUSTER NODES #列出节点信息
增加节点
192.168.64.133上增加一节点:
$ cp /usr/local/redis/cluster/redis_7003.conf /usr/local/redis/cluster/redis_7007.conf
$ vim /usr/local/redis/cluster/redis_7007.conf
bind 192.168.64.133
port 7007
daemonize yes
pidfile "/var/run/redis_7007.pid"
logfile "/usr/local/redis/cluster/redis_7007.log"
dir "/data/redis/cluster/redis_7007"
#replicaof 192.168.64.133 6379
masterauth "123456"
requirepass "123456"
appendonly yes
cluster-enabled yes
cluster-config-file nodes_7007.conf
cluster-node-timeout 15000
# mkdir /data/redis/cluster/redis_7007
# chown -R redis:redis /usr/local/redis && chown -R redis:redis /data/redis
# redis-server /usr/local/redis/cluster/redis_7007.conf
集群中增加节点:
192.168.64.133:7003> CLUSTER MEET 192.168.64.133 7007
OK
更换节点身份:
将新增的192.168.64.133:7007节点身份改为192.168.64.133:7003的slave
$ redis-cli -c -h 192.168.64.133 -p 7007 -a 123456 cluster replicate e51ab166bc0f33026887bcf8eba0dff3d5b0bf14
cluster replicate
后面跟node_id,更改对应节点身份。也可以登入集群更改
$ redis-cli -c -h 192.168.64.133 -p 7007 -a 123456
192.168.64.133:7007> CLUSTER REPLICATE e51ab166bc0f33026887bcf8eba0dff3d5b0bf14
OK
删除节点
192.168.64.133:7007> CLUSTER FORGET 1a1c7f02fce87530bd5abdfc98df1cffce4f1767
(error) ERR I tried hard but I can't forget myself... #无法删除登录节点
192.168.64.133:7007> CLUSTER FORGET e51ab166bc0f33026887bcf8eba0dff3d5b0bf14
(error) ERR Can't forget my master! #不能删除自己的master节点
192.168.64.133:7007> CLUSTER FORGET 6788453ee9a8d7f72b1d45a9093838efd0e501f1
OK #可以删除其它的master节点