虚拟机拷贝2-3台centos机器,作为Redis6集群的主备节点
部署规划:
master
192.168.28.132 8001
192.168.28.132 8002
192.168.28.132 8003
slaver
192.168.28.135 8001
192.168.28.135 8002
192.168.28.135 8003
一、安装依赖环境
1、安装编译依赖
yum install gcc tcl tcl-devel
此处使用yum,也可以下载RPM包后安装
2、安装Ruby和Ruby的redis gem包
说明:
Ruby 是一门语言,可以用来编写脚本,运行于 Ruby 环境。
RubyGems 是管理 Ruby 脚本的工具,可以通过连接 ruby 脚本源,在线下载安装 ruby 脚本。类似于前端的 node、Java 的 maven,Linux的rpm/yum.
redis-xxx.gem 是redis 对接 ruby 的脚本。
redis-trib.rb 是基于 redis 提供的集群命令,封装成的操作工具。
[root@centos7-135 ~]# yum install ruby -y
...
[root@centos7-135 redis-gem]# ll
总用量 52
-rw-r--r--. 1 root root 49664 6月 4 2023 redis-5.0.5.gem
[root@centos7-135 redis-gem]# gem install -l redis-5.0.5.gem
ERROR: While executing gem ... (Gem::DependencyError)
Unable to resolve dependencies: redis requires redis-client (>= 0.9.0)
[root@centos7-135 redis-gem]# gem --version
2.0.14.1
[root@centos7-135 redis-gem]# gem update --system
Updating rubygems-update
Fetching: rubygems-update-3.4.13.gem (100%)
ERROR: Error installing rubygems-update:
rubygems-update requires Ruby version >= 2.6.0.
ERROR: While executing gem ... (NoMethodError)
undefined method `version' for nil:NilClass
#移除包,使用源码方式
[root@centos7-135 redis-gem]# yum erase ruby
Ruby下载地址
此处下载Ruby 2.7.8
sha256: c2dab63cbc8f2a05526108ad419efa63a67ed4074dbbcf9fc2b1ca664cb45ba0
[root@centos7-135 ruby]# sha256sum ruby-3.1.4.tar.gz
a3d55879a0dfab1d7141fdf10d22a07dbf8e5cdc4415da1bde06127d5cc3c7b6 ruby-3.1.4.tar.gz
通过源码编译安装
$ ./configure
$ make
$ sudo make install
默认情况下,Ruby 安装到 /usr/local 目录。如果想使用其他目录,可以把 --prefix=DIR 选项传给 ./configure 脚本。
因为无法使用任何工具来管理通过源码编译安装的 Ruby,所以使用第三方工具或者包管理器或许是更好的选择。
[root@centos7-135 ruby]# sha256sum ruby-2.7.8.tar.gz
c2dab63cbc8f2a05526108ad419efa63a67ed4074dbbcf9fc2b1ca664cb45ba0 ruby-2.7.8.tar.gz
[root@centos7-135 ruby]# tar zxf ruby-2.7.8.tar.gz
[root@centos7-135 ruby]# cd ruby-2.7.8
[root@centos7-135 ruby]# make
[root@centos7-135 ruby]# make install
#验证
[root@centos7-135 bin]# ./ruby -v
ruby 2.7.8p225 (2023-03-30 revision 1f4d455848) [x86_64-linux]
[root@centos7-135 bin]# vim /etc/profile
#在文件最后 PATH上添加ruby安装路径。没有PATH,则这样写。
export PATH=$PATH:/app/ruby/bin
[root@centos7-135 bin]# . /etc/profile
[root@centos7-135 ~]# ruby -v
ruby 2.7.8p225 (2023-03-30 revision 1f4d455848) [x86_64-linux]
[root@centos7-135 ~]#
3、安装 RubyGems
RubyGems下载地址
[root@centos7-135 rubygems]# tar zxf rubygems-3.4.13.tgz
[root@centos7-135 rubygems]# cd rubygems-3.4.13
[root@centos7-135 rubygems-3.4.13]# ruby setup.rb
Successfully built RubyGem
Name: bundler
Version: 2.4.13
File: bundler-2.4.13.gem
Bundler 2.4.13 installed
RubyGems 3.4.13 installed
Regenerating binstubs
Regenerating plugins
Parsing documentation for rubygems-3.4.13
....
[root@centos7-135 bin]# which gem
/app/ruby/bin/gem
[root@centos7-135 bin]#
[root@centos7-135 ~]# gem -v
3.4.13
[root@centos7-135 ~]#
4、安装 redis-3.3.5.gem
[root@centos7-135 redis-gem]# gem install -l redis-3.3.5.gem
Successfully installed redis-3.3.5
Parsing documentation for redis-3.3.5
Installing ri documentation for redis-3.3.5
Done installing documentation for redis after 0 seconds
1 gem installed
[root@centos7-135 redis-gem]# ll
总用量 144
-rw-r--r--. 1 root root 92672 6月 4 2023 redis-3.3.5.gem
-rw-r--r--. 1 root root 49664 6月 4 2023 redis-5.0.5.gem
[root@centos7-135 redis-gem]#
二、编译安装Redis6
Redis下载地址
此处下载 redis-6.2.12.tar.gz
[root@centos7-135 app]# tar zxf redis-6.2.12.tar.gz
[root@centos7-135 app]# cd redis-6.2.12
# make distclean 如果之前有make失败,需清除缓存再执行make命令
[root@centos7-135 redis-6.2.12]# make -j8
LINK redis-benchmark
LINK redis-server
INSTALL redis-sentinel
INSTALL redis-check-rdb
INSTALL redis-check-aof
LINK redis-cli
Hint: It's a good idea to run 'make test' ;)
make[1]: 离开目录“/app/redis-6.2.12/src”
[root@centos7-135 redis-6.2.12]# make PREFIX=/app/redis install
cd src && make install
make[1]: 进入目录“/app/redis-6.2.12/src”
CC Makefile.dep
make[1]: 离开目录“/app/redis-6.2.12/src”
make[1]: 进入目录“/app/redis-6.2.12/src”
Hint: It's a good idea to run 'make test' ;)
INSTALL redis-server
INSTALL redis-benchmark
INSTALL redis-cli
make[1]: 离开目录“/app/redis-6.2.12/src”
[root@centos7-135 redis-6.2.12]# cp src/redis-trib.rb /app/redis/bin/
[root@centos7-135 redis-6.2.12]# make test
[root@centos7-135 bin]# vim /etc/profile
# 把/app/redis/bin加入到PATH
# vi $HOME/.profile
export PATH=$PATH:/app/redis/bin
[root@centos7-135 bin]# . /etc/profile
[root@centos7-135 bin]# redis-cli -v
redis-cli 6.2.12
[root@centos7-135 bin]# redis-server -v
Redis server v=6.2.12 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=2dbe6f900c3eed0e
三、安装Redis6集群
基本操作均整合成为脚本,方便操作。
1、创建Redis节点
# 创建节点目录并写入redis.conf
[root@centos7-135 bin]# cat >genredis.sh
#!/bin/bash
cd /app/redis/
seq 8001 8003 | while read rport
do
mkdir $rport
cat > $rport/redis.conf<<EOF
bind 0.0.0.0
port 8000
protected-mode no
tcp-backlog 32768
timeout 300
tcp-keepalive 60
daemonize yes
supervised no
pidfile "/app/redis/8000/redis.pid"
loglevel notice
logfile "/app/redis/8000/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error no
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir /app/redis/8000
masterauth "123456"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-ping-slave-period 10
repl-timeout 60
repl-disable-tcp-nodelay no
repl-backlog-size 100mb
repl-backlog-ttl 3600
slave-priority 100
#requirepass "123456"
maxclients 10000
maxmemory-policy allkeys-lfu
maxmemory-samples 5
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
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 no
lua-time-limit 5000
cluster-enabled yes
cluster-config-file "/app/redis/8000/nodes.conf"
cluster-node-timeout 15000
cluster-slave-validity-factor 100
cluster-migration-barrier 1
cluster-require-full-coverage no
cluster-slave-no-failover no
slowlog-log-slower-than 10000
slowlog-max-len 1280
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 0 0 0
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
EOF
sed -i "s/8000/$rport/g" $rport/redis.conf
done
执行脚本安装Redis主节点
[root@centos7-135 bin]# chmod +x genredis.sh
[root@centos7-135 bin]# sh genredis.sh
[root@centos7-135 bin]# cd ..
[root@centos7-135 redis]# ll
总用量 0
drwxr-xr-x. 2 root root 24 6月 3 21:53 8001
drwxr-xr-x. 2 root root 24 6月 3 21:53 8002
drwxr-xr-x. 2 root root 24 6月 3 21:53 8003
drwxr-xr-x. 2 root root 174 6月 3 21:47 bin
2、创建Redis启动脚本
[root@centos7-135 redis]# cat >start-redis.sh
#!/bin/bash
. /etc/profile
#. $HOME/.profile
REDIS_HOME=/app/redis
REDIS_SERVER="$REDIS_HOME/bin/redis-server"
case "$1" in
8001)
$REDIS_SERVER $REDIS_HOME/8001/redis.conf
echo "Redis Server 8001 Started"
;;
8002)
$REDIS_SERVER $REDIS_HOME/8002/redis.conf
echo "Redis Server 8002 Started"
;;
8003)
$REDIS_SERVER $REDIS_HOME/8003/redis.conf
echo "Redis Server 8003 Started"
;;
*)
echo "Usage: $0 {8001|8002|8003}"
exit 1
esac
exit 0
启动Redis主节点
[root@centos7-135 redis]# chmod +x start-redis.sh
[root@centos7-135 redis]# /app/redis/start-redis.sh 8001
Redis Server 8001 Started
[root@centos7-135 redis]# /app/redis/start-redis.sh 8002
Redis Server 8002 Started
[root@centos7-135 redis]# /app/redis/start-redis.sh 8003
Redis Server 8003 Started
[root@centos7-135 redis]# ps -ef|grep redis |grep -v grep
root 22097 1 0 22:05 ? 00:00:00 /app/redis/bin/redis-server 0.0.0.0:8001 [cluster]
root 22120 1 0 22:05 ? 00:00:00 /app/redis/bin/redis-server 0.0.0.0:8002 [cluster]
root 22142 1 0 22:05 ? 00:00:00 /app/redis/bin/redis-server 0.0.0.0:8003 [cluster]
[root@centos7-135 redis]#
3、安装Redis备节点
[root@centos7-135 app]# scp -r redis root@192.168.28.132:/app/
[root@www redis]# ll
总用量 4
drwxr-xr-x 2 root root 76 6月 3 22:19 8001
drwxr-xr-x 2 root root 76 6月 3 22:19 8002
drwxr-xr-x 2 root root 76 6月 3 22:19 8003
drwxr-xr-x 2 root root 174 6月 3 22:19 bin
-rwxr-xr-x 1 root root 454 6月 3 22:19 start-redis.sh
[root@www 8001]# rm nodes.conf redis.log redis.pid -f
[root@www 8001]# cd ../8002
[root@www 8002]# rm nodes.conf redis.log redis.pid -f
[root@www 8002]# cd ../8003/
[root@www 8003]# rm nodes.conf redis.log redis.pid -f
[root@www 8003]# /app/redis/start-redis.sh 8001
Redis Server 8001 Started
[root@www 8003]# /app/redis/start-redis.sh 8002
Redis Server 8002 Started
[root@www 8003]# /app/redis/start-redis.sh 8003
Redis Server 8003 Started
[root@www 8003]# ps -ef|grep redis |grep -v gre
root 3350 1 0 22:31 ? 00:00:00 /app/redis/bin/redis-server 0.0.0.0:8001 [cluster]
root 3372 1 0 22:31 ? 00:00:00 /app/redis/bin/redis-server 0.0.0.0:8002 [cluster]
root 3393 1 0 22:31 ? 00:00:00 /app/redis/bin/redis-server 0.0.0.0:8003 [cluster]
4、创建仅master节点的Redis cluster(root)
说明:如果由Redis自动创建主备节点,则节点可能不能按照期望的均衡分布在不同机器,达到容错的效果。所以此处只创建主节点,后续手工按期望创建备节点。
[root@centos7-135 redis]# redis-trib.rb create --replicas 0 192.168.28.135:8001 192.168.28.135:8002 192.168.28.135:8003
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.
All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.
Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]
Example:
redis-cli --cluster create 192.168.28.135:8001 192.168.28.135:8002 192.168.28.135:8003 --cluster-replicas 0
To get help about all subcommands, type:
redis-cli --cluster help
#查证发现redis5后已经不在依赖Ruby安装集群。前文安装的Ruby在Redis6属于无用功。但是Redis5之前仍可使用,故本文没有去除这部分。
[root@centos7-135 redis]# redis-cli --cluster create 192.168.28.135:8001 192.168.28.135:8002 192.168.28.135:8003 --cluster-replicas 0
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001
slots:[0-5460] (5461 slots) master
M: 3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002
slots:[5461-10922] (5462 slots) master
M: 31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003
slots:[10923-16383] (5461 slots) master
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 192.168.28.135:8001)
M: a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001
slots:[0-5460] (5461 slots) master
M: 31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003
slots:[10923-16383] (5461 slots) master
M: 3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002
slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
5、添加slave节点(root)
上一步骤中创建的集群仅有master节点,需添加slave节点实现高可用,手动添加slave以实现和master不同主机,避免主机故障时,主从节点同时挂掉.
redis-cli --cluster add-node slaver_ip:port master_ip:port --cluster-slave --cluster-master-id cluster-master-id
# 确认主节点信息
[root@centos7-135 redis]# redis-cli -h 192.168.28.135 -p 8001 cluster nodes
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 master - 0 1685807437691 3 connected 10923-16383
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 myself,master - 0 1685807435000 1 connected 0-5460
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685807436677 2 connected 5461-10922
[root@centos7-135 redis]#
# 根据master‐id 按实际需求添加跨主机的slave节点
#添加 192.168.28.132:8001 为192.168.28.135:8001备节点
[root@centos7-135 redis]# redis-cli --cluster add-node 192.168.28.132:8001 192.168.28.135:8001 --cluster-slave --cluster-master-id a32d7ada6b880161c04cd43da62b409675b934ee
>>> Adding node 192.168.28.132:8001 to cluster 192.168.28.135:8001
>>> Performing Cluster Check (using node 192.168.28.135:8001)
M: a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001
slots:[0-5460] (5461 slots) master
M: 31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003
slots:[10923-16383] (5461 slots) master
M: 3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002
slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.28.132:8001 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.28.135:8001.
[OK] New node added correctly.
#添加 192.168.28.132:8002 为192.168.28.135:8002备节点
[root@centos7-135 redis]# redis-cli --cluster add-node 192.168.28.132:8002 192.168.28.135:8002 --cluster-slave --cluster-master-id 3ad04092aa1f5080baa6b3777d580e710cf998f8
>>> Adding node 192.168.28.132:8002 to cluster 192.168.28.135:8002
>>> Performing Cluster Check (using node 192.168.28.135:8002)
M: 3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002
slots:[5461-10922] (5462 slots) master
S: fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001
slots: (0 slots) slave
replicates a32d7ada6b880161c04cd43da62b409675b934ee
M: a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003
slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.28.132:8002 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.28.135:8002.
[OK] New node added correctly.
#添加 192.168.28.132:8003 为192.168.28.135:8003备节点
[root@centos7-135 redis]# redis-cli --cluster add-node 192.168.28.132:8003 192.168.28.135:8003 --cluster-slave --cluster-master-id 31b89449ab8b3109f8f56eccbb66badf9b6105bd
>>> Adding node 192.168.28.132:8003 to cluster 192.168.28.135:8003
>>> Performing Cluster Check (using node 192.168.28.135:8003)
M: 31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003
slots:[10923-16383] (5461 slots) master
S: fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001
slots: (0 slots) slave
replicates a32d7ada6b880161c04cd43da62b409675b934ee
S: 18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002
slots: (0 slots) slave
replicates 3ad04092aa1f5080baa6b3777d580e710cf998f8
M: a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.28.132:8003 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.28.135:8003.
[OK] New node added correctly.
6、确认Redis集群安装情况
[root@centos7-135 redis]# redis-cli -h 192.168.28.135 -p 8001 cluster nodes
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 myself,master - 0 1685807606000 1 connected 0-5460
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685807609401 2 connected 5461-10922
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 slave a32d7ada6b880161c04cd43da62b409675b934ee 0 1685807608390 1 connected
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 master - 0 1685807611447 3 connected 10923-16383
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685807610426 2 connected
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685807610000 3 connected
[root@www redis]# redis-cli -h 192.168.28.132 -p 8001 cluster nodes
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685808117461 3 connected
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master - 0 1685808119488 1 connected 0-5460
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685808116000 2 connected 5461-10922
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 master - 0 1685808120503 3 connected 10923-16383
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 myself,slave a32d7ada6b880161c04cd43da62b409675b934ee 0 1685808118000 1 connected
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685808118473 2 connected
[root@www redis]#
7、启用客户端密码认证
4.0及之前版本的redis创建cluster时,须禁用requirepass选项,否则可能会报错无法连接redis节点,在创建完Redis cluster后,启用redis.conf的requirepass选项,再重启所有redis节点。从redis 5.0开始,集群管理集成到了redis‐cli中,支持在配置了requirepass选项的情况下创建redis
cluster,理论上无需创建完集群后再设置密码,故以下操作主要针对5.0以前版本。
[root@centos7-135 redis]# redis-cli -p 8001 shutdown save
[root@centos7-135 redis]# redis-cli -p 8002 shutdown save
[root@centos7-135 redis]# redis-cli -p 8003 shutdown save
[root@centos7-135 redis]# sed -i 's|#requirepass|requirepass|g' 8001/redis.conf
[root@centos7-135 redis]# sed -i 's|#requirepass|requirepass|g' 8002/redis.conf
[root@centos7-135 redis]# sed -i 's|#requirepass|requirepass|g' 8003/redis.conf
[root@centos7-135 redis]# sh start-redis.sh 8001
Redis Server 8001 Started
[root@centos7-135 redis]# sh start-redis.sh 8002
Redis Server 8002 Started
[root@centos7-135 redis]# sh start-redis.sh 8003
Redis Server 8003 Started
[root@centos7-135 redis]# redis-cli -h 192.168.28.135 -p 8003 cluster nodes
NOAUTH Authentication required.
[root@centos7-135 redis]# redis-cli -h 192.168.28.135 -p 8003 -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685808931000 3 connected 10923-16383
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 slave a32d7ada6b880161c04cd43da62b409675b934ee 0 1685808930783 1 connected
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685808929774 2 connected
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685808929000 3 connected
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master - 0 1685808931792 1 connected 0-5460
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685808932803 2 connected 5461-10922
8、验证高可用
模拟主节点下线并查看主节点转移情况
#关闭135:8001主节点
[root@centos7-135 redis]# redis-cli -p 8001 -a 123456 shutdown save
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
#可以看到节点已经fail
[root@centos7-135 redis]# redis-cli -h 192.168.28.135 -p 8003 -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685809048000 3 connected 10923-16383
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 master - 0 1685809048000 4 connected 0-5460
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685809048896 2 connected
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685809050945 3 connected
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master,fail - 1685809014077 1685809006914 1 disconnected
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685809049917 2 connected 5461-10922
[root@centos7-135 redis]#
#查看备节点日志,可以看到132:8001 failover后升级为主节点
[root@www 8001]# tail -f redis.log
16655:S 04 Jun 2023 22:16:13.210 # Error condition on socket for SYNC: Connection refused
16655:S 04 Jun 2023 22:16:14.219 * Connecting to MASTER 192.168.28.135:8001
16655:S 04 Jun 2023 22:16:14.219 * MASTER <-> REPLICA sync started
16655:S 04 Jun 2023 22:16:14.219 # Error condition on socket for SYNC: Connection refused
16655:S 04 Jun 2023 22:16:14.605 * FAIL message received from 3ad04092aa1f5080baa6b3777d580e710cf998f8 about a32d7ada6b880161c04cd43da62b409675b934ee
16655:S 04 Jun 2023 22:16:14.623 # Start of election delayed for 722 milliseconds (rank #0, offset 168).
16655:S 04 Jun 2023 22:16:15.235 * Connecting to MASTER 192.168.28.135:8001
16655:S 04 Jun 2023 22:16:15.235 * MASTER <-> REPLICA sync started
16655:S 04 Jun 2023 22:16:15.236 # Error condition on socket for SYNC: Connection refused
16655:S 04 Jun 2023 22:16:15.437 # Starting a failover election for epoch 4.
16655:S 04 Jun 2023 22:16:15.441 # Failover election won: I'm the new master.
16655:S 04 Jun 2023 22:16:15.441 # configEpoch set to 4 after successful failover
16655:M 04 Jun 2023 22:16:15.441 * Discarding previously cached master state.
16655:M 04 Jun 2023 22:16:15.441 # Setting secondary replication ID to 015f60bc3c114af4e40e807bbc2450e23c20c250, valid up to offset: 169. New replication ID is 73eb416ef293a872de36fa226d61f9bdc3451229
#cluster nodes 确认转移后节点信息
[root@centos7-135 redis]# redis-cli -h 192.168.28.135 -p 8003 -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685809251000 3 connected 10923-16383
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 master - 0 1685809253204 4 connected 0-5460
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685809255233 2 connected
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685809252000 3 connected
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master,fail - 1685809014077 1685809006914 1 disconnected
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685809254216 2 connected 5461-10922
[root@centos7-135 redis]#
重新上线135:8002,可以看到不会重新触发选举
[root@centos7-135 redis]# sh start-redis.sh 8001
Redis Server 8001 Started
[root@centos7-135 redis]# redis-cli -h 192.168.28.135 -p 8003 -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685809298000 3 connected 10923-16383
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 master - 0 1685809304034 4 connected 0-5460
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685809302000 2 connected
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685809305046 3 connected
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 slave fbf203ffaade738b51ec2998ba74725ba09793cc 0 1685809301000 4 connected
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685809303018 2 connected 5461-10922
[root@centos7-135 redis]#
16655:M 04 Jun 2023 22:20:36.032 * Replica 192.168.28.135:8001 asks for synchronization
16655:M 04 Jun 2023 22:20:36.032 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for '5890e775f0dd4f3d95d66e7960d6507686563676', my replication IDs are '73eb416ef293a872de36fa226d61f9bdc3451229' and '015f60bc3c114af4e40e807bbc2450e23c20c250')
16655:M 04 Jun 2023 22:20:36.032 * Starting BGSAVE for SYNC with target: disk
16655:M 04 Jun 2023 22:20:36.038 * Background saving started by pid 19150
19150:C 04 Jun 2023 22:20:36.040 * DB saved on disk
19150:C 04 Jun 2023 22:20:36.040 * RDB: 0 MB of memory used by copy-on-write
16655:M 04 Jun 2023 22:20:36.049 * Background saving terminated with success
16655:M 04 Jun 2023 22:20:36.049 * Synchronization with replica 192.168.28.135:8001 succeeded
16655:M 04 Jun 2023 22:20:36.052 * Clear FAIL state for node a32d7ada6b880161c04cd43da62b409675b934ee: master without slots is reachable again.
手动平衡节点,保证下次发生主机故障时不会集群瘫痪
注意,至少需要三台机器,才允许宕一台机器,本文只是模拟部署,演示操作集群并不具备宕一台机的功能,另外实际平衡节点一般需要夜间操作。
#下线 132:8001主节点
[root@www 8001]# redis-cli -p 8001 -a 123456 shutdown save
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
#观察 135:8001节点看是否升级为主节点
[root@centos7-135 8001]# tail -f redis.log
34447:S 04 Jun 2023 22:27:12.331 # Error condition on socket for SYNC: Connection refused
34447:S 04 Jun 2023 22:27:13.343 * Connecting to MASTER 192.168.28.132:8001
34447:S 04 Jun 2023 22:27:13.343 * MASTER <-> REPLICA sync started
34447:S 04 Jun 2023 22:27:13.344 # Error condition on socket for SYNC: Connection refused
34447:S 04 Jun 2023 22:27:14.254 * Marking node fbf203ffaade738b51ec2998ba74725ba09793cc as failing (quorum reached).
34447:S 04 Jun 2023 22:27:14.355 * Connecting to MASTER 192.168.28.132:8001
34447:S 04 Jun 2023 22:27:14.356 * MASTER <-> REPLICA sync started
34447:S 04 Jun 2023 22:27:14.356 # Start of election delayed for 727 milliseconds (rank #0, offset 616).
34447:S 04 Jun 2023 22:27:14.356 # Error condition on socket for SYNC: Connection refused
34447:S 04 Jun 2023 22:27:15.167 # Starting a failover election for epoch 5.
34447:S 04 Jun 2023 22:27:15.171 # Failover election won: I'm the new master.
34447:S 04 Jun 2023 22:27:15.171 # configEpoch set to 5 after successful failover
34447:M 04 Jun 2023 22:27:15.171 * Discarding previously cached master state.
34447:M 04 Jun 2023 22:27:15.171 # Setting secondary replication ID to 73eb416ef293a872de36fa226d61f9bdc3451229, valid up to offset: 617. New replication ID is 170540e8289e48190b5043f8828859b6e5a42caa
# 确认集群信息
[root@centos7-135 8001]# redis-cli -h 192.168.28.135 -p 8003 -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685809678000 3 connected 10923-16383
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 master,fail - 1685809613978 1685809611948 4 disconnected
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685809682458 2 connected
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685809680375 3 connected
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master - 0 1685809681405 5 connected 0-5460
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685809680000 2 connected 5461-10922
#重新启动 132:8001节点
[root@www redis]# sh start-redis.sh 8001
Redis Server 8001 Started
# 确认集群信息
[root@centos7-135 8001]# redis-cli -h 192.168.28.135 -p 8003 -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685809724000 3 connected 10923-16383
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 slave a32d7ada6b880161c04cd43da62b409675b934ee 0 1685809725261 5 connected
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685809725000 2 connected
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685809727290 3 connected
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master - 0 1685809724248 5 connected 0-5460
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685809726275 2 connected 5461-10922
[root@centos7-135 8001]#
四、Redis6集群扩容
操作步骤:
首先创建redis-cluster节点(具体配置和其他节点一样),然后再把他们加入到redis集群中
#将新节点加入到集群中
redis-cli --cluster add-node new_host_ip:port exsitint_host_ip:port
#将新节点加入集群中作为后面一个节点的从节点
redis-cli --cluster add-node new_host_ip:port exsitint_host_ip:port --cluster-slave
接着迁移槽,迁移槽时要计算原节点迁移多个槽到新节点中
redis-cli --cluster reshard <host>:<port> --cluster-from <node-id> --cluster-to <node-id> --cluster-slots <number of slots> --cluster-yes
#<host>:<port>:可以指定任意一个在集群中的节点
#--cluster-from:原节点
#--cluter-to:目标节点
#--cluter-slots:要迁移的槽的数量
迁移完可以按需执行rebalance
为了操作方便,此处编写设置删除密码快速脚本
[root@www redis]# cat>set-redispass.sh
#!/bin/bash
PASS='123456'
# delete password
del_pass() {
for ip in 132 135
do
for port in seq 8001 8003
do
redis-cli -c -h 192.168.28.${ip} -p $port -a $PASS config set masterauth ""
redis-cli -c -h 192.168.28.${ip} -p $port -a $PASS config set requirepass ""
done
echo "$ip delete password"
done
}
add_pass() {
for ip in 132 135
do
for port in seq 8001 8003
do
redis-cli -c -h 192.168.28.${ip} -p $port config set masterauth "$PASS"
redis-cli -c -h 192.168.28.${ip} -p $port config set requirepass "$PASS"
done
echo "$ip add password"
done
}
env=$1
if [[ ${env} == "del" ]];then
echo "del redis password"
del_pass
elif [[ ${env} == "add" ]];then
add_pass
else
echo "${env} not add || del "
echo ' exit ..'
exit
fi
[root@www redis]# chmod +x set-redispass.sh
1、生成Redis节点
[root@www redis]# sh genredis.sh 8004 8006
[root@www redis]# sh genredis.sh 8004 8006
[root@www redis]# sh set-redispass.sh del
[root@centos7-135 redis]# sh genredis.sh 8004 8006
[root@centos7-135 8001]# redis-cli -h 192.168.28.135 -p 8003 cluster nodes
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685812334000 3 connected 10923-16383
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 slave a32d7ada6b880161c04cd43da62b409675b934ee 0 1685812338000 5 connected
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685812341282 2 connected
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685812340267 3 connected
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master - 0 1685812338237 5 connected 0-5460
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685812339248 2 connected 5461-10922
[root@centos7-135 8001]#
2、启动Redis新节点
修改启动脚本通用性:vim start-redis.sh
# 启动redis节点
#!/bin/bash
. /etc/profile
#. $HOME/.profile
REDIS_HOME=/app/redis
REDIS_SERVER="$REDIS_HOME/bin/redis-server"
if [ $# -eq 1 ]
then
$REDIS_SERVER $REDIS_HOME/$1/redis.conf
echo "Redis Server $1 Started"
else
echo "Usage: $0 {8001|8002|8003}"
fi
启动新节点
[root@centos7-135 redis]# sh start-redis.sh 8004
Redis Server 8004 Started
[root@centos7-135 redis]# sh start-redis.sh 8005
Redis Server 8005 Started
[root@centos7-135 redis]# sh start-redis.sh 8006
Redis Server 8006 Started
[root@centos7-135 redis]# ps -ef|grep redis|grep -v grep
root 33299 1 0 22:14 ? 00:00:06 /app/redis/bin/redis-server 0.0.0.0:8002 [cluster]
root 33321 1 0 22:14 ? 00:00:06 /app/redis/bin/redis-server 0.0.0.0:8003 [cluster]
root 34447 1 0 22:21 ? 00:00:05 /app/redis/bin/redis-server 0.0.0.0:8001 [cluster]
root 43145 1 0 23:16 ? 00:00:00 /app/redis/bin/redis-server 0.0.0.0:8004 [cluster]
root 43168 1 0 23:16 ? 00:00:00 /app/redis/bin/redis-server 0.0.0.0:8005 [cluster]
root 43190 1 0 23:16 ? 00:00:00 /app/redis/bin/redis-server 0.0.0.0:8006 [cluster]
3、扩容添加Redis主节点
#将新节点加入到集群中
redis-cli --cluster add-node new_host_ip:port exsitint_host_ip:port
redis-cli --cluster add-node 192.168.28.135:8004 192.168.28.135:8001
redis-cli --cluster add-node 192.168.28.135:8005 192.168.28.135:8001
redis-cli --cluster add-node 192.168.28.135:8006 192.168.28.135:8001
[root@centos7-135 redis]# redis-cli --cluster add-node 192.168.28.135:8004 192.168.28.135:8001
>>> Adding node 192.168.28.135:8004 to cluster 192.168.28.135:8001
>>> Performing Cluster Check (using node 192.168.28.135:8001)
M: a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003
slots: (0 slots) slave
replicates 31b89449ab8b3109f8f56eccbb66badf9b6105bd
S: fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001
slots: (0 slots) slave
replicates a32d7ada6b880161c04cd43da62b409675b934ee
S: 18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002
slots: (0 slots) slave
replicates 3ad04092aa1f5080baa6b3777d580e710cf998f8
M: 3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.28.135:8004 to make it join the cluster.
[OK] New node added correctly.
[root@centos7-135 redis]# redis-cli --cluster add-node 192.168.28.135:8005 192.168.28.135:8001
>>> Adding node 192.168.28.135:8005 to cluster 192.168.28.135:8001
>>> Performing Cluster Check (using node 192.168.28.135:8001)
M: a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003
slots: (0 slots) slave
replicates 31b89449ab8b3109f8f56eccbb66badf9b6105bd
S: fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001
slots: (0 slots) slave
replicates a32d7ada6b880161c04cd43da62b409675b934ee
S: 18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002
slots: (0 slots) slave
replicates 3ad04092aa1f5080baa6b3777d580e710cf998f8
M: c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 192.168.28.135:8004
slots: (0 slots) master
M: 3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.28.135:8005 to make it join the cluster.
[OK] New node added correctly.
[root@centos7-135 redis]# redis-cli --cluster add-node 192.168.28.135:8006 192.168.28.135:8001
>>> Adding node 192.168.28.135:8006 to cluster 192.168.28.135:8001
>>> Performing Cluster Check (using node 192.168.28.135:8001)
M: a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003
slots: (0 slots) slave
replicates 31b89449ab8b3109f8f56eccbb66badf9b6105bd
S: fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001
slots: (0 slots) slave
replicates a32d7ada6b880161c04cd43da62b409675b934ee
M: d8b40d678d16080c7565baa1818d5d2cae597d37 192.168.28.135:8005
slots: (0 slots) master
S: 18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002
slots: (0 slots) slave
replicates 3ad04092aa1f5080baa6b3777d580e710cf998f8
M: c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 192.168.28.135:8004
slots: (0 slots) master
M: 3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.28.135:8006 to make it join the cluster.
[OK] New node added correctly.
[root@centos7-135 redis]# redis-cli -h 192.168.28.135 -p 8003 cluster nodes
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685812882000 3 connected 10923-16383
c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 192.168.28.135:8004@18004 master - 0 1685812882000 6 connected
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 slave a32d7ada6b880161c04cd43da62b409675b934ee 0 1685812882000 5 connected
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685812885402 3 connected
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685812884386 2 connected 5461-10922
d8b40d678d16080c7565baa1818d5d2cae597d37 192.168.28.135:8005@18005 master - 0 1685812883000 0 connected
324661e88f1c6cd3d6a8d7ff76624737d471a8ef 192.168.28.135:8006@18006 master - 0 1685812883473 7 connected
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685812886414 2 connected
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master - 0 1685812883370 5 connected 0-5460
4、迁移槽,迁移槽时要计算原节点迁移多个槽到新节点中
redis-cli --cluster reshard <host>:<port> --cluster-from <node-id> --cluster-to <node-id> --cluster-slots <number of slots> --cluster-yes
#<host>:<port>:可以指定任意一个在集群中的节点
#--cluster-from:原节点
#--cluter-to:目标节点
#--cluter-slots:要迁移的槽的数量
计算需要移动的slot数量,执行迁移操作
#用bc命令行直接计算操作:
[root@centos7-135 redis]# echo "16384/6 "|bc
2730
[root@centos7-135 redis]# redis-cli --cluster reshard 192.168.28.135:8001 --cluster-from a32d7ada6b880161c04cd43da62b409675b934ee --cluster-to c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 --cluster-slots 2730 --cluster-yes
#输出较多,省略
[root@centos7-135 redis]# redis-cli --cluster reshard 192.168.28.135:8002 --cluster-from 3ad04092aa1f5080baa6b3777d580e710cf998f8 --cluster-to d8b40d678d16080c7565baa1818d5d2cae597d37 --cluster-slots 2730 --cluster-yes
#输出较多,省略
[root@centos7-135 redis]# redis-cli --cluster reshard 192.168.28.135:8003 --cluster-from 31b89449ab8b3109f8f56eccbb66badf9b6105bd --cluster-to 324661e88f1c6cd3d6a8d7ff76624737d471a8ef --cluster-slots 2730 --cluster-yes
#输出较多,省略
#确认节点信息,可以看到,执行完迁移操作后,槽已经均衡分布在6个master节点上
[root@centos7-135 redis]# redis-cli -h 192.168.28.135 -p 8003 cluster nodes
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685813469000 3 connected 13653-16383
c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 192.168.28.135:8004@18004 master - 0 1685813469000 8 connected 0-2729
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 slave a32d7ada6b880161c04cd43da62b409675b934ee 0 1685813471040 5 connected
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685813469531 3 connected
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685813469027 2 connected 8191-10922
d8b40d678d16080c7565baa1818d5d2cae597d37 192.168.28.135:8005@18005 master - 0 1685813468000 9 connected 5461-8190
324661e88f1c6cd3d6a8d7ff76624737d471a8ef 192.168.28.135:8006@18006 master - 0 1685813472046 10 connected 10923-13652
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685813468021 2 connected
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master - 0 1685813480034 5 connected 2730-5460
[root@centos7-135 redis]#
5、添加从节点
#将新节点加入集群中作为后面一个节点的从节点
redis-cli --cluster add-node new_host_ip:port exsitint_host_ip:port --cluster-slave
#启动从节点
[root@www redis]# sh start-redis.sh 8004
Redis Server 8004 Started
[root@www redis]# sh start-redis.sh 8005
Redis Server 8005 Started
[root@www redis]# sh start-redis.sh 8006
Redis Server 8006 Started
#确认进程
[root@www redis]# ps -ef|grep redis|grep -v grep
root 16676 1 0 22:13 ? 00:00:12 /app/redis/bin/redis-server 0.0.0.0:8002 [cluster]
root 16697 1 0 22:13 ? 00:00:12 /app/redis/bin/redis-server 0.0.0.0:8003 [cluster]
root 21408 1 0 22:27 ? 00:00:10 /app/redis/bin/redis-server 0.0.0.0:8001 [cluster]
root 42527 1 0 23:33 ? 00:00:00 /app/redis/bin/redis-server 0.0.0.0:8004 [cluster]
root 42572 1 0 23:33 ? 00:00:00 /app/redis/bin/redis-server 0.0.0.0:8005 [cluster]
root 42617 1 0 23:33 ? 00:00:00 /app/redis/bin/redis-server 0.0.0.0:8006 [cluster]
#添加新的节点为新加入主节点的从节点
[root@centos7-135 redis]# redis-cli --cluster add-node 192.168.28.132:8004 192.168.28.135:8004 --cluster-slave --cluster-master-id c1def88f5bd5aaf3f5d67082c5a3671f04a073f0
[root@centos7-135 redis]# redis-cli --cluster add-node 192.168.28.132:8005 192.168.28.135:8005 --cluster-slave --cluster-master-id d8b40d678d16080c7565baa1818d5d2cae597d37
[root@centos7-135 redis]# redis-cli --cluster add-node 192.168.28.132:8006 192.168.28.135:8006 --cluster-slave --cluster-master-id 324661e88f1c6cd3d6a8d7ff76624737d471a8ef
#确认节点信息
[root@centos7-135 redis]# redis-cli -h 192.168.28.135 -p 8003 cluster nodes
a9d49b32740578e437e8c00faffbac3d9b57a83a 192.168.28.132:8005@18005 slave d8b40d678d16080c7565baa1818d5d2cae597d37 0 1685813878000 9 connected
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685813878000 3 connected 13653-16383
c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 192.168.28.135:8004@18004 master - 0 1685813875000 8 connected 0-2729
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 slave a32d7ada6b880161c04cd43da62b409675b934ee 0 1685813876306 5 connected
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685813875000 3 connected
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685813879338 2 connected 8191-10922
d8b40d678d16080c7565baa1818d5d2cae597d37 192.168.28.135:8005@18005 master - 0 1685813872246 9 connected 5461-8190
324661e88f1c6cd3d6a8d7ff76624737d471a8ef 192.168.28.135:8006@18006 master - 0 1685813875289 10 connected 10923-13652
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685813878329 2 connected
31a7199a3ff4a4d51ea302d3f05c8738c5e35202 192.168.28.132:8006@18006 slave 324661e88f1c6cd3d6a8d7ff76624737d471a8ef 0 1685813874000 10 connected
0c8a46d57359f57640c8c7fad874b25b4e22731a 192.168.28.132:8004@18004 slave c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 0 1685813873566 8 connected
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master - 0 1685813873566 5 connected 2730-5460
#查看生成的node配置
[root@centos7-135 8001]# cat nodes.conf
0c8a46d57359f57640c8c7fad874b25b4e22731a 192.168.28.132:8004@18004 slave c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 0 1685813865249 8 connected
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 slave a32d7ada6b880161c04cd43da62b409675b934ee 0 1685813869301 5 connected
d8b40d678d16080c7565baa1818d5d2cae597d37 192.168.28.135:8005@18005 master - 0 1685813862000 9 connected 5461-8190
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 myself,master - 0 1685813861000 5 connected 2730-5460
324661e88f1c6cd3d6a8d7ff76624737d471a8ef 192.168.28.135:8006@18006 master - 0 1685813864235 10 connected 10923-13652
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685813863223 2 connected 8191-10922
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685813868286 3 connected
a9d49b32740578e437e8c00faffbac3d9b57a83a 192.168.28.132:8005@18005 slave d8b40d678d16080c7565baa1818d5d2cae597d37 0 1685813864000 9 connected
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685813867278 2 connected
31a7199a3ff4a4d51ea302d3f05c8738c5e35202 192.168.28.132:8006@18006 slave 324661e88f1c6cd3d6a8d7ff76624737d471a8ef 0 1685813869402 10 connected
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 master - 0 1685813866264 3 connected 13653-16383
c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 192.168.28.135:8004@18004 master - 0 1685813863000 8 connected 0-2729
vars currentEpoch 10 lastVoteEpoch 0
[root@centos7-135 8001]#
五、集群缩容
移除slave可以直接移除,不涉及hash槽
移除master相对麻烦,因为主节点的里面是有分配了hash槽的,所以必须先把hash槽放入到其他的可用主节点中去,然后再进行移除节点操作,不然会出现数据丢失问题
最好将要下线的master的slots数量平均分配至其他master上,所以只能一次分配部分solts槽,有几个master就要分配几次。
假设要将上面新增的 192.168.28.135 和132 的8006 两个实例从集群中移除,具体操作如下
1、移除slave
# 查到你要移除的 slave的 节点ID
[root@centos7-135 8001]# redis-cli -h 192.168.28.135 -p 8003 cluster nodes
a9d49b32740578e437e8c00faffbac3d9b57a83a 192.168.28.132:8005@18005 slave d8b40d678d16080c7565baa1818d5d2cae597d37 0 1685814196026 9 connected
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685814191000 3 connected 13653-16383
c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 192.168.28.135:8004@18004 master - 0 1685814194502 8 connected 0-2729
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 slave a32d7ada6b880161c04cd43da62b409675b934ee 0 1685814198552 5 connected
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685814197541 3 connected
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685814195521 2 connected 8191-10922
d8b40d678d16080c7565baa1818d5d2cae597d37 192.168.28.135:8005@18005 master - 0 1685814195000 9 connected 5461-8190
324661e88f1c6cd3d6a8d7ff76624737d471a8ef 192.168.28.135:8006@18006 master - 0 1685814192590 10 connected 10923-13652
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685814196530 2 connected
31a7199a3ff4a4d51ea302d3f05c8738c5e35202 192.168.28.132:8006@18006 slave 324661e88f1c6cd3d6a8d7ff76624737d471a8ef 0 1685814197033 10 connected
0c8a46d57359f57640c8c7fad874b25b4e22731a 192.168.28.132:8004@18004 slave c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 0 1685814193000 8 connected
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master - 0 1685814195000 5 connected 2730-5460
[root@centos7-135 8001]#
# 这次需要移除的slaveID为: 31a7199a3ff4a4d51ea302d3f05c8738c5e35202
[root@centos7-135 8001]# redis-cli --cluster del-node 192.168.28.135:8001 31a7199a3ff4a4d51ea302d3f05c8738c5e35202
>>> Removing node 31a7199a3ff4a4d51ea302d3f05c8738c5e35202 from cluster 192.168.28.135:8001
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
#查看节点信息进行确认,可以看到已经移除
[root@centos7-135 8001]# redis-cli -h 192.168.28.135 -p 8003 cluster nodes|grep 8006
324661e88f1c6cd3d6a8d7ff76624737d471a8ef 192.168.28.135:8006@18006 master - 0 1685814274407 10 connected 10923-13652
[root@centos7-135 8001]#
2、移除master节点
因为主节点的里面是分配了hash槽,所以必须先把8006里的hash槽放入到其他的可用主节点中去,然后再进行移除节点操作,否则会出现数据丢失问题
最好将要下线的master的slots数量平均分配至其他master上,所以只能一次分配部分solts槽,有几个master就要分配几次):
由于节点 31a7199a3ff4a4d51ea302d3f05c8738c5e35202有2730个slot,那么5个master节点平均可以分配546个
#计算每个主节点分配的槽
[root@centos7-135 8001]# echo "2730/5 "|bc
546
[root@centos7-135 8001]# redis-cli -h 192.168.28.135 -p 8003 cluster nodes|grep master
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685814478000 3 connected 13653-16383
c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 192.168.28.135:8004@18004 master - 0 1685814481000 8 connected 0-2729
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685814480000 2 connected 8191-10922
d8b40d678d16080c7565baa1818d5d2cae597d37 192.168.28.135:8005@18005 master - 0 1685814483515 9 connected 5461-8190
324661e88f1c6cd3d6a8d7ff76624737d471a8ef 192.168.28.135:8006@18006 master - 0 1685814483515 10 connected 10923-13652
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master - 0 1685814482566 5 connected 2730-5460
#重新分配槽
[root@centos7-135 8001]# redis-cli --cluster reshard 192.168.28.135:8006 --cluster-from 324661e88f1c6cd3d6a8d7ff76624737d471a8ef --cluster-to a32d7ada6b880161c04cd43da62b409675b934ee --cluster-slots 546 --cluster-yes
#输出较多,略
[root@centos7-135 8001]# redis-cli --cluster reshard 192.168.28.135:8006 --cluster-from 324661e88f1c6cd3d6a8d7ff76624737d471a8ef --cluster-to 3ad04092aa1f5080baa6b3777d580e710cf998f8 --cluster-slots 546 --cluster-yes
[root@centos7-135 8001]# redis-cli --cluster reshard 192.168.28.135:8006 --cluster-from 324661e88f1c6cd3d6a8d7ff76624737d471a8ef --cluster-to 31b89449ab8b3109f8f56eccbb66badf9b6105bd --cluster-slots 546 --cluster-yes
[root@centos7-135 8001]# redis-cli --cluster reshard 192.168.28.135:8006 --cluster-from 324661e88f1c6cd3d6a8d7ff76624737d471a8ef --cluster-to c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 --cluster-slots 546 --cluster-yes
[root@centos7-135 8001]# redis-cli --cluster reshard 192.168.28.135:8006 --cluster-from 324661e88f1c6cd3d6a8d7ff76624737d471a8ef --cluster-to d8b40d678d16080c7565baa1818d5d2cae597d37 --cluster-slots 546 --cluster-yes
#确认节点信息,可以看到,槽已经重新分配完成。
[root@centos7-135 8001]# redis-cli -h 192.168.28.135 -p 8003 cluster nodes|grep master
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685814622000 13 connected 12015-12560 13653-16383
c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 192.168.28.135:8004@18004 master - 0 1685814631000 14 connected 0-2729 12561-13106
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685814631000 12 connected 8191-10922 11469-12014
d8b40d678d16080c7565baa1818d5d2cae597d37 192.168.28.135:8005@18005 master - 0 1685814632485 15 connected 5461-8190 13107-13652
324661e88f1c6cd3d6a8d7ff76624737d471a8ef 192.168.28.135:8006@18006 master - 0 1685814629463 10 connected
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master - 0 1685814626439 11 connected 2730-5460 10923-11468
# 移除要下线的主机点,redis进程会自动关闭
[root@centos7-135 8001]# redis-cli --cluster del-node 192.168.28.135:8001 324661e88f1c6cd3d6a8d7ff76624737d471a8ef
[root@centos7-135 8001]# redis-cli --cluster del-node 192.168.28.135:8001 324661e88f1c6cd3d6a8d7ff76624737d471a8ef
>>> Removing node 324661e88f1c6cd3d6a8d7ff76624737d471a8ef from cluster 192.168.28.135:8001
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
[root@centos7-135 8001]# redis-cli -h 192.168.28.135 -p 8003 cluster nodes|grep master
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685814691000 13 connected 12015-12560 13653-16383
c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 192.168.28.135:8004@18004 master - 0 1685814704000 14 connected 0-2729 12561-13106
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685814707360 12 connected 8191-10922 11469-12014
d8b40d678d16080c7565baa1818d5d2cae597d37 192.168.28.135:8005@18005 master - 0 1685814705333 15 connected 5461-8190 13107-13652
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master - 0 1685814706344 11 connected 2730-5460 10923-11468
[root@centos7-135 8001]#
[root@centos7-135 8001]#
#检查集群
[root@centos7-135 8001]# redis-cli --cluster check 192.168.28.135:8001
192.168.28.135:8001 (a32d7ada...) -> 0 keys | 3277 slots | 1 slaves.
192.168.28.135:8005 (d8b40d67...) -> 0 keys | 3276 slots | 1 slaves.
192.168.28.135:8002 (3ad04092...) -> 0 keys | 3278 slots | 1 slaves.
192.168.28.135:8003 (31b89449...) -> 0 keys | 3277 slots | 1 slaves.
192.168.28.135:8004 (c1def88f...) -> 0 keys | 3276 slots | 1 slaves.
[OK] 0 keys in 5 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.28.135:8001)
M: a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001
slots:[2730-5460],[10923-11468] (3277 slots) master
1 additional replica(s)
S: 0c8a46d57359f57640c8c7fad874b25b4e22731a 192.168.28.132:8004
slots: (0 slots) slave
replicates c1def88f5bd5aaf3f5d67082c5a3671f04a073f0
S: fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001
slots: (0 slots) slave
replicates a32d7ada6b880161c04cd43da62b409675b934ee
M: d8b40d678d16080c7565baa1818d5d2cae597d37 192.168.28.135:8005
slots:[5461-8190],[13107-13652] (3276 slots) master
1 additional replica(s)
M: 3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002
slots:[8191-10922],[11469-12014] (3278 slots) master
1 additional replica(s)
S: b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003
slots: (0 slots) slave
replicates 31b89449ab8b3109f8f56eccbb66badf9b6105bd
S: a9d49b32740578e437e8c00faffbac3d9b57a83a 192.168.28.132:8005
slots: (0 slots) slave
replicates d8b40d678d16080c7565baa1818d5d2cae597d37
S: 18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002
slots: (0 slots) slave
replicates 3ad04092aa1f5080baa6b3777d580e710cf998f8
M: 31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003
slots:[12015-12560],[13653-16383] (3277 slots) master
1 additional replica(s)
M: c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 192.168.28.135:8004
slots:[0-2729],[12561-13106] (3276 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@centos7-135 8001]#
3、执行rebalance确认,并检查缩容后的集群节点是否正常
[root@centos7-135 8001]# redis-cli --cluster rebalance 192.168.28.135:8001
>>> Performing Cluster Check (using node 192.168.28.135:8001)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** No rebalancing needed! All nodes are within the 2.00% threshold.
#可以看到,集群已经缩容为5主5从的新集群
[root@centos7-135 8001]# redis-cli -h 192.168.28.135 -p 8003 cluster nodes
a9d49b32740578e437e8c00faffbac3d9b57a83a 192.168.28.132:8005@18005 slave d8b40d678d16080c7565baa1818d5d2cae597d37 0 1685815069927 15 connected
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 myself,master - 0 1685815071000 13 connected 12015-12560 13653-16383
c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 192.168.28.135:8004@18004 master - 0 1685815069000 14 connected 0-2729 12561-13106
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 slave a32d7ada6b880161c04cd43da62b409675b934ee 0 1685815080000 11 connected
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685815073574 13 connected
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685815074587 12 connected 8191-10922 11469-12014
d8b40d678d16080c7565baa1818d5d2cae597d37 192.168.28.135:8005@18005 master - 0 1685815072559 15 connected 5461-8190 13107-13652
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685815071000 12 connected
0c8a46d57359f57640c8c7fad874b25b4e22731a 192.168.28.132:8004@18004 slave c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 0 1685815070534 14 connected
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 master - 0 1685815071546 11 connected 2730-5460 10923-11468
[root@centos7-135 8001]# cat nodes.conf
0c8a46d57359f57640c8c7fad874b25b4e22731a 192.168.28.132:8004@18004 slave c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 0 1685814701000 14 connected
fbf203ffaade738b51ec2998ba74725ba09793cc 192.168.28.132:8001@18001 slave a32d7ada6b880161c04cd43da62b409675b934ee 0 1685814703201 11 connected
d8b40d678d16080c7565baa1818d5d2cae597d37 192.168.28.135:8005@18005 master - 0 1685814704000 15 connected 5461-8190 13107-13652
a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001@18001 myself,master - 0 1685814800000 11 connected 2730-5460 10923-11468
3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002@18002 master - 0 1685814703000 12 connected 8191-10922 11469-12014
b7b199561d0079883402b8aa0553440e60983dc2 192.168.28.132:8003@18003 slave 31b89449ab8b3109f8f56eccbb66badf9b6105bd 0 1685814701174 13 connected
a9d49b32740578e437e8c00faffbac3d9b57a83a 192.168.28.132:8005@18005 slave d8b40d678d16080c7565baa1818d5d2cae597d37 0 1685814705228 15 connected
18f533eaa202ad418a4155e940b754249df3d638 192.168.28.132:8002@18002 slave 3ad04092aa1f5080baa6b3777d580e710cf998f8 0 1685814701000 12 connected
31b89449ab8b3109f8f56eccbb66badf9b6105bd 192.168.28.135:8003@18003 master - 0 1685814702589 13 connected 12015-12560 13653-16383
c1def88f5bd5aaf3f5d67082c5a3671f04a073f0 192.168.28.135:8004@18004 master - 0 1685814702000 14 connected 0-2729 12561-13106
vars currentEpoch 15 lastVoteEpoch 0
[root@centos7-135 8001]#
附:文章来源:https://www.toymoban.com/news/detail-490221.html
stop-redis.sh脚本文章来源地址https://www.toymoban.com/news/detail-490221.html
#!/bin/bash
#description: stop redis‐server after rdb save
. /etc/profile
#. $HOME/.profile
REDIS_HOME=/app/redis
REDIS_CLI="$REDIS_HOME/bin/redis-cli"
REDIS_PASS="123456"
if [ $# -eq 1 ]
then
$REDIS_CLI -p $1 -a "$REDIS_PASS" shutdown save
echo "Redis Server $1 will be stopped after background save finished"
else
echo "Usage: $0 {8001|8002|8003}"
fi
到了这里,关于Redis6集群安装及其扩容缩容的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!