Redis 集群安装-Centos

导读:本篇文章讲解 Redis 集群安装-Centos,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Redis 集群安装-Centos

Redis3.0以后的版本虽然有了集群功能,提供了比之前版本的哨兵模式更高的性能与可用性,但是集群的水平扩展却比较麻烦,今天就来带大家看看redis高可用集群如何做水平扩展,原始集群(见下图)由6个节点组成,6个节点分布在三台机器上,采用三主三从的模式

Redis 集群安装-Centos

Redis Cluster要求至少需要3个master才能组成一个集群,同时每个master至少需要有一个slave节点。各个节点之间保持TCP通信。当master发生了宕机, Redis Cluster自动会将对应的slave节点提拔为master,来重新对外提供服务。
Redis Cluster 功能 : 负载均衡,故障切换,主从复制。

依然使用一台机器演示部署

下载安装Redis

$ wget http://download.redis.io/releases/redis-5.0.4.tar.gz
$ tar -xzvf redis-5.0.4.tar.gz -C /usr/local  
$ cd redis-5.0.4
# 官网示例是使用 `make`,它会将其安装到当前目录的src目录下。
# 将`make`替换成`make install PREFIX=/usr/local/redis`,将其安装到指定目录,记得将配置文件进行拷贝过去
$ make install PREFIX=/usr/local/redis

因为Redis是使用C语言编写的,所以需要提前安装gcc:yum install gcc -y

创建目录

因为在一台机器上,所以创建6个文件夹来做隔离。
master:8001,8002,8003
slave:8004,8005,8006

$ mkdir -p /usr/local/redis-cluster/8001 /usr/local/redis-cluster/8002 /usr/local/redis-cluster/8003 /usr/local/redis-cluster/8004 /usr/local/redis-cluster/8005 /usr/local/redis-cluster/8006

创建配置文件

8001

$ vim /usr/local/redis-cluster/8001/redis.conf

配置文件内容如下:

port 8001
daemonize yes
protected-mode no
pidfile /usr/local/redis-cluster/8001/redis.pid
logfile /usr/local/redis-cluster/8001/redis.log
requirepass soft01
masterauth soft01
databases 16
timeout 300

# rdb aof
save 900 1
save 300 10
save 60 10000
dir /usr/local/redis-cluster/8001
dbfilename dump.rdb
appendonly yes
appendfilename appendonly.aof
appendfsync everysec
auto-AOF-rewrite-percentage 100
auto-AOF-rewrite-min-size 64mb
aof-use-rdb-preamble yes

# cluster
# 启动集群
cluster-enabled yes
# 生成的集群节点配置文件名
cluster-config-file nodes-8001.conf
# 集群节点回应最长时间,超过该时间被认为下线
cluster-node-timeout 10000

8002

$ vim /usr/local/redis-cluster/8002/redis.conf

配置文件内容如下:

port 8002
daemonize yes
protected-mode no
pidfile /usr/local/redis-cluster/8002/redis.pid
logfile /usr/local/redis-cluster/8002/redis.log
requirepass soft01
masterauth soft01
databases 16
timeout 300

# rdb aof
save 900 1
save 300 10
save 60 10000
dir /usr/local/redis-cluster/8002
dbfilename dump.rdb
appendonly yes
appendfilename appendonly.aof
appendfsync everysec
auto-AOF-rewrite-percentage 100
auto-AOF-rewrite-min-size 64mb
aof-use-rdb-preamble yes

# cluster
# 启动集群
cluster-enabled yes
# 生成的集群节点配置文件名
cluster-config-file nodes-8002.conf
# 集群节点回应最长时间,超过该时间被认为下线
cluster-node-timeout 10000

8003

$ vim /usr/local/redis-cluster/8003/redis.conf

配置文件内容如下:

port 8003
daemonize yes
protected-mode no
pidfile /usr/local/redis-cluster/8003/redis.pid
logfile /usr/local/redis-cluster/8003/redis.log
requirepass soft01
masterauth soft01
databases 16
timeout 300

# rdb aof
save 900 1
save 300 10
save 60 10000
dir /usr/local/redis-cluster/8003
dbfilename dump.rdb
appendonly yes
appendfilename appendonly.aof
appendfsync everysec
auto-AOF-rewrite-percentage 100
auto-AOF-rewrite-min-size 64mb
aof-use-rdb-preamble yes

# cluster
# 启动集群
cluster-enabled yes
# 生成的集群节点配置文件名
cluster-config-file nodes-8003.conf
# 集群节点回应最长时间,超过该时间被认为下线
cluster-node-timeout 10000

8004

$ vim /usr/local/redis-cluster/8004/redis.conf

配置文件内容如下:

port 8004
daemonize yes
protected-mode no
pidfile /usr/local/redis-cluster/8004/redis.pid
logfile /usr/local/redis-cluster/8004/redis.log
requirepass soft01
masterauth soft01
databases 16
timeout 300

# rdb aof
save 900 1
save 300 10
save 60 10000
dir /usr/local/redis-cluster/8004
dbfilename dump.rdb
appendonly yes
appendfilename appendonly.aof
appendfsync everysec
auto-AOF-rewrite-percentage 100
auto-AOF-rewrite-min-size 64mb
aof-use-rdb-preamble yes

# cluster
# 启动集群
cluster-enabled yes
# 生成的集群节点配置文件名
cluster-config-file nodes-8004.conf
# 集群节点回应最长时间,超过该时间被认为下线
cluster-node-timeout 10000

8005

$ vim /usr/local/redis-cluster/8005/redis.conf

配置文件内容如下:

port 8005
daemonize yes
protected-mode no
pidfile /usr/local/redis-cluster/8005/redis.pid
logfile /usr/local/redis-cluster/8005/redis.log
requirepass soft01
masterauth soft01
databases 16
timeout 300

# rdb aof
save 900 1
save 300 10
save 60 10000
dir /usr/local/redis-cluster/8005
dbfilename dump.rdb
appendonly yes
appendfilename appendonly.aof
appendfsync everysec
auto-AOF-rewrite-percentage 100
auto-AOF-rewrite-min-size 64mb
aof-use-rdb-preamble yes

# cluster
# 启动集群
cluster-enabled yes
# 生成的集群节点配置文件名
cluster-config-file nodes-8005.conf
# 集群节点回应最长时间,超过该时间被认为下线
cluster-node-timeout 10000

8006

$ vim /usr/local/redis-cluster/8006/redis.conf

配置文件内容如下:

port 8006
daemonize yes
protected-mode no
pidfile /usr/local/redis-cluster/8006/redis.pid
logfile /usr/local/redis-cluster/8006/redis.log
requirepass soft01
masterauth soft01
databases 16
timeout 300

# rdb aof
save 900 1
save 300 10
save 60 10000
dir /usr/local/redis-cluster/8006
dbfilename dump.rdb
appendonly yes
appendfilename appendonly.aof
appendfsync everysec
auto-AOF-rewrite-percentage 100
auto-AOF-rewrite-min-size 64mb
aof-use-rdb-preamble yes

# cluster
# 启动集群
cluster-enabled yes
# 生成的集群节点配置文件名
cluster-config-file nodes-8006.conf
# 集群节点回应最长时间,超过该时间被认为下线
cluster-node-timeout 10000

创建集群

如果是多台机器上运行,请关闭防火墙。

  1. 启动redis实例
$ /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8001/redis.conf 
$ /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8002/redis.conf 
$ /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8003/redis.conf 
$ /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8004/redis.conf 
$ /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8005/redis.conf 
$ /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8006/redis.conf 
  1. 查看启动状态:
$ ps -ef | grep redis
root       7769      1  0 02:15 ?        00:00:00 /usr/local/redis/bin/redis-server *:8001 [cluster]
root       8052      1  0 02:15 ?        00:00:00 /usr/local/redis/bin/redis-server *:8002 [cluster]
root       8803      1  0 02:15 ?        00:00:00 /usr/local/redis/bin/redis-server *:8003 [cluster]
root       9045      1  0 02:15 ?        00:00:00 /usr/local/redis/bin/redis-server *:8004 [cluster]
root       9626      1  0 02:15 ?        00:00:00 /usr/local/redis/bin/redis-server *:8005 [cluster]
root       9970      1  0 02:15 ?        00:00:00 /usr/local/redis/bin/redis-server *:8006 [cluster]
  1. 创建Redis集群:

    --cluster-replicas 1 指定集群中从节点个数为1,后面紧跟所有节点。

$ /usr/local/redis/bin/redis-cli -a soft01 --cluster create --cluster-replicas 1 192.168.0.138:8001 192.168.0.138:8002 192.168.0.138:8003 192.168.0.138:8004 192.168.0.138:8005 192.168.0.138:8006
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.0.138:8005 to 192.168.0.138:8001
Adding replica 192.168.0.138:8006 to 192.168.0.138:8002
Adding replica 192.168.0.138:8004 to 192.168.0.138:8003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 192.168.0.138:8001
   slots:[0-5460] (5461 slots) master
M: 1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 192.168.0.138:8002
   slots:[5461-10922] (5462 slots) master
M: bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 192.168.0.138:8003
   slots:[10923-16383] (5461 slots) master
S: 44e2ec56944a0d9760b7180d7aebbc97b1fb495f 192.168.0.138:8004
   replicates 1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1
S: 5aaf267e75753a20b9b5a43273e68accb10571f5 192.168.0.138:8005
   replicates bab2c897f42a3e05af40d9f4d8abd0ae65beb34d
S: a90d542f240e9fd9bb0ca3861463163fac475b3a 192.168.0.138:8006
   replicates 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52
# 是否使用上述进行配置
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.0.138:8001)
M: 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 192.168.0.138:8001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 192.168.0.138:8002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 5aaf267e75753a20b9b5a43273e68accb10571f5 192.168.0.138:8005
   slots: (0 slots) slave
   replicates bab2c897f42a3e05af40d9f4d8abd0ae65beb34d
S: 44e2ec56944a0d9760b7180d7aebbc97b1fb495f 192.168.0.138:8004
   slots: (0 slots) slave
   replicates 1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1
S: a90d542f240e9fd9bb0ca3861463163fac475b3a 192.168.0.138:8006
   slots: (0 slots) slave
   replicates 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52
M: bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 192.168.0.138: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.

根据上面的信息,我们可以看到8001、8002、8003分别是8006、8004、8005的主节点。3个主节点平分了slot(2^14=16384)

  1. 查看集群信息
$ /usr/local/redis/bin/redis-cli -c -p 8001 -a soft01
127.0.0.1:8001> cluster info
cluster_state:ok                      # 集群状态正常
cluster_slots_assigned:16384          # 已分配16384 slots
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6                 # 集群的节点数
cluster_size:3                        # 集群主节点数
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:379
cluster_stats_messages_pong_sent:388
cluster_stats_messages_sent:767
cluster_stats_messages_ping_received:383
cluster_stats_messages_pong_received:379
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:767
127.0.0.1:8001> cluster nodes
1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 192.168.0.138:8002@18002 master - 0 1666258121038 2 connected 5461-10922                             # 8002主节点     
5aaf267e75753a20b9b5a43273e68accb10571f5 192.168.0.138:8005@18005 slave bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 0 1666258120000 5 connected  # 8005是8003的从节点
44e2ec56944a0d9760b7180d7aebbc97b1fb495f 192.168.0.138:8004@18004 slave 1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 0 1666258119000 4 connected  # 8004是8002的从节点
a90d542f240e9fd9bb0ca3861463163fac475b3a 192.168.0.138:8006@18006 slave 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 0 1666258120000 6 connected  # 8006是8001的从节点
3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 192.168.0.138:8001@18001 myself,master - 0 1666258119000 1 connected 0-5460                          # 8001主节点
bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 192.168.0.138:8003@18003 master - 0 1666258120028 3 connected 10923-16383                            # 8003主节点

关闭集群

关闭集群需要一个一个关闭

/usr/local/redis/bin/redis-cli -c -p 8001 -a soft01 shutdown

如果关闭一个master节点后,集群內部会将它的从节点提升成主节点。当该节点再启动时,会自动变为从节点。

新增节点

向上面搭建好的集群添加节点

创建8007、8008目录

$ mkdir -p /usr/local/redis-cluster/8007 /usr/local/redis-cluster/8008

8007

编辑8007的配置文件

$ vim /usr/local/redis-cluster/8007/redis.conf

配置文件内容如下:

port 8007
daemonize yes
protected-mode no
pidfile /usr/local/redis-cluster/8007/redis.pid
logfile /usr/local/redis-cluster/8007/redis.log
requirepass soft01
masterauth soft01
databases 16
timeout 300

# rdb aof
save 900 1
save 300 10
save 60 10000
dir /usr/local/redis-cluster/8007
dbfilename dump.rdb
appendonly yes
appendfilename appendonly.aof
appendfsync everysec
auto-AOF-rewrite-percentage 100
auto-AOF-rewrite-min-size 64mb
aof-use-rdb-preamble yes

# cluster
# 启动集群
cluster-enabled yes
# 生成的集群节点配置文件名
cluster-config-file nodes-8007.conf
# 集群节点回应最长时间,超过该时间被认为下线
cluster-node-timeout 10000

8008

编辑8008的配置文件

$ vim /usr/local/redis-cluster/8008/redis.conf

配置文件内容如下:

port 8008
daemonize yes
protected-mode no
pidfile /usr/local/redis-cluster/8008/redis.pid
logfile /usr/local/redis-cluster/8008/redis.log
requirepass soft01
masterauth soft01
databases 16
timeout 300

# rdb aof
save 900 1
save 300 10
save 60 10000
dir /usr/local/redis-cluster/8008
dbfilename dump.rdb
appendonly yes
appendfilename appendonly.aof
appendfsync everysec
auto-AOF-rewrite-percentage 100
auto-AOF-rewrite-min-size 64mb
aof-use-rdb-preamble yes

# cluster
# 启动集群
cluster-enabled yes
# 生成的集群节点配置文件名
cluster-config-file nodes-8008.conf
# 集群节点回应最长时间,超过该时间被认为下线
cluster-node-timeout 10000

启动8007,8008

启动8007、8008节点

$ /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8007/redis.conf
$ /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8008/redis.conf

查看节点运行情况

$ ps -ef | grep redis
root       8052      1  0 10月20 ?      00:02:41 /usr/local/redis/bin/redis-server *:8002 [cluster]
root       8803      1  0 10月20 ?      00:02:42 /usr/local/redis/bin/redis-server *:8003 [cluster]
root       9045      1  0 10月20 ?      00:02:42 /usr/local/redis/bin/redis-server *:8004 [cluster]
root       9626      1  0 10月20 ?      00:02:44 /usr/local/redis/bin/redis-server *:8005 [cluster]
root       9970      1  0 10月20 ?      00:02:43 /usr/local/redis/bin/redis-server *:8006 [cluster]
root      91738      1  0 00:32 ?        00:00:00 /usr/local/redis/bin/redis-server *:8007 [cluster]
root      92180      1  0 00:32 ?        00:00:00 /usr/local/redis/bin/redis-server *:8008 [cluster]
root     103952      1  0 10月20 ?      00:02:42 /usr/local/redis/bin/redis-server *:8001 [cluster]

将8007,8008添加到集群

cluster 帮助文档

$ /usr/local/redis/bin/redis-cli --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN # 创建集群
                 --cluster-replicas <arg>	 #从节点个数
  check          host:port #检查集群
                 --cluster-search-multiple-owners #检查是否有槽同时被分配给了多个节点
  info           host:port #查看集群状态
  fix            host:port #修复集群
                 --cluster-search-multiple-owners #修复槽的重复分配问题
  reshard        host:port #指定集群的任意一节点进行迁移slot,重新分
                 --cluster-from <arg> #需要从哪些源节点上迁移slot,可从多个源节点完成迁移,以逗号隔开,传递的是节点的node id,还可以直接传递--from all,这样源节点就是集群的所有节点,不传递该参数的话,则会在迁移过程中提示用户输入
                 --cluster-to <arg> #slot需要迁移的目的节点的node id,目的节点只能填写一个,不传递该参数的话,则会在迁移过程中提示用户输入
                 --cluster-slots <arg>  #需要迁移的slot数量,不传递该参数的话,则会在迁移过程中提示用户输入。
                 --cluster-yes #指定迁移时的确认输入
                 --cluster-timeout <arg> #设置migrate命令的超时时间
                 --cluster-pipeline <arg> #定义cluster getkeysinslot命令一次取出的key数量,不传的话使用默认值为10
                 --cluster-replace  #是否直接replace到目标节点
  rebalance      host:port #指定集群的任意一节点进行平衡集群节点slot数量
                 --cluster-weight <node1=w1...nodeN=wN> #指定集群节点的权重
                 --cluster-use-empty-masters #设置可以让没有分配slot的主节点参与,默认不允许
                 --cluster-timeout <arg> #设置migrate命令的超时时间
                 --cluster-simulate  #模拟rebalance操作,不会真正执行迁移操作
                 --cluster-pipeline <arg>  #定义cluster getkeysinslot命令一次取出的key数量,默认值为10
                 --cluster-threshold <arg>  #迁移的slot阈值超过threshold,执行rebalance操作
                 --cluster-replace  #是否直接replace到目标节点
  add-node       new_host:new_port existing_host:existing_port  #添加节点,把新节点加入到指定的集群,默认添加主节点
                 --cluster-slave #新节点作为从节点,默认随机一个主节点
                 --cluster-master-id <arg> #给新节点指定主节点
  del-node       host:port node_id #删除给定的一个节点,成功后关闭该节点服务
  call           host:port command arg arg .. arg #在集群的所有节点执行相关命令
  set-timeout    host:port milliseconds  #设置cluster-node-timeout
  import         host:port #将外部redis数据导入集群
                 --cluster-from <arg> #将指定实例的数据导入到集群
                 --cluster-copy #migrate时指定copy
                 --cluster-replace #migrate时指定replace
  help           

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

create:创建一个集群环境host1:port1 … hostN:portN
add-node:将一个节点添加到集群里,第一个参数为新节点的ip:port,第二个参数为集群中任意一个已经存在的节点的ip:port
del-node:移除一个节点
reshard:重新分片
check:检查集群状态

8007加入集群(Master)

192.168.0.138:8007为本次添加的节点,192.168.0.138:8001 为集群中正在运行的节点

$ /usr/local/redis/bin/redis-cli -a soft01 --cluster add-node 192.168.0.138:8007 192.168.0.138:8001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.0.138:8007 to cluster 192.168.0.138:8001
>>> Performing Cluster Check (using node 192.168.0.138:8001)
M: 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 192.168.0.138:8001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 44e2ec56944a0d9760b7180d7aebbc97b1fb495f 192.168.0.138:8004
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 192.168.0.138:8002
   slots: (0 slots) slave
   replicates 44e2ec56944a0d9760b7180d7aebbc97b1fb495f
S: a90d542f240e9fd9bb0ca3861463163fac475b3a 192.168.0.138:8006
   slots: (0 slots) slave
   replicates 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52
M: 5aaf267e75753a20b9b5a43273e68accb10571f5 192.168.0.138:8005
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 192.168.0.138:8003
   slots: (0 slots) slave
   replicates 5aaf267e75753a20b9b5a43273e68accb10571f5
[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.0.138:8007 to make it join the cluster.
[OK] New node added correctly.

查看集群状态:

$ /usr/local/redis/bin/redis-cli -a soft01 -p 8001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:8001> cluster nodes
44e2ec56944a0d9760b7180d7aebbc97b1fb495f 192.168.0.138:8004@18004 master - 0 1666338116089 9 connected 5461-10922
1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 192.168.0.138:8002@18002 slave 44e2ec56944a0d9760b7180d7aebbc97b1fb495f 0 1666338118126 9 connected
a90d542f240e9fd9bb0ca3861463163fac475b3a 192.168.0.138:8006@18006 slave 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 0 1666338117111 10 connected
3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 192.168.0.138:8001@18001 myself,master - 0 1666338116000 10 connected 0-5460
5aaf267e75753a20b9b5a43273e68accb10571f5 192.168.0.138:8005@18005 master - 0 1666338117000 8 connected 10923-16383
bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 192.168.0.138:8003@18003 slave 5aaf267e75753a20b9b5a43273e68accb10571f5 0 1666338116000 8 connected
c80becfa75dd146eefdca89fa3c2a83d1a885b23 192.168.0.138:8007@18007 master - 0 1666338115000 0 connected

注意:当添加节点成功以后,新增的节点不会有任何数据,因为它还没有分配任何的slot(hash槽),我们需要为新节点手工分配hash槽

给8007分配Hash槽

$ /usr/local/redis/bin/redis-cli -a soft01 --cluster reshard 192.168.0.138:8001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.0.138:8001)
M: 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 192.168.0.138:8001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 44e2ec56944a0d9760b7180d7aebbc97b1fb495f 192.168.0.138:8004
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 192.168.0.138:8002
   slots: (0 slots) slave
   replicates 44e2ec56944a0d9760b7180d7aebbc97b1fb495f
S: a90d542f240e9fd9bb0ca3861463163fac475b3a 192.168.0.138:8006
   slots: (0 slots) slave
   replicates 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52
M: 5aaf267e75753a20b9b5a43273e68accb10571f5 192.168.0.138:8005
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 192.168.0.138:8003
   slots: (0 slots) slave
   replicates 5aaf267e75753a20b9b5a43273e68accb10571f5
M: c80becfa75dd146eefdca89fa3c2a83d1a885b23 192.168.0.138:8007
   slots: (0 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 需要多少个槽移动到新的节点上,自己设置,比如600个hash槽
How many slots do you want to move (from 1 to 16384)? 600
# 把这600个hash槽移动到哪个节点上去,需要指定节点id(8007对应的id)
What is the receiving node ID? c80becfa75dd146eefdca89fa3c2a83d1a885b23
Please enter all the source node IDs.
  # 输入'all'以使用所有节点作为散列槽的源节点。
  Type 'all' to use all the nodes as source nodes for the hash slots.
  # 输入所有源节点id,输入“done”。
  Type 'done' once you entered all the source nodes IDs.
# 输入all为从所有主节点(8001,8002,8003)中分别抽取相应的槽数指定到新节点中,抽取的总槽数为600个  
Source node #1: all

# 输入yes 开始重新分片
Do you want to proceed with the proposed reshard plan (yes/no)? yes

查看集群状态:

$ /usr/local/redis/bin/redis-cli -a soft01 -p 8001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:8001> cluster nodes
44e2ec56944a0d9760b7180d7aebbc97b1fb495f 192.168.0.138:8004@18004 master - 0 1666338849283 9 connected 5662-10922
1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 192.168.0.138:8002@18002 slave 44e2ec56944a0d9760b7180d7aebbc97b1fb495f 0 1666338850293 9 connected
a90d542f240e9fd9bb0ca3861463163fac475b3a 192.168.0.138:8006@18006 slave 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 0 1666338851307 10 connected
3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 192.168.0.138:8001@18001 myself,master - 0 1666338849000 10 connected 199-5460
5aaf267e75753a20b9b5a43273e68accb10571f5 192.168.0.138:8005@18005 master - 0 1666338849000 8 connected 11122-16383
bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 192.168.0.138:8003@18003 slave 5aaf267e75753a20b9b5a43273e68accb10571f5 0 1666338848000 8 connected
c80becfa75dd146eefdca89fa3c2a83d1a885b23 192.168.0.138:8007@18007 master - 0 1666338850000 11 connected 0-198 5461-5661 10923-11121

现在我们的8007已经有hash槽了,也就是说可以在8007上进行读写数据啦!到此为止我们的8007已经加入到集群中,并且是主节点(Master)

将8008添加到集群(Slave)

这一步和8007是差不多的。

$ /usr/local/redis/bin/redis-cli -a soft01 --cluster add-node 192.168.0.138:8008 192.168.0.138:8001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.0.138:8008 to cluster 192.168.0.138:8001
>>> Performing Cluster Check (using node 192.168.0.138:8001)
M: 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 192.168.0.138:8001
   slots:[199-5460] (5262 slots) master
   1 additional replica(s)
M: 44e2ec56944a0d9760b7180d7aebbc97b1fb495f 192.168.0.138:8004
   slots:[5662-10922] (5261 slots) master
   1 additional replica(s)
S: 1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 192.168.0.138:8002
   slots: (0 slots) slave
   replicates 44e2ec56944a0d9760b7180d7aebbc97b1fb495f
S: a90d542f240e9fd9bb0ca3861463163fac475b3a 192.168.0.138:8006
   slots: (0 slots) slave
   replicates 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52
M: 5aaf267e75753a20b9b5a43273e68accb10571f5 192.168.0.138:8005
   slots:[11122-16383] (5262 slots) master
   1 additional replica(s)
S: bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 192.168.0.138:8003
   slots: (0 slots) slave
   replicates 5aaf267e75753a20b9b5a43273e68accb10571f5
M: c80becfa75dd146eefdca89fa3c2a83d1a885b23 192.168.0.138:8007
   slots:[0-198],[5461-5661],[10923-11121] (599 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.0.138:8008 to make it join the cluster.
[OK] New node added correctly.
[root@192 ~]# 

查看集群状态:

$ /usr/local/redis/bin/redis-cli -a soft01 -p 8001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:8001> cluster nodes
44e2ec56944a0d9760b7180d7aebbc97b1fb495f 192.168.0.138:8004@18004 master - 0 1666339203015 9 connected 5662-10922
1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 192.168.0.138:8002@18002 slave 44e2ec56944a0d9760b7180d7aebbc97b1fb495f 0 1666339203000 9 connected
a90d542f240e9fd9bb0ca3861463163fac475b3a 192.168.0.138:8006@18006 slave 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 0 1666339204059 10 connected
3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 192.168.0.138:8001@18001 myself,master - 0 1666339202000 10 connected 199-5460
9993aa805467a740fe40c76467f24e40cc84be4a 192.168.0.138:8008@18008 master - 0 1666339202000 0 connected
5aaf267e75753a20b9b5a43273e68accb10571f5 192.168.0.138:8005@18005 master - 0 1666339202000 8 connected 11122-16383
bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 192.168.0.138:8003@18003 slave 5aaf267e75753a20b9b5a43273e68accb10571f5 0 1666339202000 8 connected
c80becfa75dd146eefdca89fa3c2a83d1a885b23 192.168.0.138:8007@18007 master - 0 1666339205096 11 connected 0-198 5461-5661 10923-11121

注意:此时的8008也是master节点,我们后面需要将其修改成从节点

我们需要执行replicate命令来指定当前节点(从节点)的主节点id为哪个,首先需要连接新加的8008节点的客户端,然后使用集群命令进行操作,把当前的8008(slave)节点指定到一个主节点下(这里使用之前创建的8007主节点)

$ /usr/local/redis/bin/redis-cli -a soft01 -c -p 8008
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# 8007的id是c80becfa75dd146eefdca89fa3c2a83d1a885b23
127.0.0.1:8008> cluster replicate c80becfa75dd146eefdca89fa3c2a83d1a885b23
OK
127.0.0.1:8008> cluster nodes
a90d542f240e9fd9bb0ca3861463163fac475b3a 192.168.0.138:8006@18006 slave 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 0 1666339377426 10 connected
bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 192.168.0.138:8003@18003 slave 5aaf267e75753a20b9b5a43273e68accb10571f5 0 1666339376000 8 connected
c80becfa75dd146eefdca89fa3c2a83d1a885b23 192.168.0.138:8007@18007 master - 0 1666339378434 11 connected 0-198 5461-5661 10923-11121
44e2ec56944a0d9760b7180d7aebbc97b1fb495f 192.168.0.138:8004@18004 master - 0 1666339376011 9 connected 5662-10922
1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 192.168.0.138:8002@18002 slave 44e2ec56944a0d9760b7180d7aebbc97b1fb495f 0 1666339376000 9 connected
5aaf267e75753a20b9b5a43273e68accb10571f5 192.168.0.138:8005@18005 master - 0 1666339379444 8 connected 11122-16383
9993aa805467a740fe40c76467f24e40cc84be4a 192.168.0.138:8008@18008 myself,slave c80becfa75dd146eefdca89fa3c2a83d1a885b23 0 1666339375000 0 connected
3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 192.168.0.138:8001@18001 master - 0 1666339377000 10 connected 199-5460

可以看到,8008变成了从节点,它的master节点是8007

删除节点

删除从节点

del-node删除从节点8008

192.168.0.138:8008 被删除的节点 ip:port

9993aa805467a740fe40c76467f24e40cc84be4a 被删除节点的id

$ /usr/local/redis/bin/redis-cli -a soft01 --cluster del-node 192.168.0.138:8008 9993aa805467a740fe40c76467f24e40cc84be4a
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 9993aa805467a740fe40c76467f24e40cc84be4a from cluster 192.168.0.138:8008
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

再次查看集群状态,如下所示,8008这个slave节点已经移除,并且该节点的redis服务也已被停止

$ /usr/local/redis/bin/redis-cli -a soft01 -p 8001
127.0.0.1:8001> cluster nodes
44e2ec56944a0d9760b7180d7aebbc97b1fb495f 192.168.0.138:8004@18004 master - 0 1666340569144 9 connected 5662-10922
1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 192.168.0.138:8002@18002 slave 44e2ec56944a0d9760b7180d7aebbc97b1fb495f 0 1666340567000 9 connected
a90d542f240e9fd9bb0ca3861463163fac475b3a 192.168.0.138:8006@18006 slave 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 0 1666340568136 10 connected
3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 192.168.0.138:8001@18001 myself,master - 0 1666340567000 10 connected 199-5460
5aaf267e75753a20b9b5a43273e68accb10571f5 192.168.0.138:8005@18005 master - 0 1666340567000 8 connected 11122-16383
bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 192.168.0.138:8003@18003 slave 5aaf267e75753a20b9b5a43273e68accb10571f5 0 1666340567128 8 connected
c80becfa75dd146eefdca89fa3c2a83d1a885b23 192.168.0.138:8007@18007 master - 0 1666340567128 11 connected 0-198 5461-5661 10923-11121

$ [root@192 ~]# ps -ef | grep redis
root       8052      1  0 10月20 ?      00:02:48 /usr/local/redis/bin/redis-server *:8002 [cluster]
root       8803      1  0 10月20 ?      00:02:49 /usr/local/redis/bin/redis-server *:8003 [cluster]
root       9045      1  0 10月20 ?      00:02:49 /usr/local/redis/bin/redis-server *:8004 [cluster]
root       9626      1  0 10月20 ?      00:02:51 /usr/local/redis/bin/redis-server *:8005 [cluster]
root       9970      1  0 10月20 ?      00:02:50 /usr/local/redis/bin/redis-server *:8006 [cluster]
root      91738      1  0 00:32 ?        00:00:07 /usr/local/redis/bin/redis-server *:8007 [cluster]
root     103952      1  0 10月20 ?      00:02:50 /usr/local/redis/bin/redis-server *:8001 [cluster]

删除主节点

最后,我们尝试删除之前加入的主节点8007,这个步骤相对比较麻烦一些,因为主节点的里面是有分配了hash槽的,所以我们这里必须 先把8007里的hash槽放入到其他的可用主节点中去,然后再进行移除节点操作,不然会出现数据丢失问题(目前只能把master的数据迁移 到一个节点上,暂时做不了平均分配功能),执行命令如下:

[root@192 ~]# /usr/local/redis/bin/redis-cli -a soft01 --cluster reshard 192.168.0.138:8007
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.0.138:8007)
M: c80becfa75dd146eefdca89fa3c2a83d1a885b23 192.168.0.138:8007
   slots:[0-198],[5461-5661],[10923-11121] (599 slots) master
S: a90d542f240e9fd9bb0ca3861463163fac475b3a 192.168.0.138:8006
   slots: (0 slots) slave
   replicates 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52
S: bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 192.168.0.138:8003
   slots: (0 slots) slave
   replicates 5aaf267e75753a20b9b5a43273e68accb10571f5
M: 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 192.168.0.138:8001
   slots:[199-5460] (5262 slots) master
   1 additional replica(s)
M: 5aaf267e75753a20b9b5a43273e68accb10571f5 192.168.0.138:8005
   slots:[11122-16383] (5262 slots) master
   1 additional replica(s)
M: 44e2ec56944a0d9760b7180d7aebbc97b1fb495f 192.168.0.138:8004
   slots:[5662-10922] (5261 slots) master
   1 additional replica(s)
S: 1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 192.168.0.138:8002
   slots: (0 slots) slave
   replicates 44e2ec56944a0d9760b7180d7aebbc97b1fb495f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 
How many slots do you want to move (from 1 to 16384)? 600             
# 这里是需要把数据移动到哪?8001的主节点id
What is the receiving node ID? 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
# 准备移动的数据源?这里是8007的节点id  
Source node #1: c80becfa75dd146eefdca89fa3c2a83d1a885b23
# 结束指定数据源
Source node #2: done
# 指定计划
Do you want to proceed with the proposed reshard plan (yes/no)? yes

至此,我们已经成功的把8007主节点的数据迁移到8001上去了,我们可以看一下现在的集群状态如下图,你会发现8007下面已经没有任 何hash槽了,证明迁移成功!

$ /usr/local/redis/bin/redis-cli -a soft01 -p 8001
127.0.0.1:8001> cluster nodes
44e2ec56944a0d9760b7180d7aebbc97b1fb495f 192.168.0.138:8004@18004 master - 0 1666341387270 9 connected 5662-10922
1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 192.168.0.138:8002@18002 slave 44e2ec56944a0d9760b7180d7aebbc97b1fb495f 0 1666341388000 9 connected
a90d542f240e9fd9bb0ca3861463163fac475b3a 192.168.0.138:8006@18006 slave 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 0 1666341389305 12 connected
3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 192.168.0.138:8001@18001 myself,master - 0 1666341385000 12 connected 0-5661 10923-11121
5aaf267e75753a20b9b5a43273e68accb10571f5 192.168.0.138:8005@18005 master - 0 1666341388000 8 connected 11122-16383
bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 192.168.0.138:8003@18003 slave 5aaf267e75753a20b9b5a43273e68accb10571f5 0 1666341388281 8 connected
c80becfa75dd146eefdca89fa3c2a83d1a885b23 192.168.0.138:8007@18007 master - 0 1666341388080 11 connected

最后我们直接使用del-node命令删除8007主节点即可

$ /usr/local/redis/bin/redis-cli -a soft01 --cluster del-node 192.168.0.138:8007 c80becfa75dd146eefdca89fa3c2a83d1a885b23
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node c80becfa75dd146eefdca89fa3c2a83d1a885b23 from cluster 192.168.0.138:8007
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

# 查看集群状态,一切还原为最初始状态啦!大功告成!

$ /usr/local/redis/bin/redis-cli -a soft01 -p 8001
127.0.0.1:8001> cluster nodes
44e2ec56944a0d9760b7180d7aebbc97b1fb495f 192.168.0.138:8004@18004 master - 0 1666341540418 9 connected 5662-10922
1c6e6e3986840a30cb3ec9a3137b9fe82e8fc2e1 192.168.0.138:8002@18002 slave 44e2ec56944a0d9760b7180d7aebbc97b1fb495f 0 1666341538000 9 connected
a90d542f240e9fd9bb0ca3861463163fac475b3a 192.168.0.138:8006@18006 slave 3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 0 1666341538000 12 connected
3754fe578c29b147b0aaf4b6ed64ce97fdb3ab52 192.168.0.138:8001@18001 myself,master - 0 1666341539000 12 connected 0-5661 10923-11121
5aaf267e75753a20b9b5a43273e68accb10571f5 192.168.0.138:8005@18005 master - 0 1666341540000 8 connected 11122-16383
bab2c897f42a3e05af40d9f4d8abd0ae65beb34d 192.168.0.138:8003@18003 slave 5aaf267e75753a20b9b5a43273e68accb10571f5 0 1666341539407 8 connected

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/78239.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!