创建集群
集群环境
拓扑结构
IP规划
工作原理
工作原理
- 无论有多少台redis服务器,一共都会有0-16383共16384个槽
- slot的作用:用来确定存储变量时,变量存储在哪一台redis服务器上的
- 存取数据的过程:变量名与内置的crc16算法做一个hash计算,得到的数字再与16384进行取余,根据余数的结果确定在哪一台服务器节点
- 0-5460
- 5461-10922
- 10923-16383
- 能够存多少变量取决于本机的内存的大小
创建集群
- 环境准备
=========================================
1.确保每一台redis都是空的,由于前次试验在host51上操作,所以先将host51还原至空的状态,一定要保证redis是空的状态!!!
[root@host51 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@host51 ~]# vim /etc/redis/6379.conf #将第501行注释掉,不使用密码登录
501 #requirepass 123456
[root@host51 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> exit
#######################
如果此时重启报错,可能是pid冲突,可以进行如下操作
[root@host51 ~]# /etc/init.d/redis_6379 start
/var/run/redis_6379.pid exists, process is already running or crashed
[root@host51 ~]# rm -rf /var/run/redis_6379.pid
[root@host51 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
#######################
******************************
2.接下来在host52-56主机分别准备好redis服务器,我们可以写一些简单的不能再简单的脚本,以host52主机为例
[root@host52 ~]# cd /opt
[root@host52 opt]# for i in 53 54 55 56
> do
> scp /opt/redis-4.0.8.tar.gz root@192.168.4.$i:/opt
> done
[root@host52 opt]# vim redis.sh
#!/bin/bash
yum -y install gcc #下载依赖包
tar -zxvf redis-4.0.8.tar.gz #解压缩
cd redis-4.0.8/
make && make install #编译安装
./utils/install_server.sh
[root@host52 opt]# for i in 52 53 54 55 56
>do
>scp /opt/redis.sh root@192.168.4.$i:/opt
>done
[root@host52 opt]# bash redis.sh
[root@host52 opt]#ss -nutlp | grep 6379
[root@host52 opt]#/etc/init.d/redis_6379 stop #停止服务
[root@host52 opt]#vim +70 /etc/redis/6379.conf #修改IP地址
70 bind 192.168.4.52
[root@host52 opt]#vim +93 /etc/redis/6379.conf #将端口号改为6352
93 port 6352
[root@host52 opt]#/etc/init.d/redis_6379 start #起服务
[root@host52 opt]#redis-cli -p 6352 -h 192.168.4.52
*****
检查准备工作:
[root@host51 ~]# ss -nutlp | grep 6351
tcp LISTEN 0 128 192.168.4.51:6351 *:* users:(("redis-server",pid=1841,fd=6))
[root@host52 opt]# ss -nutlp | grep 6352
tcp LISTEN 0 128 127.0.0.1:6352 *:* users:(("redis-server",pid=4753,fd=6))
[root@host53 opt]# ss -nutlp | grep 6353
tcp LISTEN 0 128 127.0.0.1:6353 *:* users:(("redis-server",pid=4721,fd=6))
[root@host54 opt]# ss -nutlp | grep 6354
tcp LISTEN 0 128 127.0.0.1:6354 *:* users:(("redis-server",pid=4722,fd=6))
[root@host55 opt]# ss -nutlp | grep 6355
tcp LISTEN 0 128 127.0.0.1:6355 *:* users:(("redis-server",pid=4749,fd=6))
[root@host56 opt]# ss -nutlp | grep 6356
tcp LISTEN 0 128 127.0.0.1:6356 *:* users:(("redis-server",pid=4710,fd=6))
每台redis服务器都必须是空的
redis-trib脚本
- 用法:
- [root@host57 ~]# redis-trib.rb <arguments…>
命令 | 描述 |
---|---|
create | 创建集群 |
check | 检查集群 |
info | 查看集群信息 |
reshard | 重新分片 |
del-node | 删除主机 |
add-node --slave | 添加slave主机 |
add-node | 添加master主机 |
rebalance | 平均分配hash slots |
- 创建集群
===============================================
1.配置管理主机host57,也可以部署在任意一台redis服务器上
[root@host57 opt]# rpm -q ruby
未安装软件包 ruby
[root@host57 opt]# which gem
/usr/bin/which: no gem in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
[root@host57 opt]# yum -y install rubygems ruby
[root@host57 opt]# ls
redis-4.0.8.tar.gz redis-3.2.1.gem
[root@host57 opt]# gem install redis-3.2.1.gem
Successfully installed redis-3.2.1
Parsing documentation for redis-3.2.1
Installing ri documentation for redis-3.2.1
1 gem installed
[root@host57 opt]# tar -zvxf redis-4.0.8.tar.gz
[root@host57 opt]# cd redis-4.0.8/
[root@host57 redis-4.0.8]# ls
00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests
BUGS deps MANIFESTO runtest sentinel.conf utils
CONTRIBUTING INSTALL README.md runtest-cluster src
[root@host57 redis-4.0.8]# ls src/*.rb #ruby脚本
src/redis-trib.rb
[root@host57 redis-4.0.8]# echo $PATH #查看当前变量
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@host57 redis-4.0.8]# mkdir /root/bin #创建命令检索目录
[root@host57 redis-4.0.8]# cp src/redis-trib.rb /root/bin #创建管理集群脚本
[root@host57 redis-4.0.8]# chmod +x /root/bin/redis-trib.rb
[root@host57 redis-4.0.8]# redis-trib.rb help #查看命令帮助
[root@host57 redis-4.0.8]# redis-trib.rb help
Usage: redis-trib <command> <options> <arguments ...>
create host1:port1 ... hostN:portN
--replicas <arg>
check host:port
info host:port
fix host:port
--timeout <arg>
reshard host:port
--from <arg>
--to <arg>
--slots <arg>
--yes
--timeout <arg>
--pipeline <arg>
rebalance host:port
--weight <arg>
--auto-weights
--use-empty-masters
--timeout <arg>
--simulate
--pipeline <arg>
--threshold <arg>
add-node new_host:new_port existing_host:existing_port
--slave
--master-id <arg>
del-node host:port node_id
set-timeout host:port milliseconds
call host:port command arg arg .. arg
import host:port
--from <arg>
--copy
--replace
help (show this help)
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
*************************************
2.开启集群,以host51为例,每台redis集群都要做
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> cluster info
ERR This instance has cluster support disabled #此时会报错,因为没有配置集群功能
[root@host51 ~]# vim +815 /etc/redis/6379.conf
815 cluster-enabled yes #启用集群功能
823 cluster-config-file nodes-6379.conf #存储集群信息的配置文件
829 cluster-node-timeout 5000 #集群节点的通信超时时间
[root@host51 ~]# rm -rf /var/lib/redis/6379/* #清空数据
[root@host51 ~]# vim +43 /etc/init.d/redis_6379
$CLIEXEC -h 192.168.4.51 -p 6351 shutdown
:wq
[root@host51 ~]# /etc/init.d/redis_6379 start
Starting Redis server..
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> cluster info #查看集群信息,此时没有将其他主机加入集群,所以查看集群状态是fail
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
[root@host51 ~]# netstat -nutlp | grep redis-server
tcp 0 0 192.168.4.51:16351 0.0.0.0:* LISTEN 1964/redis-server 1
tcp 0 0 192.168.4.51:6351 0.0.0.0:* LISTEN 1964/redis-server 1
#16351为集群通信端口,默认端口服务+10000
---------------------------------------------------------------
或者也可以写个脚本
[root@host51 ~]# vim redis2.sh
#!/bin/bash
sed -i '815s/# //' /etc/redis/6379.conf
sed -i '823s/# //' /etc/redis/6379.conf
sed -i '829s/# //' /etc/redis/6379.conf
sed -i '829s/15000/5000/' /etc/redis/6379.conf
redis-cli -h 192.168.4.$1 -p 63$1 shutdown
/etc/init.d/redis_6379 start
netstat -antulp | grep redis-server
-----------------------------------------------------------------
#########
如果查不到pid号,可以进行如下操作
[root@host51 ~]# ls /var/run/
auditd.pid dbus initramfs mysqld setrans tuned
chrony dmeventd-client lock netreport sshd.pid udev
chronyd.pid dmeventd-server log NetworkManager sudo user
console ebtables.lock lvm plymouth syslogd.pid utmp
crond.pid faillock lvmetad.pid redis_6379.pid systemd vmware
cron.reboot firewalld mount sepermit tmpfiles.d xtables.lock
[root@host51 ~]# netstat -nutlp | grep 6351 #查找pid号
tcp 0 0 192.168.4.51:16351 0.0.0.0:* LISTEN 1964/redis-server 1
tcp 0 0 192.168.4.51:6351 0.0.0.0:* LISTEN 1964/redis-server 1
[root@host51 ~]# ls /proc | grep 1964 #pid号文件在这里
1964
或者先停止redis服务,再重新启动
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 shutdown
[root@host51 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351
########
****************************************
3.在管理主机57创建集群(记得关防火墙)
[root@host57 ~]# redis-trib.rb create --replicas 1 192.168.4.51:6351 192.168.4.52:6352 192.168.4.53:6353 192.168.4.54:6354 192.168.4.55:6355 192.168.4.56:6356
#replicas 1,定义从服务器个数,给每台主服务器配置一台从服务器,加入后面写3,则要准备12台redis服务器才可以
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters: #选举出三个主服务器
192.168.4.51:6351
192.168.4.52:6352
192.168.4.53:6353
Adding replica 192.168.4.55:6355 to 192.168.4.51:6351
Adding replica 192.168.4.56:6356 to 192.168.4.52:6352
Adding replica 192.168.4.54:6354 to 192.168.4.53:6353
M: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351 #M代表主服务器
slots:0-5460 (5461 slots) master
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354 #S代表从服务器,replicates代表4.54的主服务器是以ce822为结尾的主服务器
replicates 7747411ab79e16a5493e66d4a46515bd110ce822
S: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355
replicates 25991ad67a2e5b7c570ca87eed3140432cc78eee
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356
replicates 23b46dfa5997798b02837b365122d0315a48a84e
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.4.51:6351)
M: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354
slots: (0 slots) slave
replicates 7747411ab79e16a5493e66d4a46515bd110ce822
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356
slots: (0 slots) slave
replicates 23b46dfa5997798b02837b365122d0315a48a84e
S: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355
slots: (0 slots) slave
replicates 25991ad67a2e5b7c570ca87eed3140432cc78eee
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352
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. #共占用了16384个hash槽,从服务器不占用槽,主服务器写入数据,从服务器自动同步
*********************************************
4.在任意一台redis服务器本机,查看集群信息
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> cluster info #查看集群信息
cluster_state:ok #代表51本机已经在集群中
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6 #集群中有6台主机
cluster_size:3 #有三台主服务器
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:487
cluster_stats_messages_pong_sent:587
cluster_stats_messages_sent:1074
cluster_stats_messages_ping_received:582
cluster_stats_messages_pong_received:487
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1074
查看集群中的节点信息成员列表
192.168.4.51:6351> cluster nodes #查看集群节点信息,带myself字样是本机自己
8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354@16354 slave 7747411ab79e16a5493e66d4a46515bd110ce822 0 1582890996432 4 connected
75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356@16356 slave 23b46dfa5997798b02837b365122d0315a48a84e 0 1582890997000 6 connected
b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355@16355 slave 25991ad67a2e5b7c570ca87eed3140432cc78eee 0 1582890998000 5 connected
7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353@16353 master - 0 1582890998449 3 connected 10923-16383
25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351@16351 myself,master - 0 1582890998000 1 connected 0-5460
23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352@16352 master - 0 1582890997442 2 connected 5461-10922
[root@host51 ~]# cat /var/lib/redis/6379/nodes-6379.conf #存储集群信息的配置文件,在Redis中查看成员列表调用的就是该文件
8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354@16354 slave 7747411ab79e16a5493e66d4a46515bd110ce822 0 1582890552000 4 connected
75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356@16356 slave 23b46dfa5997798b02837b365122d0315a48a84e 0 1582890552000 6 connected
b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355@16355 slave 25991ad67a2e5b7c570ca87eed3140432cc78eee 0 1582890551442 5 connected
7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353@16353 master - 0 1582890552550 3 connected 10923-16383
25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351@16351 myself,master - 0 1582890551000 1 connected 0-5460
23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352@16352 master - 0 1582890552450 2 connected 5461-10922
vars currentEpoch 6 lastVoteEpoch 0
在管理主机上查看集群统计信息
[root@host57 ~]# redis-trib.rb info 192.168.4.51:6351
192.168.4.51:6351 (25991ad6...) -> 0 keys | 5461 slots | 1 slaves.
192.168.4.53:6353 (7747411a...) -> 0 keys | 5461 slots | 1 slaves.
192.168.4.52:6352 (23b46dfa...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
在管理主机上看集群的详细信息
[root@host57 ~]# redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354
slots: (0 slots) slave
replicates 7747411ab79e16a5493e66d4a46515bd110ce822
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356
slots: (0 slots) slave
replicates 23b46dfa5997798b02837b365122d0315a48a84e
S: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355
slots: (0 slots) slave
replicates 25991ad67a2e5b7c570ca87eed3140432cc78eee
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352
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.
如果主服务器坏了,那么对应的从服务器就会自动变成主
*****************************************************
还原操作
在每一台redis服务器上作如下操作:
1)停止redis服务
2)清空数据库目录 rm -rf /var/lib/redis/6379/*
3)检查集群功能是否能启用
4)启动redis服务
5)查看端口,进程
6)在管理主机上创建集群
*****************************************************
5.访问集群
在客户端可以连接集群中任意一台redis服务器,也可以在redis服务器本机访问
在任意一台redis服务器上写数据,数据都会存入相应的hash槽内,这就是分片式存储
[root@host51 ~]# redis-cli -c -h 192.168.4.56 -p 6356 #在51主机访问56主机的redis服务
192.168.4.56:6356> keys *
(empty list or set)
192.168.4.56:6356> set a 456 #在56服务器存入变量a,发现数据存到了53的服务器上,因为redis高可用集群会自动寻找hash值对应的槽,然后放入对应的服务器中
-> Redirected to slot [15495] located at 192.168.4.53:6353
OK
192.168.4.53:6353> keys * #此时就会发现在53服务器上出现刚刚存入的变量,而根据redis集群信息查看,54主机是53主机的从服务器,所以此时,54主机也应该会有该数据
1) "a"
192.168.4.53:6353> exit
[root@host51 ~]# redis-cli -c -h 192.168.4.54 -p 6354 #登录到54的服务器
192.168.4.54:6354> keys * #查看,确有刚刚存入的新数据
1) "a"
192.168.4.54:6354> set b 784 #再次赋新的变量,会对应存入相应的hash槽
-> Redirected to slot [3300] located at 192.168.4.51:6351
OK
192.168.4.51:6351> exit
[root@host51 ~]# redis-cli -c -h 192.168.4.55 -p 6355
192.168.4.55:6355> keys * #55主机是51主机的从服务器,此时存入51主机的数据会自动同步至55主机
1) "b"
192.168.4.55:6355> set x 23 #存数据时不能mset存多个,因为会找不到hash值对应的槽位
-> Redirected to slot [16287] located at 192.168.4.53:6353
OK
192.168.4.53:6353> keys *
1) "a"
2) "x"
192.168.4.53:6353> get b #此时,如果取一个不属于当前主机的数据,也一举可以取到
-> Redirected to slot [3300] located at 192.168.4.51:6351
"784"
192.168.4.51:6351>
管理集群
- 测试管理集群功能
1.故障切换测试
先查看当前集群中的主服务器
[root@host57 ~]# redis-trib.rb info 192.168.4.51:6351
192.168.4.51:6351 (25991ad6...) -> 1 keys | 5461 slots | 1 slaves.
192.168.4.53:6353 (7747411a...) -> 2 keys | 5461 slots | 1 slaves.
192.168.4.52:6352 (23b46dfa...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 3 keys in 3 masters.
0.00 keys per slot on average.
[root@host57 ~]# redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354
slots: (0 slots) slave
replicates 7747411ab79e16a5493e66d4a46515bd110ce822
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356
slots: (0 slots) slave
replicates 23b46dfa5997798b02837b365122d0315a48a84e
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355
slots: (0 slots) slave
replicates 25991ad67a2e5b7c570ca87eed3140432cc78eee
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
-----------------------------------------------------------------------
停止master主机的redis服务,master宕机后对应的slave自动被选举为master;
[root@host51 ~]# ss -nutlp | grep 6351
tcp LISTEN 0 128 192.168.4.51:16351 *:* users:(("redis-server",pid=1099,fd=8))
tcp LISTEN 0 128 192.168.4.51:6351 *:* users:(("redis-server",pid=1099,fd=6))
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 shutdown #把51主机的服务停掉,根据集群详细信息知道,55主机是51主机的从服务器,那么此时51主机挂掉,55主机就会自动变为主服务器
[root@host51 ~]# ss -nutlp | grep 6351
[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #此时55主机变成了主,并且没有从服务器
192.168.4.52:6352 (23b46dfa...) -> 0 keys | 5462 slots | 1 slaves.
192.168.4.55:6355 (b69bbe09...) -> 1 keys | 5461 slots | 0 slaves.
192.168.4.53:6353 (7747411a...) -> 2 keys | 5461 slots | 1 slaves.
[OK] 3 keys in 3 masters.
0.00 keys per slot on average.
在客户端连接任意一台55主机写入数据
[root@host56 ~]# redis-cli -c -h 192.168.4.55 -p 6355
192.168.4.55:6355> set ar 66
-> Redirected to slot [9934] located at 192.168.4.52:6352
OK
192.168.4.52:6352> set kk 4444
-> Redirected to slot [2589] located at 192.168.4.55:6355
OK
192.168.4.55:6355> keys *
1) "kk"
2) "b"
192.168.4.55:6355> set pp 4575
-> Redirected to slot [14030] located at 192.168.4.53:6353
OK
192.168.4.53:6353> keys *
1) "a"
2) "pp"
3) "x"
192.168.4.53:6353> set ii 11123
-> Redirected to slot [3133] located at 192.168.4.55:6355
OK
192.168.4.55:6355> keys * #可以看到55主机多了两个变量
1) "ii"
2) "kk"
3) "b"
-------------------------------------------------------------------------
源master启动后,会自动配置为当前master的slave;
[root@host51 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host51 ~]# ss -nutlp | grep 6351
tcp LISTEN 0 128 192.168.4.51:16351 *:* users:(("redis-server",pid=1748,fd=8))
tcp LISTEN 0 128 192.168.4.51:6351 *:* users:(("redis-server",pid=1748,fd=6))
[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #此时可以看到,55主机有了从服务器
192.168.4.52:6352 (23b46dfa...) -> 1 keys | 5462 slots | 1 slaves.
192.168.4.55:6355 (b69bbe09...) -> 3 keys | 5461 slots | 1 slaves.
192.168.4.53:6353 (7747411a...) -> 3 keys | 5461 slots | 1 slaves.
[OK] 7 keys in 3 masters.
0.00 keys per slot on average.
[root@host57 ~]# redis-trib.rb check 192.168.4.52:6352 #check检查发现,51主机变成了55主机的从
>>> Performing Cluster Check (using node 192.168.4.52:6352)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356
slots: (0 slots) slave
replicates 23b46dfa5997798b02837b365122d0315a48a84e
S: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351
slots: (0 slots) slave
replicates b69bbe093c47927c88fa3eaf10cce898e4593cc5
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354
slots: (0 slots) slave
replicates 7747411ab79e16a5493e66d4a46515bd110ce822
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
连接51主机的服务器,会发现,在55主机上写入的数据自动同步到了51主机上
[root@host51 ~]# redis-cli -c -h 192.168.4.51 -p 6351
192.168.4.51:6351> keys *
1) "b"
2) "kk"
3) "ii"
用Redis集群存取数据时一定要确保每组主从服务器至少有一个是活着的,否则整个集群都会崩溃
- 添加服务器
存储空间不足,或者当前已有的服务器处理不了客户并发访问量时,可以添加新的服务器到集群中
1.添加master服务器
1)部署一台新redis服务器host58,运行服务并启用集群配置
[root@host58 opt]# ls /opt
redis-4.0.8.tar.gz
[root@host58 opt]# yum -y install gcc #先安装gcc依赖
[root@host58 opt]#tar -zvxf redis-4.0.8.tar.gz
[root@host58 opt]#cd redis-4.0.8/
[root@host58 redis-4.0.8]#make && make install
[root@host58 redis-4.0.8]# ./utils/install_server.sh #一路回车
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
[root@host58 redis-4.0.8]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@host58 redis-4.0.8]# vim /etc/redis/6379.conf
70 bind 192.168.4.58
93 port 6358
815 cluster-enabled yes #启用集群
823 cluster-config-file nodes-6379.conf #存储集群信息文件
829 cluster-node-timeout 5000
[root@host58 redis-4.0.8]# /etc/init.d/redis_6379 start #起服务
Starting Redis server...
[root@host58 redis-4.0.8]# ss -nutlp | grep 6358
tcp LISTEN 0 128 192.168.4.58:16358 *:* users:(("redis-server",pid=4940,fd=8))
tcp LISTEN 0 128 192.168.4.58:6358 *:* users:(("redis-server",pid=4940,fd=6))
[root@host58 redis-4.0.8]# redis-cli -h 192.168.4.58 -p 6358
192.168.4.58:6358> ping
PONG
192.168.4.58:6358> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
192.168.4.58:6358> exit
2)在管理主机57,执行添加新服务的操作,添加master主机,分配hash槽(slots)
[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #先查看当前的主服务器
192.168.4.52:6352 (23b46dfa...) -> 1 keys | 5462 slots | 1 slaves.
192.168.4.55:6355 (b69bbe09...) -> 3 keys | 5461 slots | 1 slaves.
192.168.4.53:6353 (7747411a...) -> 3 keys | 5461 slots | 1 slaves.
[OK] 7 keys in 3 masters.
[root@host57 ~]# redis-trib.rb add-node 192.168.4.58:6358 192.168.4.52:6352 #添加新的集群节点
[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #查看集群信息,58主机成功添加为主服务器,但没有hash slots槽
192.168.4.52:6352 (23b46dfa...) -> 1 keys | 5462 slots | 1 slaves.
192.168.4.55:6355 (b69bbe09...) -> 3 keys | 5461 slots | 1 slaves.
192.168.4.53:6353 (7747411a...) -> 3 keys | 5461 slots | 1 slaves.
192.168.4.58:6358 (5e0d1807...) -> 0 keys | 0 slots | 0 slaves.
[OK] 7 keys in 4 masters.
0.00 keys per slot on average.
3)重新分片(hash slot)
[root@host57 ~]# redis-trib.rb reshard 192.168.4.52:6352 #重新分片
...
How many slots do you want to move (from 1 to 16384)? 4096 #移出hash槽个数,拿出4096个hash槽给主机58
What is the receiving node ID? 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 #接收hash槽主机ID
Type 'all' to use all the nodes as source nodes for the hash slots. #键入“all”将所有节点用作hash槽的源节点。
Type 'done' once you entered all the source nodes IDs. #输入所有源节点ID后,键入“done”。
Source node #1: all #移出hash槽主机ID,从所有主服务器移除slots
Do you want to proceed with the proposed reshard plan (yes/no)? yes #同意配置否
[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #查看集群信息
192.168.4.52:6352 (23b46dfa...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.55:6355 (b69bbe09...) -> 3 keys | 4096 slots | 1 slaves.
192.168.4.53:6353 (7747411a...) -> 3 keys | 4096 slots | 1 slaves.
192.168.4.58:6358 (5e0d1807...) -> 0 keys | 4096 slots | 0 slaves. #主服务器58hash槽4096个
[OK] 7 keys in 4 masters.
0.00 keys per slot on average.
[root@host57 ~]# redis-trib.rb check 192.168.4.52:6352 #检测集群
>>> Performing Cluster Check (using node 192.168.4.52:6352)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352
slots:0-1365,8193-10922 (4096 slots) master
1 additional replica(s)
M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355
slots:1366-5461 (4096 slots) master
1 additional replica(s)
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353
slots:5462-6826,13653-16383 (4096 slots) master
1 additional replica(s)
M: 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 192.168.4.58:6358
slots:6827-8192,10923-13652 (4096 slots) master
1 additional replica(s)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356
slots: (0 slots) slave
replicates 23b46dfa5997798b02837b365122d0315a48a84e
S: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351
slots: (0 slots) slave
replicates b69bbe093c47927c88fa3eaf10cce898e4593cc5
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354
slots: (0 slots) slave
replicates 7747411ab79e16a5493e66d4a46515bd110ce822
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
-------------------------------------------------------------------------
2.添加slave角色主机到集群里
1)部署新的redis服务器192.168.4.59,运行redis服务,启用集群功能
[root@host59 opt]# ls
redis-4.0.8.tar.gz
[root@host59 opt]# yum -y install gcc
[root@host59 opt]# tar -zvxf redis-4.0.8.tar.gz
[root@host59 opt]# cd redis-4.0.8/
[root@host59 redis-4.0.8]# make && make install && ./utils/install_server.sh
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
[root@host59 redis-4.0.8]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@host59 redis-4.0.8]# vim /etc/redis/6379.conf
70 bind 192.168.4.59
93 port 6359
815 cluster-enabled yes #启用集群
823 cluster-config-file nodes-6379.conf #存储集群信息文件
829 cluster-node-timeout 5000
[root@host59 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host59 redis-4.0.8]# ss -nutlp | grep 6359
tcp LISTEN 0 128 192.168.4.59:16359 *:* users:(("redis-server",pid=14657,fd=8))
tcp LISTEN 0 128 192.168.4.59:6359 *:* users:(("redis-server",pid=14657,fd=6))
[root@host59 redis-4.0.8]# redis-cli -h 192.168.4.59 -p 6359
192.168.4.59:6359> ping
PONG
192.168.4.59:6359> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
2)在管理主机添加slave角色主机
[root@host57 ~]# redis-trib.rb add-node --slave 192.168.4.59:6359 192.168.4.58:6358
#如果不指定主节点的id的话,会把新节点随机添加为从节点最少的主库
[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #查看信息
192.168.4.52:6352 (23b46dfa...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.55:6355 (b69bbe09...) -> 3 keys | 4096 slots | 1 slaves.
192.168.4.53:6353 (7747411a...) -> 3 keys | 4096 slots | 1 slaves.
192.168.4.58:6358 (5e0d1807...) -> 0 keys | 4096 slots | 1 slaves. #58主机有一个从服务器
[OK] 7 keys in 4 masters.
0.00 keys per slot on average.
[root@host57 ~]# redis-trib.rb check 192.168.4.52:6352 #检测集群
>>> Performing Cluster Check (using node 192.168.4.52:6352)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352
slots:0-1365,8193-10922 (4096 slots) master
1 additional replica(s)
M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355
slots:1366-5461 (4096 slots) master
1 additional replica(s)
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353
slots:5462-6826,13653-16383 (4096 slots) master
1 additional replica(s)
M: 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 192.168.4.58:6358
slots:6827-8192,10923-13652 (4096 slots) master
1 additional replica(s)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356
slots: (0 slots) slave
replicates 23b46dfa5997798b02837b365122d0315a48a84e
S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359
slots: (0 slots) slave
replicates 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58
S: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351
slots: (0 slots) slave
replicates b69bbe093c47927c88fa3eaf10cce898e4593cc5
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354
slots: (0 slots) slave
replicates 7747411ab79e16a5493e66d4a46515bd110ce822
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
如果reshard分配不平均的话,可以执行以下操作:
[root@host57 ~]# redis-trib.rb rebalance 192.168.4.52:6352
- 移出服务器
1)移除slave主机
从服务器没有hash槽,直接移除即可,一出事指定从服务器id值
[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #先查看当前集群中的主服务器
192.168.4.52:6352 (23b46dfa...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.55:6355 (b69bbe09...) -> 3 keys | 4096 slots | 1 slaves.
192.168.4.53:6353 (7747411a...) -> 3 keys | 4096 slots | 1 slaves.
192.168.4.58:6358 (5e0d1807...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 7 keys in 4 masters.
0.00 keys per slot on average.
[root@host57 ~]# redis-trib.rb check 192.168.4.52:6352 #查看主服务器对应的从服务器
>>> Performing Cluster Check (using node 192.168.4.52:6352)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352
slots:0-1365,8193-10922 (4096 slots) master
1 additional replica(s)
M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355
slots:1366-5461 (4096 slots) master
1 additional replica(s)
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353
slots:5462-6826,13653-16383 (4096 slots) master
1 additional replica(s)
M: 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 192.168.4.58:6358
slots:6827-8192,10923-13652 (4096 slots) master
1 additional replica(s)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356
slots: (0 slots) slave
replicates 23b46dfa5997798b02837b365122d0315a48a84e
S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359
slots: (0 slots) slave
replicates 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58
S: 25991ad67a2e5b7c570ca87eed3140432cc78eee 192.168.4.51:6351
slots: (0 slots) slave
replicates b69bbe093c47927c88fa3eaf10cce898e4593cc5
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354
slots: (0 slots) slave
replicates 7747411ab79e16a5493e66d4a46515bd110ce822
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@host57 ~]# redis-trib.rb del-node 192.168.4.52:6352 25991ad67a2e5b7c570ca87eed3140432cc78eee #将51主机移除
>>> Removing node 25991ad67a2e5b7c570ca87eed3140432cc78eee from cluster 192.168.4.52:6352
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node. #注:移除时也会同时将该主机的redis服务关闭
[root@host51 ~]# netstat -nutlp | grep redis-server #此时去51主机查看没有服务端口,再次开启服务也不会再存在于集群中
[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #可以看到55主机没有从服务器了
192.168.4.52:6352 (23b46dfa...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.55:6355 (b69bbe09...) -> 3 keys | 4096 slots | 0 slaves.
192.168.4.53:6353 (7747411a...) -> 3 keys | 4096 slots | 1 slaves.
192.168.4.58:6358 (5e0d1807...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 7 keys in 4 masters.
0.00 keys per slot on average.
[root@host51 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #再次重新开启服务后,55主机依旧没有从服务器
192.168.4.52:6352 (23b46dfa...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.55:6355 (b69bbe09...) -> 3 keys | 4096 slots | 0 slaves.
192.168.4.53:6353 (7747411a...) -> 3 keys | 4096 slots | 1 slaves.
192.168.4.58:6358 (5e0d1807...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 7 keys in 4 masters.
0.00 keys per slot on average.
2)把master服务器移除集群
①在管理主机,先删除master服务器占用的hash槽
[root@host57 ~]# redis-trib.rb reshard 192.168.4.52:6352
How many slots do you want to move (from 1 to 16384)? 4096 #移除4096个数槽
What is the receiving node ID? b69bbe093c47927c88fa3eaf10cce898e4593cc5 #要移动给谁的id即目标主机,可以随便写一个master的id(此处为master55的id)
Type 'all' to use all the nodes as source nodes for the hash slots. #键入“all”将所有节点用作hash槽的源节点。
Type 'done' once you entered all the source nodes IDs. #输入所有源节点ID后,键入“done”。
Source node #1:5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 #从谁那移动即援助局(这里写4.58的id)
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? yes #同意配置否
②在管理主机查看集群信息
[root@host57 ~]# redis-trib.rb del-node 192.168.4.52:6352 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 #删除主机,删除谁就加谁的id
>>> Removing node 5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 from cluster 192.168.4.52:6352
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #此时已经没有了58主机
192.168.4.52:6352 (23b46dfa...) -> 1 keys | 4096 slots | 1 slaves.
192.168.4.55:6355 (b69bbe09...) -> 3 keys | 8192 slots | 1 slaves.
192.168.4.53:6353 (7747411a...) -> 3 keys | 4096 slots | 1 slaves.
[OK] 7 keys in 3 masters.
0.00 keys per slot on average.
[root@host57 ~]# redis-trib.rb rebalance 192.168.4.52:6352 #将每个主机的hash槽平均
[root@host57 ~]# redis-trib.rb info 192.168.4.52:6352 #查看集群信息
192.168.4.52:6352 (23b46dfa...) -> 2 keys | 5462 slots | 1 slaves.
192.168.4.55:6355 (b69bbe09...) -> 0 keys | 5461 slots | 1 slaves.
192.168.4.53:6353 (7747411a...) -> 5 keys | 5461 slots | 1 slaves.
[OK] 7 keys in 3 masters. #主服务器个数3台,没有58
0.00 keys per slot on average.
[root@host57 ~]# redis-trib.rb check 192.168.4.52:6352 #此时59主机就自动变成了没有从服务器的55主机的从服务器
>>> Performing Cluster Check (using node 192.168.4.52:6352)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352
slots:0-2731,8193-10922 (5462 slots) master
1 additional replica(s)
M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355
slots:4097-5461,6827-8192,10923-13652 (5461 slots) master
1 additional replica(s)
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353
slots:2732-4096,5462-6826,13653-16383 (5461 slots) master
1 additional replica(s)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356
slots: (0 slots) slave
replicates 23b46dfa5997798b02837b365122d0315a48a84e
S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359
slots: (0 slots) slave
replicates b69bbe093c47927c88fa3eaf10cce898e4593cc5
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354
slots: (0 slots) slave
replicates 7747411ab79e16a5493e66d4a46515bd110ce822
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
- 把删除的主机再添加到集群中
[root@host57 ~]# redis-trib.rb add-node 192.168.4.58:6358 192.168.4.52:6352
>>> Adding node 192.168.4.58:6358 to cluster 192.168.4.52:6352
>>> Performing Cluster Check (using node 192.168.4.52:6352)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352
slots:0-2731,8193-10922 (5462 slots) master
1 additional replica(s)
M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355
slots:4097-5461,6827-8192,10923-13652 (5461 slots) master
1 additional replica(s)
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353
slots:2732-4096,5462-6826,13653-16383 (5461 slots) master
1 additional replica(s)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356
slots: (0 slots) slave
replicates 23b46dfa5997798b02837b365122d0315a48a84e
S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359
slots: (0 slots) slave
replicates b69bbe093c47927c88fa3eaf10cce898e4593cc5
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354
slots: (0 slots) slave
replicates 7747411ab79e16a5493e66d4a46515bd110ce822
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[ERR] Node 192.168.4.58:6358 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
#192.168.4.58主机不是空的状态,并且集群已经知道了该节点
解决办法:
1.可以将/var/lib/redis/6379/nodes-6379.conf删除,然后将该主机的redis服务关闭重启
[root@host58 redis-4.0.8]# cat /var/lib/redis/6379/nodes-6379.conf
a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359@16359 slave b69bbe093c47927c88fa3eaf10cce898e4593cc5 0 1582909828964 12 connected
8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354@16354 slave 7747411ab79e16a5493e66d4a46515bd110ce822 0 1582909828964 14 connected
75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356@16356 slave 23b46dfa5997798b02837b365122d0315a48a84e 0 1582909829000 13 connected
7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353@16353 master - 0 1582909828964 14 connected 2732-4096 5462-6826 13653-16383
b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355@16355 master - 0 1582909829000 12 connected 4097-5461 6827-8192 10923-13652
5e0d18070c3fecdfcb3d560e4550f200ab8e0d58 192.168.4.58:6358@16358 myself,master - 0 1582909828957 8 connected
23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352@16352 master - 0 1582909828964 13 connected 0-2731 8193-10922
vars currentEpoch 14 lastVoteEpoch 0
2.或者可以直接在redis服务器内执行cluster reset重置
[root@host58 redis-4.0.8]# redis-cli -h 192.168.4.58 -p 6358
192.168.4.58:6358> cluster info #此时查看,发现该主机还存在集群节点
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:7
cluster_size:3
cluster_current_epoch:14
cluster_my_epoch:8
cluster_stats_messages_ping_sent:631
cluster_stats_messages_sent:631
cluster_stats_messages_pong_received:631
cluster_stats_messages_received:631
192.168.4.58:6358> cluster reset #重置集群信息
192.168.4.58:6358> cluster info #再次查看
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:14
cluster_my_epoch:8
cluster_stats_messages_ping_sent:719
cluster_stats_messages_sent:719
cluster_stats_messages_pong_received:719
cluster_stats_messages_received:719
[root@host57 ~]# redis-trib.rb add-node 192.168.4.58:6358 192.168.4.52:6352
>>> Adding node 192.168.4.58:6358 to cluster 192.168.4.52:6352
>>> Performing Cluster Check (using node 192.168.4.52:6352)
M: 23b46dfa5997798b02837b365122d0315a48a84e 192.168.4.52:6352
slots:0-2731,8193-10922 (5462 slots) master
1 additional replica(s)
M: b69bbe093c47927c88fa3eaf10cce898e4593cc5 192.168.4.55:6355
slots:4097-5461,6827-8192,10923-13652 (5461 slots) master
1 additional replica(s)
M: 7747411ab79e16a5493e66d4a46515bd110ce822 192.168.4.53:6353
slots:2732-4096,5462-6826,13653-16383 (5461 slots) master
1 additional replica(s)
S: 75917bffed4334444e2cf50165b9b03652f6bdc2 192.168.4.56:6356
slots: (0 slots) slave
replicates 23b46dfa5997798b02837b365122d0315a48a84e
S: a33e7cd079dceefb8d8fc699947e9d17a114515e 192.168.4.59:6359
slots: (0 slots) slave
replicates b69bbe093c47927c88fa3eaf10cce898e4593cc5
S: 8da07552c93155e4ac83c63bf1ee717cb15610a0 192.168.4.54:6354
slots: (0 slots) slave
replicates 7747411ab79e16a5493e66d4a46515bd110ce822
[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:6358 to make it join the cluster.
[OK] New node added correctly.
[root@host57 ~]# redis-trib.rb info 192.168.4.58:6358
192.168.4.58:6358 (5e0d1807...) -> 0 keys | 0 slots | 0 slaves.
192.168.4.53:6353 (7747411a...) -> 5 keys | 5461 slots | 1 slaves.
192.168.4.55:6355 (b69bbe09...) -> 0 keys | 5461 slots | 1 slaves.
192.168.4.52:6352 (23b46dfa...) -> 2 keys | 5462 slots | 1 slaves.
[OK] 7 keys in 4 masters.
0.00 keys per slot on average.
- 把集群中的主机还原成独立的数据库服务器
- 停止redis服务
- 清空数据库目录
- 注释集群功能
- 启动redis服务
- 查看端口只有服务端口,没有集群端口
- cluster info 显示enabled
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351 shutdown
[root@host51 ~]# rm -rf /var/lib/redis/6379/*
[root@host51 ~]# vim /etc/redis/6379.conf
815 #cluster-enabled yes
823 #cluster-config-file nodes-6379.conf
829 #cluster-node-timeout 5000
[root@host51 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host51 ~]# netstat -nutlp | grep redis-server
tcp 0 0 192.168.4.51:6351 0.0.0.0:* LISTEN 2005/redis-server 1
[root@host51 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> cluster info
ERR This instance has cluster support disabled
[root@host51 ~]#sed -i "815s/^/#/ ; 823s/^/#/ ; 829s/^/#/" /etc/redis/6379.conf
部署redis脚本
#!/bin/bash
rpm -q gcc
if [ $? -eq 0 ];then
echo "gcc已经安装"
else
yum -y install gcc expect
fi
redis(){
tar -zxf redis-4.0.8.tar.gz
cd redis-4.0.8/
make
make install
cd utils/
expect <<ok
spawn ./install_server.sh
expect "Please select the redis port for this instance" {send "exit\r"}
expect "Please select the redis config file name " {send "exit\r"}
expect "Please select the redis log file name " {send "exit\r"}
expect "select the data directory for this instance " {send "exit\r"}
expect "Please select the redis executable path " {send "exit\r"}
expect "Is this ok? Then press ENTER to go on or Ctrl-C to abort." {send "exit\r"}
expect "##" {send "exit\r"}
expect "##" {send "exit\r"}
ok
}
redis
echo "redis服务已安装完成"