Redis6集群安装及其扩容缩容

这篇具有很好参考价值的文章主要介绍了Redis6集群安装及其扩容缩容。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

虚拟机拷贝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]# 

附:

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模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • CentOS 7安装Redis6

    若系统没有安装 wget ,需要先安装 wget 从官网下载redis 将下载的文件解压至 /usr/local redis编译需要依赖 gcc ,安装 gcc 检查 gcc 版本,若 gcc 版本过低5.3以下,则无法编译 redis6 升级到 gcc 9 若没有安装 tcl ,还需安装 tcl ,或者下载压缩包编译安装 切换到解压后的redis目录,开始

    2024年02月10日
    浏览(38)
  • Centos下安装Redis6.X

             如果提示找不到gcc程序,说明没有安装,可以用dnf命令安装:         准备好gcc,可接下来进行下载安装。         下载后执行解压:         PREFIX=/usr/local/soft/redis6:用来指定安装目录,这里我们指定安装到 /usr/local/soft/redis6

    2024年03月10日
    浏览(32)
  • Docker进阶:mysql 主从复制、redis集群3主3从【扩缩容案例】

    💖The Begin💖点点关注,收藏不迷路💖 首先,确保你已经安装了Docker。 要下载特定版本的Tomcat镜像,可以在 docker pull 命令后面添加 image_name: tag 参数。其中 tag 制定了镜像的版本号。 解决插入中文报错: 总结: docker安装完mysql,运行实例之后,建议先修改完字符集编码后再

    2024年02月10日
    浏览(39)
  • linux centos8下安装redis6.2.12

    Download | Redis  解压操作  tar -zxvf redis-6.2.12.tar.gz cd redis-6.2.12   ctrl+c 就可以有输入命令的地方  里面有个redis.conf文件复制到opt/redis/bin中去   vim redis.conf 进入编辑模式  将bind 127.0.0.1注释   改成yes 密码      

    2024年02月12日
    浏览(56)
  • Linux-安装redis6.2.1及主备复制模式(replication)

    地址==》https://redis.io/download/ 例:/data/replication/ 目录redis-6.2.1修改为7000-master 再copy一份为从节点备份,文件夹命名为7001-slave 分别进入文件夹7000-master和7001-slave,执行命令 编译后,会在/data/replication/7000-master/src/和目录/data/replication/7001-slave/src/下生成执行文件,如下 修改主节

    2024年02月10日
    浏览(30)
  • 分布式项目08 redis的扩容,预备redis宕机的哨兵处理 和 最后集大成redis集群

    01.redis扩容 由于redis的容量也是有限的,所以,就会有扩容的操作。也就好像内存中扩容一样。redis有分片这种说法,具体实现操作如下: 第一步:首先在/usr/local/src中去找到redis文件夹。cd /usr/local/src 而后进入redis文件夹。使用的linux命令是 cd redis 。 第二步: 01.使用查看当前

    2024年02月05日
    浏览(40)
  • Redis分片集群搭建及其原理

    主从和哨兵可以解决高可用、高并发读的问题。但是依然有两个问题没有解决: 海量数据存储问题 高并发写的问题 使用分片集群可以解决上述问题,如图: 分片集群特征: 集群中有多个master,每个master保存不同数据 每个master都可以有多个slave节点 master之间通过ping监测彼此

    2024年02月05日
    浏览(38)
  • 银河麒麟服务器v10 sp2 LVM分区扩容和缩容

    一、LVM分区扩容 首先查看下硬盘情况,原先是30G,现在变成40G df -h查看/目录只有27G,lsblk查看sda2也只有29g 新建分区 fdisk  /dev/sda 命令(输入 m 获取帮助):n  然后一路回车 命令(输入 m 获取帮助):w   回车 然后有一个新增的/dev/sda3出来       找到 / 对应的LV Path对应的路径

    2024年01月24日
    浏览(95)
  • k8s基础4——deployment控制器、应用部署、升级、回滚、水平扩容缩容

    基本了解: Deployment是最常用的K8s工作负载控制器(Workload Controllers),实际项目部署调试中必用资源之一,所以必须要熟练掌握deploy资源的使用。 它是K8s的一个抽象概念,用于更高级层次对象,部署和管理Pod。 其他控制器还有DaemonSet、StatefulSet等,不同控制器针对不同的需

    2024年02月03日
    浏览(56)
  • 4.Docker 搭建 redis6

    1.下载redis 2.创建需要挂载的宿主机文件夹 3.配置redis 切换到/data/redis/conf文件夹下,创建redis.conf,复制redis.conf配置文件内容到redis.conf文件中,然后按下键盘 esc 按键,退出编辑界面;再同时按下 shift + : ,再输入wq,保存并退出文件界面即可 redis.conf里的部分配置说明: requirep

    2024年02月07日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包