在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Redis集群详解 Redis有三种集群模式,分别是:
三种集群模式各有特点,关于Redis介绍可以参考这里:NoSQL(二)——Redis Redis官网:https://redis.io/ ,最新版本6.0.5 主从模式 主从模式介绍 其中主从复制有如下特点: * 主数据库可以进行读写操作,当读写操作导致数据变化时会自动将数据同步给从数据库 * 从数据库一般都是只读的,并且接收主数据库同步过来的数据 * 一个master可以拥有多个slave,但是一个slave只能对应一个master * slave挂了不影响其他slave的读和master的读和写,重新启动后会将数据从master同步过来 * master挂了以后,不影响slave的读,但redis不再提供写服务,master重启后redis将重新对外提供写服务 * master挂了以后,不会在slave节点中重新选一个master 工作机制: 当slave启动后,主动向master发送SYNC命令。master接收到SYNC命令后在后台保存快照(RDB持久化)和缓存保存快照这段时间的命令,然后将保存的快照文件和缓存的命令发送给slave。slave接收到快照文件和命令后加载快照文件和缓存的执行命令。 复制初始化后,master每次接收到的写命令都会同步发送给slave,保证主从数据一致性。 安全设置: 当master节点设置密码后,
缺点: 从上面可以看出,master节点在主从模式中唯一,若master挂掉,则redis无法对外提供写服务。 主从模式搭建 环境准备:
全部下载安装: # cd /software # wget http://download.redis.io/releases/redis-6.0.5.tar.gz # tar zxf redis-5.0.4.tar.gz && mv redis-6.0.5/ /usr/local/redis # cd /usr/local/redis && make && make install # echo $? 0 全部配置成服务: 服务文件
shutdown脚本 # vim /usr/libexec/redis-shutdown #!/bin/bash # # Wrapper to close properly redis and sentinel test x"$REDIS_DEBUG" != x && set -x REDIS_CLI=/usr/local/bin/redis-cli # Retrieve service name SERVICE_NAME="$1" if [ -z "$SERVICE_NAME" ]; then SERVICE_NAME=redis fi # Get the proper config file based on service name CONFIG_FILE="/usr/local/redis/$SERVICE_NAME.conf" # Use awk to retrieve host, port from config file HOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1` PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1` PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1` SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1` # Just in case, use default host, port HOST=${HOST:-127.0.0.1} if [ "$SERVICE_NAME" = redis ]; then PORT=${PORT:-6379} else PORT=${PORT:-26739} fi # Setup additional parameters # e.g password-protected redis instances [ -z "$PASS" ] || ADDITIONAL_PARAMS="-a $PASS" # shutdown the service properly if [ -e "$SOCK" ] ; then $REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown else $REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown fi # chmod +x /usr/libexec/redis-shutdown # useradd -s /sbin/nologin redis # chown -R redis:redis /usr/local/redis # chown -R reids:redis /data/redis # yum install -y bash-completion && source /etc/profile #命令补全 # systemctl daemon-reload # systemctl enable redis 修改配置: 192.168.30.128 # mkdir -p /data/redis # vim /usr/local/redis/redis.conf bind 192.168.30.128 #监听ip,多个ip用空格分隔 daemonize yes #允许后台启动 logfile "/usr/local/redis/redis.log" #日志路径 dir /data/redis #数据库备份文件存放目录 masterauth 123456 #slave连接master密码,master可省略 requirepass 123456 #设置master连接密码,slave可省略 appendonly yes #在/data/redis/目录生成appendonly.aof文件,将每一次写操作请求都追加到appendonly.aof 文件中 # echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf # sysctl -p 192.168.30.129 # mkdir -p /data/redis # vim /usr/local/redis/redis.conf bind 192.168.30.129 daemonize yes logfile "/usr/local/redis/redis.log" dir /data/redis replicaof 192.168.30.128 6379 masterauth 123456 requirepass 123456 appendonly yes # echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf # sysctl -p 192.168.30.130 # mkdir -p /data/redis # vim /usr/local/redis/redis.conf bind 192.168.30.130 daemonize yes logfile "/usr/local/redis/redis.log" dir /data/redis replicaof 192.168.30.128 6379 masterauth 123456 requirepass 123456 appendonly yes # echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf # sysctl -p 全部启动redis:
查看集群状态: # redis-cli -h 192.168.30.128 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.30.128:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.30.129,port=6379,state=online,offset=168,lag=1 slave1:ip=192.168.30.130,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.30.129 -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.30.128 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 数据演示: 192.168.30.128:6379> keys * (empty list or set) 192.168.30.128:6379> set key1 100 OK 192.168.30.128:6379> set key2 lzx OK 192.168.30.128:6379> keys * 1) "key1" 2) "key2" # redis-cli -h 192.168.30.129 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.30.129:6379> keys * 1) "key2" 2) "key1" 192.168.30.129:6379> CONFIG GET dir 1) "dir" 2) "/data/redis" 192.168.30.129:6379> CONFIG GET dbfilename 1) "dbfilename" 2) "dump.rdb" 192.168.30.129:6379> get key1 "100" 192.168.30.129:6379> get key2 "lzx" 192.168.30.129:6379> set key3 aaa (error) READONLY You can't write against a read only replica. # redis-cli -h 192.168.30.130 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.30.130:6379> keys * 1) "key2" 2) "key1" 192.168.30.130:6379> CONFIG GET dir 1) "dir" 2) "/data/redis" 192.168.30.130:6379> CONFIG GET dbfilename 1) "dbfilename" 2) "dump.rdb" 192.168.30.130:6379> get key1 "100" 192.168.30.130:6379> get key2 "lzx" 192.168.30.130:6379> set key3 aaa (error) READONLY You can't write against a read only replica. 可以看到,在master节点写入的数据,很快就同步到slave节点上,而且在slave节点上无法写入数据。 Sentinel模式 Sentinel模式介绍 sentinel中文含义为哨兵,顾名思义,它的作用就是监控redis集群的运行状况,特点如下:
工作机制:
当使用sentinel模式的时候,客户端就不要直接连接Redis,而是连接sentinel的ip和port,由sentinel来提供具体的可提供服务的Redis实现,这样当master节点挂掉以后,sentinel就会感知并将新的master节点提供给使用者。 Sentinel模式搭建 环境准备:
修改配置: 前面已经下载安装了redis,这里省略,直接修改sentinel配置文件。 192.168.30.128
这里需要注意,sentinel auth-pass mymaster 123456需要配置在sentinel monitor mymaster 192.168.30.128 6379 2下面,否则启动报错: # /usr/local/bin/redis-sentinel /usr/local/redis/sentinel.conf *** FATAL CONFIG FILE ERROR *** Reading the configuration file, at line 104 >>> 'sentinel auth-pass mymaster 123456' No such master with specified name. 全部启动sentinel:
任一主机查看日志:
Sentinel模式下的几个事件:
master宕机演示: 192.168.30.128
从日志中可以看到,master已经从192.168.30.128转移到192.168.30.129上 192.168.30.129上查看集群信息 # /usr/local/bin/redis-cli -h 192.168.30.129 -p 6379 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.30.129:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=192.168.30.130,port=6379,state=online,offset=291039,lag=1 master_replid:757aff269236ed2707ba584a86a40716c1c76d74 master_replid2:47a862fc0ff20362be29096ecdcca6d432070ee9 master_repl_offset:291182 second_repl_offset:248123 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:291182 192.168.30.129:6379> set key4 linux OK 当前集群中只有一个slave——192.168.30.130,master是192.168.30.129,且192.168.30.129具有写权限。 192.168.30.130上查看redis的配置文件也可以看到replicaof 192.168.30.129 6379,这是sentinel在选举master是做的修改。 重新把192.168.30.128上进程启动
查看集群信息 # /usr/local/bin/redis-cli -h 192.168.30.128 -p 6379 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.30.128:6379> info replication # Replication role:slave master_host:192.168.30.129 master_port:6379 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:514774 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:757aff269236ed2707ba584a86a40716c1c76d74 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:514774 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:376528 repl_backlog_histlen:138247 192.168.30.128:6379> get key4 "linux" 192.168.30.128:6379> set key5 (error) ERR wrong number of arguments for 'set' command 即使192.168.30.128重新启动redis服务,也是作为slave加入redis集群,192.168.30.129仍然是master。 Cluster模式 Cluster模式介绍 cluster可以说是sentinel和主从模式的结合体,通过cluster可以实现主从和master重选功能,所以如果配置两个副本三个分片的话,就需要六个Redis实例。因为Redis的数据是根据一定规则分配到cluster的不同机器的,当数据量过大时,可以新增机器进行扩容。 使用集群,只需要将redis配置文件中的cluster-enable配置打开即可。每个集群中至少需要三个主数据库才能正常运行,新增节点非常方便。 cluster集群特点:
Cluster模式搭建 环境准备: 三台机器,分别开启两个redis服务(端口)
修改配置文件: 192.168.30.128
# vim /usr/local/redis/cluster/redis_7001.conf bind 192.168.30.128 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.30.129 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.30.128 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.30.129 6379 masterauth "123456" requirepass "123456" appendonly yes cluster-enabled yes cluster-config-file nodes_7002.conf cluster-node-timeout 15000 其它两台机器配置与192.168.30.128一致,此处省略 启动redis服务:
其它两台机器启动与192.168.30.128一致,此处省略 安装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 #目的是安装这个,用于配置集群
创建集群: 我这里是redis6.0.5,所以不需要安装ruby,直接创建集群即可 # redis-cli -a 123456 --cluster create 192.168.30.128:7001 192.168.30.128:7002 192.168.30.129:7003 192.168.30.129:7004 192.168.30.130:7005 192.168.30.130: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.30.129:7004 to 192.168.30.128:7001 Adding replica 192.168.30.130:7006 to 192.168.30.129:7003 Adding replica 192.168.30.128:7002 to 192.168.30.130:7005 M: 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001 slots:[0-5460] (5461 slots) master S: b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002 replicates 6788453ee9a8d7f72b1d45a9093838efd0e501f1 M: 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003 slots:[5461-10922] (5462 slots) master S: b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004 replicates 80c80a3f3e33872c047a8328ad579b9bea001ad8 M: 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005 slots:[10923-16383] (5461 slots) master S: 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130: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 可以看到,
自动生成nodes.conf文件: # ls /data/redis/cluster/redis_7001/ appendonly.aof dump.rdb nodes-7001.conf # vim /data/redis/cluster/redis_7001/nodes-7001.conf 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557454406312 5 connected 10923-16383 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557454407000 6 connected b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557454408371 5 connected 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 myself,master - 0 1557454406000 1 connected 0-5460 b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557454407366 4 connected 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 master - 0 1557454407000 3 connected 5461-10922 vars currentEpoch 6 lastVoteEpoch 0 集群操作 登录集群:
查看集群信息:
列出节点信息: 192.168.30.128:7001> CLUSTER NODES #列出节点信息 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557455176000 5 connected 10923-16383 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557455174000 6 connected b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557455175000 5 connected 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 myself,master - 0 1557455175000 1 connected 0-5460 b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557455174989 4 connected 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 master - 0 1557455175995 3 connected 5461-10922 这里与nodes.conf文件内容相同 写入数据: 192.168.30.128:7001> set key111 aaa -> Redirected to slot [13680] located at 192.168.30.130:7005 #说明数据到了192.168.30.130:7005上 OK 192.168.30.130:7005> set key222 bbb -> Redirected to slot [2320] located at 192.168.30.128:7001 #说明数据到了192.168.30.128:7001上 OK 192.168.30.128:7001> set key333 ccc -> Redirected to slot [7472] located at 192.168.30.129:7003 #说明数据到了192.168.30.129:7003上 OK 192.168.30.129:7003> get key111 -> Redirected to slot [13680] located at 192.168.30.130:7005 "aaa" 192.168.30.130:7005> get key333 -> Redirected to slot [7472] located at 192.168.30.129:7003 "ccc" 192.168.30.129:7003> 可以看出redis cluster集群是去中心化的,每个节点都是平等的,连接哪个节点都可以获取和设置数据。 当然,平等指的是master节点,因为slave节点根本不提供服务,只是作为对应master节点的一个备份。 增加节点: 192.168.30.129上增加一节点: # 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.30.129 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.30.129 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.30.130上增加一节点: # cp /usr/local/redis/cluster/redis_7005.conf /usr/local/redis/cluster/redis_7008.conf # vim /usr/local/redis/cluster/redis_7007.conf bind 192.168.30.130 port 7008 daemonize yes pidfile "/var/run/redis_7008.pid" logfile "/usr/local/redis/cluster/redis_7008.log" dir "/data/redis/cluster/redis_7008" #replicaof 192.168.30.130 6379 masterauth "123456" requirepass "123456" appendonly yes cluster-enabled yes cluster-config-file nodes_7008.conf cluster-node-timeout 15000 # mkdir /data/redis/cluster/redis_7008 # chown -R redis:redis /usr/local/redis && chown -R redis:redis /data/redis # redis-server /usr/local/redis/cluster/redis_7008.conf 集群中增加节点: 192.168.30.129:7003> CLUSTER MEET 192.168.30.129 7007 OK 192.168.30.129:7003> CLUSTER NODES 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 myself,master - 0 1557457361000 3 connected 5461-10922 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 master - 0 1557457364746 1 connected 0-5460 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557457362000 6 connected b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557457363000 4 connected b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557457362000 5 connected e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:7007@17007 master - 0 1557457362729 0 connected 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557457363739 5 connected 10923-16383 192.168.30.129:7003> CLUSTER MEET 192.168.30.130 7008 OK 192.168.30.129:7003> CLUSTER NODES 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 myself,master - 0 1557457489000 3 connected 5461-10922 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 master - 0 1557457489000 1 connected 0-5460 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557457489000 6 connected b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557457488000 4 connected b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557457489472 5 connected 1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:7008@17008 master - 0 1557457489259 0 connected e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:7007@17007 master - 0 1557457489000 0 connected 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557457490475 5 connected 10923-16383 可以看到,新增的节点都是以master身份加入集群的 更换节点身份: 将新增的192.168.30.130:7008节点身份改为192.168.30.129:7007的slave
cluster replicate后面跟node_id,更改对应节点身份。也可以登入集群更改 # redis-cli -c -h 192.168.30.130 -p 7008 -a 123456 192.168.30.130:7008> CLUSTER REPLICATE e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 OK 192.168.30.130:7008> CLUSTER NODES 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557458316881 3 connected 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 master - 0 1557458314864 1 connected 0-5460 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 master - 0 1557458316000 3 connected 5461-10922 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557458315872 5 connected 10923-16383 b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557458317890 5 connected 1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:7008@17008 myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557458315000 7 connected b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557458315000 1 connected e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:7007@17007 master - 0 1557458314000 0 connected 查看相应的nodes.conf文件,可以发现有更改,它记录当前集群的节点信息
删除节点:
保存配置:
可以看到,之前删除的节点又恢复了,这是因为对应的配置文件没有删除,执行CLUSTER SAVECONFIG恢复。 模拟master节点挂掉: 192.168.30.128 # netstat -lntp |grep 7001 tcp 0 0 192.168.30.128:17001 0.0.0.0:* LISTEN 6701/redis-server 1 tcp 0 0 192.168.30.128:7001 0.0.0.0:* LISTEN 6701/redis-server 1 # kill 6701
对应7001的一行可以看到,master fail,状态为disconnected;而对应7004的一行,slave已经变成master。 重新启动7001节点: # redis-server /usr/local/redis/cluster/redis_7001.conf 192.168.30.130:7008> CLUSTER NODES 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557461307000 3 connected 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 slave b6331cbc986794237c83ed2d5c30777c1551546e 0 1557461305441 8 connected 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 master - 0 1557461307962 3 connected 5461-10922 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557461304935 5 connected 10923-16383 b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557461306000 5 connected 1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:7008@17008 myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557461305000 7 connected b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 master - 0 1557461308972 8 connected 0-5460 e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:7007@17007 master - 0 1557461307000 0 connected 可以看到,7001节点启动后为slave节点,并且是7004的slave节点。即master节点如果挂掉,它的slave节点变为新master节点继续对外提供服务,而原来的master节点如果重启,则变为新master节点的slave节点。 另外,如果这里是拿7007节点做测试的话,会发现7008节点并不会切换,这是因为7007节点上根本没数据。集群数据被分为三份,采用哈希槽 (hash slot)的方式来分配16384个slot的话,它们三个节点分别承担的slot 区间是:
更多参考: |
请发表评论