当前位置:首页 > 有云笔记 > NOSQL > 正文内容

创建集群 、 管理集群

小白2年前 (2022-03-29)NOSQL236350

一、部署Redis集群

1.1、准备6Redis服务器

创建6台虚拟机

Ip地址 主机名

192.168.4.51 host51

192.168.4.52 host52

192.168.4.53   host53

192.168.4.54   host54

192.168.4.55   host55

192.168.4.56   host56

分别做如下配置

1步 安装redis软件并做初始化配置

2步 停止按照初始化配置启动的Redis服务

3步 修改主配置文件(启用集群功能)

4步 启动redis服务 并查看端口号

重要说明:内存里不允许有数据 不能设置连接密码

配置服务器192.168.4.51 (删除已有51 重新创建新虚拟机 做如下配置)

[root@host51 ~]# yum -y install gcc
[root@host51 ~]# tar -xf redis-4.0.8.tar.gz
[root@host51 ~]# cd redis-4.0.8/
[root@host51 ~]# make && make install
[root@host51 ~]# ./utils/install_server.sh  遇到提示就回车
[root@host51 ~]# /etc/init.d/redis_6379  stop
[root@host51 ~]# vim /etc/redis/6379.conf
bind 192.168.4.51
port 6379
cluster-enabled yes #启用集群功能
cluster-config-file nodes-6379.conf 保存集群信息的配置文件
cluster-node-timeout 5000  集群中主机的连接超时时间
[root@host51 ~]#        
[root@host51 ~]# /etc/init.d/redis_6379  start
[root@host51 ~]# netstat  -utnlp  | grep redis-server
tcp        0      0 192.168.4.51:16379      0.0.0.0:*               LISTEN      4249/redis-server 1
tcp        0      0 192.168.4.51:6379      0.0.0.0:*               LISTEN      4249/redis-server 1

配置服务器192.168.4.52

[root@host52 ~]# yum -y install gcc
[root@host52 ~]# tar -xf redis-4.0.8.tar.gz
[root@host52 ~]# cd redis-4.0.8/
[root@host52 ~]# make && make install
[root@host52 ~]# ./utils/install_server.sh  遇到提示就回车
[root@host52 ~]# /etc/init.d/redis_6379  stop
[root@host52 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.4.52
port 6379
cluster-enabled yes #启用集群功能
cluster-config-file nodes-6379.conf 保存集群信息的配置文件
cluster-node-timeout 5000  集群中主机的连接超时时间
[root@host52 ~]#        
[root@host52 ~]# /etc/init.d/redis_6379  start
[root@host52 ~]# netstat  -utnlp  | grep redis-server
tcp        0      0 192.168.4.52:16379      0.0.0.0:*               LISTEN      4249/redis-server 1
tcp        0      0 192.168.4.52:6379       0.0.0.0:*               LISTEN      4249/redis-server 1

配置服务器192.168.4.53

[root@host53 ~]# yum -y install gcc
[root@host53 ~]# tar -xf redis-4.0.8.tar.gz
[root@host53 ~]# cd redis-4.0.8/
[root@host53 ~]# make && make install
[root@host53 ~]# ./utils/install_server.sh  遇到提示就回车
[root@host53 ~]# /etc/init.d/redis_6379  stop
[root@host53 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.4.53
port 6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
[root@host53 ~]#        
[root@host53 ~]# /etc/init.d/redis_6379  start
[root@host53 ~]# netstat  -utnlp  | grep redis-server
tcp        0      0 192.168.4.53:16379      0.0.0.0:*               LISTEN      4249/redis-server 1
tcp        0      0 192.168.4.53:6379       0.0.0.0:*               LISTEN      4249/redis-server 1

 配置服务器192.168.4.54

[root@host54 ~]# yum -y install gcc
[root@host54 ~]# tar -xf redis-4.0.8.tar.gz
[root@host54 ~]# cd redis-4.0.8/
[root@host54 ~]# make && make install
[root@host54 ~]# ./utils/install_server.sh  遇到提示就回车
[root@host54 ~]# /etc/init.d/redis_6379  stop
[root@host54 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.4.54
port 6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
[root@host54 ~]#        
[root@host54 ~]# /etc/init.d/redis_6379  start
[root@host54 ~]# netstat  -utnlp  | grep redis-server
tcp        0      0 192.168.4.54:16379      0.0.0.0:*               LISTEN      4249/redis-server 1
tcp        0      0 192.168.4.54:6379       0.0.0.0:*               LISTEN      4249/redis-server 1

配置服务器192.168.4.55

[root@host55 ~]# yum -y install gcc
[root@host55 ~]# tar -xf redis-4.0.8.tar.gz
[root@host55 ~]# cd redis-4.0.8/
[root@host55 ~]# make && make install
[root@host55 ~]# ./utils/install_server.sh  遇到提示就回车
[root@host55 ~]# /etc/init.d/redis_6379  stop
[root@host55 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.4.55
port 6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
[root@host55 ~]#        
[root@host55 ~]# /etc/init.d/redis_6379  start
[root@host55 ~]# netstat  -utnlp  | grep redis-server
tcp        0      0 192.168.4.55:16379      0.0.0.0:*               LISTEN      4249/redis-server 1
tcp        0      0 192.168.4.55:6379      0.0.0.0:*               LISTEN      4249/redis-server 1

配置服务器192.168.4.56

[root@host56 ~]# yum -y install gcc
[root@host56 ~]# tar -xf redis-4.0.8.tar.gz
[root@host56 ~]# cd redis-4.0.8/
[root@host56 ~]# make && make install
[root@host56 ~]# ./utils/install_server.sh  遇到提示就回车
[root@host56 ~]# /etc/init.d/redis_6379  stop
[root@host56 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.4.56
port 6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
[root@host56 ~]#        
[root@host56 ~]# /etc/init.d/redis_6379  start
[root@host56 ~]# netstat  -utnlp  | grep redis-server
tcp        0      0 192.168.4.56:16379      0.0.0.0:*               LISTEN      4249/redis-server 1
tcp        0      0 192.168.4.56:6379       0.0.0.0:*               LISTEN      4249/redis-server 1

分别启用6Redis服务的集群功能

[root@host51 ~]# vim /etc/redis/6379.conf
cluster-enabled yes #启用集群功能
cluster-config-file nodes-6379.conf 保存集群信息的配置文件
cluster-node-timeout 5000  集群中主机的连接超时时间

[root@host51 ~]# /etc/init.d/redis_6379  restart
[root@host51 ~]# netstat  -utnlp  | grep redis-server
tcp        0      0 192.168.4.51:16379      0.0.0.0:*               LISTEN      4249/redis-server 1
tcp        0      0 192.168.4.51:6379      0.0.0.0:*               LISTEN      4249/redis-server 1

#配置52

[root@host52 ~]# vim /etc/redis/6379.conf
cluster-enabled yes #启用集群功能
cluster-config-file nodes-6379.conf #保存集群信息的配置文件
cluster-node-timeout 5000  #集群中主机的连接超时时间
       
[root@host52 ~]# /etc/init.d/redis_6379  restart
[root@host52 ~]# netstat  -utnlp  | grep redis-server
tcp        0      0 192.168.4.52:16379      0.0.0.0:*               LISTEN      4249/redis-server 1
tcp        0      0 192.168.4.52:6379      0.0.0.0:*               LISTEN      4249/redis-server 1

配置53

[root@host53 ~]# vim /etc/redis/6379.conf
cluster-enabled yes #启用集群功能
cluster-config-file nodes-6379.conf #保存集群信息的配置文件
cluster-node-timeout 5000  #集群中主机的连接超时时间
       
[root@host53 ~]# /etc/init.d/redis_6379  start
[root@host53 ~]# netstat  -utnlp  | grep redis-server
tcp        0      0 192.168.4.53:16379      0.0.0.0:*               LISTEN      4249/redis-server 1
tcp        0      0 192.168.4.53:6379      0.0.0.0:*               LISTEN      4249/redis-server 1


配置54

[root@host54 ~]# vim /etc/redis/6379.conf
cluster-enabled yes #启用集群功能
cluster-config-file nodes-6379.conf 保存集群信息的配置文件
cluster-node-timeout 5000  #集群中主机的连接超时时间
       
[root@host54 ~]# /etc/init.d/redis_6379  restart
[root@host54 ~]# netstat  -utnlp  | grep redis-server
tcp        0      0 192.168.4.54:16379      0.0.0.0:*               LISTEN      4249/redis-server 1
tcp        0      0 192.168.4.54:6379      0.0.0.0:*               LISTEN      4249/redis-server 1

配置55

[root@host55 ~]# vim /etc/redis/6379.conf
cluster-enabled yes #启用集群功能
cluster-config-file nodes-6379.conf #保存集群信息的配置文件
cluster-node-timeout 5000  #集群中主机的连接超时时间
       
[root@host55 ~]# /etc/init.d/redis_6379  restart
[root@host55 ~]# netstat  -utnlp  | grep redis-server
tcp        0      0 192.168.4.55:16379      0.0.0.0:*               LISTEN      4249/redis-server 1
tcp        0      0 192.168.4.55:6379      0.0.0.0:*               LISTEN      4249/redis-server 1

配置56

[root@host56 ~]# vim /etc/redis/6379.conf
cluster-enabled yes #启用集群功能
cluster-config-file nodes-6379.conf #保存集群信息的配置文件
cluster-node-timeout 5000  #集群中主机的连接超时时间
       
[root@host56 ~]# /etc/init.d/redis_6379  restart
[root@host56 ~]# netstat  -utnlp  | grep redis-server
tcp        0      0 192.168.4.56:16379      0.0.0.0:*               LISTEN      4249/redis-server 1
tcp        0      0 192.168.4.56:6379      0.0.0.0:*               LISTEN      4249/redis-server 1

1.2 配置管理主机 192.168.4.57

 第一步:准备ruby脚本的运行环境

 第二步:创建脚本

 第三步:查看脚本帮助信息

]#yum  -y  install   rubygems   ruby
]#gem  install  redis-3.2.1.gem
]#mkdir  /root/bin      //创建命令检索目录
]#tar -xf redis-4.0.8.tar.gz
]#cp  redis-4.0.8/src/redis-trib.rb   /root/bin/  
]#chmod  +x   /root/bin/redis-trib.rb  设置执行权限
]#redis-trib.rb   help   (能看到帮助信息为成功)

1.3 创建集群

#在管理主机mgm57 执行创建集群的命令

创建集群的命令格式   

[root@mgm57 ~]# redis-trib.rb  create --replicas 数字   ip地址:端口  ip地址:端口  ip地址:端口  ....

说明,创建集群时,会把前3台服务器 配置为主服务器,剩下的其他主机全做从服务器

--replicas 从服务器的台数(指定每个主服务器有几台从服务器)

[root@mgm57 ~]# redis-trib.rb create --replicas 1  192.168.4.51:6379  192.168.4.52:6379 192.168.4.53:6379 192.168.4.54:6379 192.168.4.55:6379 192.168.4.56:6379
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.4.51:6379
192.168.4.52:6379
192.168.4.53:6379
Adding replica 192.168.4.55:6379 to 192.168.4.51:6379
Adding replica 192.168.4.56:6379 to 192.168.4.52:6379
Adding replica 192.168.4.54:6379 to 192.168.4.53:6379
M: 0eb3b7aa0493a19189cba35b0c658202cc20884b 192.168.4.51:6379
   slots:0-5460 (5461 slots) master
M: a9cb8ccd31bf3eb70433c00906c9f1a99b5e8441 192.168.4.52:6379
   slots:5461-10922 (5462 slots) master
M: f2c1bdb78d8d224c3666fa6440bdf80ba563c581 192.168.4.53:6379
   slots:10923-16383 (5461 slots) master
S: bdba77868965371680fa825badff59bf8ef95a81 192.168.4.54:6379
   replicates f2c1bdb78d8d224c3666fa6440bdf80ba563c581
S: 11510f42bfd9cf667bab5f31c01476d94984200a 192.168.4.55:6379
   replicates 0eb3b7aa0493a19189cba35b0c658202cc20884b
S: fe572ce003ee634c52adc4b42d92d15f847937d7 192.168.4.56:6379
   replicates a9cb8ccd31bf3eb70433c00906c9f1a99b5e8441
Can I set the above configuration? (type 'yes' to accept): yes 同意 
....
....
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.  创建成功的提示
[root@mgm57 ~]#

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

创建集群失败:

1 如果所有的Redis服务都启用的集群功能 内存里也没有数据 也没设置连接密码 还是创建失败

  可能是物理内存不足 导致创建失败 把没用的虚拟机删除

2 在集群中所有主机执行清除集群信息的命令 > cluster reset  

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1.4 查看集群信息

#查看集群统计信息

[root@mgm57 ~]# redis-trib.rb info  192.168.4.56:6379
192.168.4.53:6379 (f2c1bdb7...) -> 0 keys | 5461 slots | 1 slaves.
192.168.4.51:6379 (0eb3b7aa...) -> 0 keys | 5461 slots | 1 slaves.
192.168.4.52:6379 (a9cb8ccd...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
[root@mgm57 ~]#

#查看集群详细信息

[root@mgm57 ~]# redis-trib.rb check  192.168.4.51:6379
>>> Performing Cluster Check (using node 192.168.4.51:6379)
M: 0eb3b7aa0493a19189cba35b0c658202cc20884b 192.168.4.51:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: f2c1bdb78d8d224c3666fa6440bdf80ba563c581 192.168.4.53:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: bdba77868965371680fa825badff59bf8ef95a81 192.168.4.54:6379
   slots: (0 slots) slave
   replicates f2c1bdb78d8d224c3666fa6440bdf80ba563c581
M: a9cb8ccd31bf3eb70433c00906c9f1a99b5e8441 192.168.4.52:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 11510f42bfd9cf667bab5f31c01476d94984200a 192.168.4.55:6379
   slots: (0 slots) slave
   replicates 0eb3b7aa0493a19189cba35b0c658202cc20884b
S: fe572ce003ee634c52adc4b42d92d15f847937d7 192.168.4.56:6379
   slots: (0 slots) slave
   replicates a9cb8ccd31bf3eb70433c00906c9f1a99b5e8441
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@mgm57 ~]#

二、访问集群存取数据

命令格式  

]# redis-cli  -c   -h redis服务器的ip   -p 端口号

说明:

连接集群中的任意一台服务器都可以查询数据和存储数据)

-c 连接集群中的主机 使用集群算法存储数据

2.1 在客户端连接集群中的主机

说明:

统一在Host51主机演示存储数数据

向集群中的主机存储数据一次只能存储一个变量集群算法每次只有一个计算结果

#存储数据

[root@host51 ~]# redis-cli -c  -h 192.168.4.51
192.168.4.51:6379> keys *
(empty list or set)
192.168.4.51:6379> set name plj
-> Redirected to slot [5798] located at 192.168.4.52:6379
OK
192.168.4.52:6379> keys *
1) "name"
192.168.4.52:6379> set age 19
-> Redirected to slot [741] located at 192.168.4.51:6379
OK
192.168.4.51:6379> keys *
1) "age"
192.168.4.51:6379> set school tarena
-> Redirected to slot [8455] located at 192.168.4.52:6379
OK
192.168.4.52:6379> exit

#查询数据

root@host51 ~]# redis-cli -c  -h 192.168.4.56
192.168.4.56:6379> keys *
1) "school"
2) "name"
192.168.4.56:6379> get name
-> Redirected to slot [5798] located at 192.168.4.52:6379
"plj"
192.168.4.52:6379> keys *
1) "name"
2) "school"
192.168.4.52:6379> get age
-> Redirected to slot [741] located at 192.168.4.51:6379
"19"
192.168.4.51:6379> keys *
1) "age"
192.168.4.51:6379> exit
[root@host51 ~]#

2.2 集群存储数据的工作原理 (!!!重点难点!!!)

Redis集群是如何实现数据的分布式存储的?

存储数据和查询数据时 调用集群算法  集群算法会计算出1个数字, 数字

在哪个主服务占用的范围内 ,就连接对应的主服务器存储数据或查询数据。

三、管理集群

环境准备:

创建2台新的虚拟机 ip  192.168.4.58  和  192.168.4.59

分别运行redis 服务且启用了集群功能

192.168.4.58主机redis服务的端口 6379   服务使用的地址 192.168.4.58

192.168.4.59主机redis服务的端口 6379   服务使用的地址 192.168.4.59

[root@host58 ~]# yum -y install gcc
[root@host58 ~]# tar -xf redis-4.0.8.tar.gz
[root@host58 ~]# cd redis-4.0.8/
[root@host58 ~]# make && make install
[root@host58 ~]# ./utils/install_server.sh  遇到提示就回车
[root@host58 ~]# /etc/init.d/redis_6379  stop
[root@host58 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
[root@host58 ~]# vim /etc/redis/6379.conf
bind 192.168.4.58
port 6379
cluster-enabled yes #启用集群功能
cluster-config-file nodes-6379.conf 保存集群信息的配置文件
cluster-node-timeout 5000  集群中主机的连接超时时间


[root@host58 ~]# /etc/init.d/redis_6379  restart
[root@host58 ~]# netstat  -utnlp  | grep redis-server
tcp        0      0 192.168.4.58:16379      0.0.0.0:*               LISTEN      4249/redis-server 1
tcp        0      0 192.168.4.58:6379      0.0.0.0:*               LISTEN      4249/redis-server 1
 
[root@host59 ~]# yum -y install gcc
[root@host59 ~]# tar -xf redis-4.0.8.tar.gz
[root@host59 ~]# cd redis-4.0.8/
[root@host59 ~]# make && make install
[root@host59 ~]# ./utils/install_server.sh  遇到提示就回车
[root@host59 ~]# /etc/init.d/redis_6379  stop
[root@host59 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
[root@host59 ~]# vim /etc/redis/6379.conf
bind 192.168.4.59
port 6379
cluster-enabled yes #启用集群功能
cluster-config-file nodes-6379.conf 保存集群信息的配置文件
cluster-node-timeout 5000  集群中主机的连接超时时间
       
[root@host59 ~]# /etc/init.d/redis_6379  restart
[root@host59 ~]# netstat  -utnlp  | grep redis-server
tcp        0      0 192.168.4.59:16379      0.0.0.0:*               LISTEN      4249/redis-server 1
tcp        0      0 192.168.4.59:6379      0.0.0.0:*               LISTEN      4249/redis-server 1

3.1 向集群里添加新服务器

说明:什么情况下需要向集群里添加新主机(添加master角色服务器)

扩大内存空间(添加master角色服务器)。

为了保证服务的可靠性(给主服务器添加多个从服务器)

1)添加master角色的服务器

诉求把新的Redis服务器Host58 添加到集群里做master 服务器

具体配置如下:

1Host58运行redis 服务且启用了集群功能

redis服务的端口 6379   服务使用的地址 192.168.4.58

2) 在管理主机mgm57 如下操作:

2.1 host58主机添加到集群里

命令格式

[root@mgm57 ~]# redis-trib.rb add-node 新主机Ip:端口  集群中已有主机的ip:端口

[root@mgm57 ~]# redis-trib.rb add-node 192.168.4.58:6379 192.168.4.51:6379
.....
.....
[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.4.58:6379 to make it join the cluster.
[OK] New node added correctly.  添加成功的提示
[root@mgm57 ~]#

#新添加的master角色主机没有hash slots

[root@mgm57 ~]# redis-trib.rb info  192.168.4.51:6379
192.168.4.51:6379 (0eb3b7aa...) -> 2 keys | 5461 slots | 1 slaves.
192.168.4.53:6379 (f2c1bdb7...) -> 1 keys | 5461 slots | 1 slaves.
192.168.4.52:6379 (a9cb8ccd...) -> 3 keys | 5462 slots | 1 slaves.
192.168.4.58:6379 (87cc1c12...) -> 0 keys | 0 slots | 0 slaves.       零个hash slots
[OK] 6 keys in 4 masters.
0.00 keys per slot on average.
[root@mgm57 ~]#

2.2 分配hast slots (master角色的服务器没有hast slots得不到存储数据机会)

命令格式

[root@mgm57 ~]# redis-trib.rb   reshard  集群中已有主机的ip:端口

[root@mgm57 ~]# redis-trib.rb reshard  192.168.4.56:6379

1个问题 移除多少个hast slots

How many slots do you want to move (from 1 to 16384)? 4096

2个问题 把4096个槽给哪台主数据库服务器 (host58主机的id)

What is the receiving node ID?87cc1c128166e08a16cc294758611453bbc71437

3个问题 提供4096个槽 主机的id (all 表示当前所有主服务器一起提供4096个槽给host58)

Source node #1:all 

4个问题 确认前3步的配置 yes 同意 no  退出

Do you want to proceed with the proposed reshard plan (yes/no)? yes  

2.3 查看集群信息 (查看到多新的主服务器且有hast slots 为成功)

[root@mgm57 ~]# redis-trib.rb info  192.168.4.56:6379
192.168.4.53:6379 (f2c1bdb7...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.51:6379 (0eb3b7aa...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.52:6379 (a9cb8ccd...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.58:6379 (87cc1c12...) -> 2 keys | 4096 slots | 0 slaves.
[OK] 6 keys in 4 masters.
0.00 keys per slot on average.
[root@mgm57 ~]#
#具体查看host58 占用4096个槽的范围
[root@mgm57 ~]# redis-trib.rb check  192.168.4.56:6379
....
....
M: 87cc1c128166e08a16cc294758611453bbc71437 192.168.4.58:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)

2)添加slave角色的服务器

说明:从角色服务器的数据是master服务器同步过来的数据

  所以slave角色的服务器不需要分配hash slots

  只要把主机添加到集群了做slave服务器就可以了。

命令格式

[root@mgm57 ~]# redis-trib.rb  add-node   --slave  新主机Ip:端口  集群中已有主机的ip:端口

    诉求: Redis服务器host59 添加到集群里做host58 slave 服务器

  具体操作如下:

1) host59 主机运行redis服务 且启用了集群功能

2) 在管理主机做如下操作:

2.1 host59 添加到集群里 做slave服务器

2.2 查看集群统计信息 (发现host58 有一个slave服务器)

2.3 连接host59 查看数据 (会自动同步主服务器host58 的数据)

!!!!会自动做从服务器个数最少的 master服务器的从服务器。!!!

[root@mgm57 ~]# redis-trib.rb add-node --slave 192.168.4.59:6379  192.168.4.56:6379
....
....
[OK] All 16384 slots covered.
Automatically selected master 192.168.4.58:6379
>>> Send CLUSTER MEET to node 192.168.4.59:6379 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.4.58:6379.
[OK] New node added correctly.
[root@mgm57 ~]#
[root@mgm57 ~]# redis-trib.rb info  192.168.4.56:6379
192.168.4.53:6379 (f2c1bdb7...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.51:6379 (0eb3b7aa...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.52:6379 (a9cb8ccd...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.58:6379 (87cc1c12...) -> 2 keys | 4096 slots | 1 slaves.  host58主机的从
[OK] 6 keys in 4 masters.
0.00 keys per slot on average.
[root@mgm57 ~]#
 
[root@mgm57 ~]# redis-trib.rb check  192.168.4.56:6379
....
.....
S: d50aa7c1acebe69af0834f1838c8b17b2348472e 192.168.4.59:6379
   slots: (0 slots) slave
   replicates 87cc1c128166e08a16cc294758611453bbc71437    主服务器的id
    
[root@host56 ~]# redis-cli  -c  -h 192.168.4.59 -p 6379  连接host59 查看数据
192.168.4.59:6379> keys *
1) "name"
2) "age"
192.168.4.59:6379>

3.2 把服务器移除集群

3.2.1  移除slave角色的主机

命令格式  [root@mgm57 ~]# redis-trib.rb del-node  集群中任意主机的Ip:端口  被移除主机的id

说明:slave角色的主机的没有hash slots 直接移除即可

  主机被移除集群后redis服务会自动停止

诉求: Host59  slave 服务器移除集群,在管理主机做如下操作

  1) 查看host59主机的id , 执行移除主机host59的命令

  2) 查看集群信息 (发现host58 主机slave 服务个数是 0台)

具体操作如下                

root@mgm57 ~]# redis-trib.rb  info  192.168.4.56:6379 没删除之前查看信息
192.168.4.53:6379 (f2c1bdb7...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.51:6379 (0eb3b7aa...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.52:6379 (a9cb8ccd...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.58:6379 (87cc1c12...) -> 2 keys | 4096 slots | 1 slaves.
[OK] 6 keys in 4 masters.
0.00 keys per slot on average.
[root@mgm57 ~]#

[root@mgm57 ~]# redis-trib.rb  check   192.168.4.56:6379 | grep 192.168.4.59
S: d50aa7c1acebe69af0834f1838c8b17b2348472e 192.168.4.59:6379
[root@mgm57 ~]#

[root@mgm57 ~]# redis-trib.rb  del-node 192.168.4.56:6379 d50aa7c1acebe69af0834f1838c8b17b2348472e
>>> Removing node d50aa7c1acebe69af0834f1838c8b17b2348472e from cluster 192.168.4.56:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node. 把服务给停止了
[root@mgm57 ~]#
[root@mgm57 ~]# redis-trib.rb  info  192.168.4.56:6379
192.168.4.53:6379 (f2c1bdb7...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.51:6379 (0eb3b7aa...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.52:6379 (a9cb8ccd...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.58:6379 (87cc1c12...) -> 2 keys | 4096 slots | 0 slaves.  零台从服务器
[OK] 6 keys in 4 masters.
0.00 keys per slot on average.
[root@mgm57 ~]#
 
#host59主机查看Redis状态 (看不到了)
[root@host59 ~]# netstat  -utnalp  | grep redis-server
[root@host59 ~]#

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·

3.2.2  移除master角色的主机

说明:master角色的服务器会占用hash slots  要先释放hash slots  再执行移除主机的命令

具体操作步骤:

第一步:释放 hash slots (再次执行reshard命令)

第二步:移除主机 (执行删除主机的命令)

诉求把master角色 主机 host58移除集群,在管理主机mgm57 做如下操作:

#释放 hash slots (再次执行reshard命令)

[root@mgm57 ~]# redis-trib.rb  reshard  192.168.4.56:6379

1个问题:释放hash slots 的个数

How many slots do you want to move (from 1 to 16384)? 4096  host58主机占用hash slots 的个数)

2个问题:接收4096的个hash slots的主数据库服务器的ID (随便给那个主服务器都可以)

What is the receiving node ID?0eb3b7aa0493a19189cba35b0c658202cc20884b   (host51主机的id ,就是把释放的4096hash slots给主数据库服务器host51)

3个问题:从那台主服务器移除4096hash slots

Source node #1:87cc1c128166e08a16cc294758611453bbc71437  (host58主机的id)

Source node #2:done 结束指定

4个问题:确认配置 yes同意  no 退出

Do you want to proceed with the proposed reshard plan (yes/no)? yes  同意

 查看集群信息(发现host51 主服务器hash slots变多了 )

[root@mgm57 ~]# redis-trib.rb  info  192.168.4.56:6379
192.168.4.53:6379 (f2c1bdb7...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.51:6379 (0eb3b7aa...) -> 3 keys | 8192 slots | 1 slaves. 槽多了
192.168.4.52:6379 (a9cb8ccd...) -> 2 keys | 4096 slots | 1 slaves.
192.168.4.58:6379 (87cc1c12...) -> 0 keys | 0 slots | 0 slaves. 一个槽也没有了
[OK] 6 keys in 4 masters.
0.00 keys per slot on average.
[root@mgm57 ~]#

查看host58主机的id 然后删除主机 Host58

[root@mgm57 ~]# redis-trib.rb  check  192.168.4.56:6379 | grep  192.168.4.58
M: 87cc1c128166e08a16cc294758611453bbc71437 192.168.4.58:6379
[root@mgm57 ~]#
[root@mgm57 ~]# redis-trib.rb  del-node   192.168.4.56:6379  87cc1c128166e08a16cc294758611453bbc71437
>>> Removing node 87cc1c128166e08a16cc294758611453bbc71437 from cluster 192.168.4.56:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[root@mgm57 ~]#

再次查看集群信息 (没有host58 主机了 )

[root@mgm57 ~]# redis-trib.rb  info  192.168.4.56:6379
192.168.4.53:6379 (f2c1bdb7...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.51:6379 (0eb3b7aa...) -> 3 keys | 8192 slots | 1 slaves.
192.168.4.52:6379 (a9cb8ccd...) -> 2 keys | 4096 slots | 1 slaves.
[OK] 6 keys in 3 masters.
0.00 keys per slot on average.
[root@mgm57 ~]#

 扩展知识

1) 平均分配当前所有主服务器的hash slots

[root@mgm57 ~]# redis-trib.rb  info  192.168.4.56:6379  槽不平均
192.168.4.53:6379 (f2c1bdb7...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.51:6379 (0eb3b7aa...) -> 3 keys | 8192 slots | 1 slaves.
192.168.4.52:6379 (a9cb8ccd...) -> 2 keys | 4096 slots | 1 slaves.
[OK] 6 keys in 3 masters.
0.00 keys per slot on average.
[root@mgm57 ~]#

平均分配hash slots

[root@mgm57 ~]# redis-trib.rb rebalance 192.168.4.56:6379

再次查看平均了

[root@mgm57 ~]# redis-trib.rb info 192.168.4.56:6379
192.168.4.53:6379 (f2c1bdb7...) -> 2 keys | 5462 slots | 1 slaves.
192.168.4.51:6379 (0eb3b7aa...) -> 2 keys | 5461 slots | 1 slaves.
192.168.4.52:6379 (a9cb8ccd...) -> 2 keys | 5461 slots | 1 slaves.
[OK] 6 keys in 3 masters.
0.00 keys per slot on average.
[root@mgm57 ~]#

2) 把移除的主机再次添加到集群里的步骤

第一步: 启动Redis服务并清除前世记忆(在Redis服务器本机操作)

第二步: 清空内存里的数据(在Redis服务器本机操作)

第三步: 在管理主机执行执行添加命令(在管理主机执行)

第四步: 查看是否添加成功(在管理主机执行)

把移除host58再添加到集群里做master服务器

#58主机启动服务并清除前世记忆

[root@host58 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host58 ~]# netstat  -utnlp  | grep  -i redis-server
tcp        0      0 192.168.4.58:6379       0.0.0.0:*               LISTEN      4480/redis-server 1
tcp        0      0 192.168.4.58:16379      0.0.0.0:*               LISTEN      4480/redis-server 1
[root@host58 ~]#
[root@host58 ~]# redis-cli  -h 192.168.4.58 -p 6379
192.168.4.58:6379> cluster reset清除前世记忆
OK
192.168.4.58:6379>
192.168.4.58:6379> keys *
(empty list or set)
192.168.4.58:6379>
        #在管理主机把5添加到集群里
[root@mgm57 ~]# redis-trib.rb add-node 192.168.4.58:6379  192.168.4.51:6351
 
        #查看集群信息
[root@mgm57 ~]# redis-trib.rb info  192.168.4.51:6351  发现多了主服务器58

移除的59 再次添加到集群 做53主机的slave数据库服务器 

命令格式

redis-trib.rb add-node --slave --master-id 主服务器的id   新服务器的ip:端口 集群中已有机器的Ip:端口  

 #启动53主机的redis服务并清除前世记录

]# /etc/init.d/redis_6379 start
]# redis-cli  -h 192.168.4.59 -p 6379
> cluster  reset
> exit

#在管理主机查看host53主机的id

[root@mgm57 ~]# redis-trib.rb check   192.168.4.51:6351 | grep 192.168.4.53
M: d9c9ef29c07ad740a05fa7aaa4d96ce25fa089b5 192.168.4.53:6353
[root@mgm57 ~]#
#添加59主机做主服务器53的 slave服务器
[root@mgm57 ~]# redis-trib.rb add-node --slave --master-id d9c9ef29c07ad740a05fa7aaa4d96ce25fa089b5 192.168.4.59:6379 192.168.4.51:6351

#查看集群信息

[root@mgm57 ~]# redis-trib.rb info  192.168.4.51:6351
192.168.4.51:6351 (4cf556bc...) -> 1 keys | 5461 slots | 1 slaves.
192.168.4.52:6352 (94cbe47c...) -> 3 keys | 5462 slots | 1 slaves.
192.168.4.53:6353 (d9c9ef29...) -> 2 keys | 5461 slots | 2 slaves.  53 有2台从服务器
[OK] 6 keys in 3 masters.
0.00 keys per slot on average.
[root@mgm57 ~]#

3)把集群中的主机恢复为独立的数据库服务器(host51~host56

在数据库服务器本机执行如下操作

#停止redis服务

#注释掉配置文件中的集群功能

#清空数据库目录

#启动服务

#连接服务查看不到集群信息 也没有数据

以数据库服务器host51为例演示:

[root@mgm57 ~]# redis-trib.rb info 192.168.4.56:6379
192.168.4.53:6379 (f2c1bdb7...) -> 2 keys | 5462 slots | 1 slaves.
192.168.4.51:6379 (0eb3b7aa...) -> 2 keys | 5461 slots | 2 slaves.
192.168.4.52:6379 (a9cb8ccd...) -> 2 keys | 5461 slots | 1 slaves.
[OK] 6 keys in 3 masters.
0.00 keys per slot on average.
[root@mgm57 ~]#

[root@host51 ~]# redis-cli  -h 192.168.4.51 shutdown
[root@host51 ~]# vim  /etc/redis/6379.conf
#cluster-enabled yes
#cluster-config-file nodes-6379.conf
#cluster-node-timeout 5000
[root@host51 ~]#
[root@host51 ~]# rm -rf /var/lib/redis/6379/*
[root@host51 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host51 ~]#
[root@host51 ~]# netstat -utnalp  |grep -i redis-server
tcp        0      0 192.168.4.51:6379       0.0.0.0:*               LISTEN      9082/redis-server 1
[root@host51 ~]#
 
[root@host51 ~]# redis-cli  -h 192.168.4.51 -p 6379
192.168.4.51:6379> keys *
(empty list or set)
192.168.4.51:6379> cluster info
ERR This instance has cluster support disabled  没有启用集群功能
192.168.4.51:6379>

#在管理主机查看集群信息看不到host51主机了

[root@mgm57 ~]# redis-trib.rb info 192.168.4.56:6379
192.168.4.54:6379 (bdba7786...) -> 2 keys | 5462 slots | 1 slaves.
192.168.4.52:6379 (a9cb8ccd...) -> 2 keys | 5461 slots | 1 slaves.
192.168.4.58:6379 (87cc1c12...) -> 2 keys | 5461 slots | 0 slaves.
[OK] 6 keys in 3 masters.
0.00 keys per slot on average.
[root@mgm57 ~]#

over

创建集群 、 管理集群  您阅读本篇文章共花了: 

分享到:

    扫描二维码推送至手机访问。

    版权声明:本文由有云转晴发布,如需转载请注明出处。

    本文链接:https://www.yyzq.cf/?id=154

    分享给朋友:

    发表评论

    访客

    ◎欢迎参与讨论,请在这里发表您的看法和观点。