BcLinux-Redis-集群(cluster)模式安装配置
IP:192.168.0.2 8081、8082
IP:192.168.0.3 8081、8082
IP:192.168.0.4 8081、8082
1、三个节点同样操作
[root@server-1 setup]# yum install tcl
或者:
wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz
tar xzvf tcl8.6.1-src.tar.gz -C /home/local/
cd /home/local/tcl8.6.1/unix/
./configure
make
make install
[yyxt@o-0034 ~]$ tar -zxvf redis-6.2.7.tar.gz
[yyxt@o-0034 ~]$ cd redis-6.2.7/
[yyxt@o-0034 redis-6.2.7]$ pwd
/home/test/redis-6.2.7
[yyxt@o-0034 redis-6.2.7]$ make
[yyxt@o-0034 redis-6.2.7]$ make test
[yyxt@o-0034 redis-6.2.7]$ make install PREFIX=/home/test/redis_6.2.7
2、配置每个节点
在server-1上:
[root@server-1 redis_6.2.7]# mkdir -p /home/test/redis_6.2.7/redis-cluster/{8081,8082} //在/home/test/redis_6.2.7目录下新建一个redis-cluster文件夹,并在里面新建2个文件夹,用来区分节点
[root@server-1 redis_6.2.7]# cp redis.conf /home/test/redis_6.2.7/redis-cluster/8081/redis-8081.conf
[root@server-1 redis_6.2.7]# vim /home/test/redis_6.2.7/redis-cluster/8081/redis-8081.conf //必要需要修改如下,其他细化优化的配置项可暂时保持默认
bind 192.168.0.2 #本机IP
port 8081 #改为设定的端口
daemonize yes #后台启动
masterauth gd@Redis123 #由于每个节点都开启了密码认证,因此又增加 masterauth 配置,使得从机可以登录到主机上。
requirepass gd@Redis123 #表示登录该 Redis 实例所需的密码
cluster-enabled yes #启动集群模式
protected-mode: #由于配置了密码登录,这里将 protected-mode 设置为 no 关闭保护模式。
cluster-config-file nodes-8081.conf
cluster-node-timeout 15000
pidfile /home/test/redis_6.2.7/redis_8081.pid
logfile "/home/test/redis_6.2.7/redis_8081.log"
appendonly yes
save 900 1
save 300 10
save 60 10000
[root@server-1 redis_6.2.7]# cp redis.conf /home/test/redis_6.2.7/redis-cluster/8082/redis-8082.conf
[root@server-1 redis_6.2.7]# vim /home/test/redis_6.2.7/redis-cluster/8082/redis-8082.conf
bind 192.168.0.2
port 8082
daemonize yes
masterauth gd@Redis123
requirepass gd@Redis123
cluster-enabled yes
cluster-config-file nodes-8082.conf
cluster-node-timeout 15000
pidfile /home/test/redis_6.2.7/redis_8082.pid
logfile "/home/test/redis_6.2.7/redis_8082.log"
appendonly yes
save 900 1
save 300 10
save 60 10000
在server-2上:
[root@server-1 redis_6.2.7]# mkdir -p /home/test/redis_6.2.7/redis-cluster/{8081,8082} //在/home/test/redis_6.2.7目录下新建一个redis-cluster文件夹,并在里面新建2个文件夹,用来区分节点
[root@server-1 redis_6.2.7]# cp redis.conf /home/test/redis_6.2.7/redis-cluster/8081/redis-8081.conf
[root@server-1 redis_6.2.7]# vim /home/test/redis_6.2.7/redis-cluster/8081/redis-8081.conf //必要需要修改如下,其他细化优化的配置项可暂时保持默认
bind 192.168.0.3
port 8081
daemonize yes
masterauth gd@Redis123
requirepass gd@Redis123
cluster-enabled yes
cluster-config-file nodes-8081.conf
cluster-node-timeout 15000
pidfile /home/test/redis_6.2.7/redis_8081.pid
logfile "/home/test/redis_6.2.7/redis_8081.log"
appendonly yes
save 900 1
save 300 10
save 60 10000
[root@server-1 redis_6.2.7]# cp redis.conf /home/test/redis_6.2.7/redis-cluster/8082/redis-8082.conf
[root@server-1 redis_6.2.7]# vim /home/test/redis_6.2.7/redis-cluster/8082/redis-8082.conf
bind 192.168.0.3
port 8082
daemonize yes
masterauth gd@Redis123
requirepass gd@Redis123
cluster-enabled yes
cluster-config-file nodes-8082.conf
cluster-node-timeout 15000
pidfile /home/test/redis_6.2.7/redis_8082.pid
logfile "/home/test/redis_6.2.7/redis_8082.log"
appendonly yes
save 900 1
save 300 10
save 60 10000
在server-3上:
[root@server-1 redis_6.2.7]# mkdir -p /home/test/redis_6.2.7/redis-cluster/{8081,8082} //在/home/test/redis_6.2.7目录下新建一个redis-cluster文件夹,并在里面新建2个文件夹,用来区分节点
[root@server-1 redis_6.2.7]# cp redis.conf /home/test/redis_6.2.7/redis-cluster/8081/redis-8081.conf
[root@server-1 redis_6.2.7]# vim /home/test/redis_6.2.7/redis-cluster/8081/redis-8081.conf //必要需要修改如下,其他细化优化的配置项可暂时保持默认
bind 192.168.0.4
port 8081
daemonize yes
masterauth gd@Redis123
requirepass gd@Redis123
cluster-enabled yes
cluster-config-file nodes-8081.conf
cluster-node-timeout 15000
pidfile /home/test/redis_6.2.7/redis_8081.pid
logfile "/home/test/redis_6.2.7/redis_8081.log"
appendonly yes
save 900 1
save 300 10
save 60 10000
[root@server-1 redis_6.2.7]# cp redis.conf /home/test/redis_6.2.7/redis-cluster/8082/redis-8082.conf
[root@server-1 redis_6.2.7]# vim /home/test/redis_6.2.7/redis-cluster/8082/redis-8082.conf
bind 192.168.0.4
port 8082
daemonize yes
masterauth gd@Redis123
requirepass gd@Redis123
cluster-enabled yes
cluster-config-file nodes-8082.conf
cluster-node-timeout 15000
pidfile /home/test/redis_6.2.7/redis_8082.pid
logfile "/home/test/redis_6.2.7/redis_8082.log"
appendonly yes
save 900 1
save 300 10
save 60 10000
[yyxt@localhost ~/redis_6.2.7/bin]$ /home/test/redis_6.2.7/bin/./redis-server
3. 启动每个节点
在server-1上:
[root@server-1 redis_6.2.7]# /home/test/redis_6.2.7/bin/./redis-server /home/test/redis_6.2.7/redis-cluster/8081/redis-8081.conf
[root@server-1 redis_6.2.7]# /home/test/redis_6.2.7/bin/./redis-server /home/test/redis_6.2.7/redis-cluster/8082/redis-8082.conf
在server-2上:
[root@server-2 redis_6.2.7]# /home/test/redis_6.2.7/bin/./redis-server /home/test/redis_6.2.7/redis-cluster/8081/redis-8081.conf
[root@server-2 redis_6.2.7]# /home/test/redis_6.2.7/bin/./redis-server /home/test/redis_6.2.7/redis-cluster/8082/redis-8082.conf
在server-3上:
[root@server-3 redis_6.2.7]# /home/test/redis_6.2.7/bin/./redis-server /home/test/redis_6.2.7/redis-cluster/8081/redis-8081.conf
[root@server-3 redis_6.2.7]# /home/test/redis_6.2.7/bin/./redis-server /home/test/redis_6.2.7/redis-cluster/8082/redis-8082.conf
5. 创建集群
IP:192.168.0.2
cd /home/test/redis_6.2.7
[yyxt@localhost ~/redis_6.2.7]$ /home/test/redis_6.2.7/src/./redis-cli --cluster create 192.168.0.2:8081 192.168.0.3:8081 192.168.0.4:8081 192.168.0.2:8082 192.168.0.3:8082 192.168.0.4:8082 --cluster-replicas 1 -a gd@Redis123
创建集群命令:
进入到集群中某个实例下的bin目录,高版本执行
./redis-cli --cluster create --cluster-replicas 1 192.168.2.75:7001 192.168.2.75:7002 192.168.2.75:7003 192.168.2.75:7004 192.168.2.75:7005 192.168.2.75:7006
低版本执行
./redis-trib.rb create --replicas 1 192.168.2.75:7001 192.168.2.75:7002 192.168.2.75:7003 192.168.2.75:7004 192.168.2.75:7005 192.168.2.75:7006
注:replicas参数表示一个master有接个slave
然后就会按照默认的方式进行分配matser,主从关系。
[yyxt@localhost ~/redis_6.2.7]$ /home/test/redis_6.2.7/src/./redis-cli --cluster create 192.168.0.2:8081 192.168.0.3:8081 192.168.0.4:8081 192.168.0.2:8082 192.168.0.3:8082 192.168.0.4:8082 --cluster-replicas 1 -a gd@Redis123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> 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.3:8082 to 192.168.0.2:8081
Adding replica 192.168.0.4:8082 to 192.168.0.3:8081
Adding replica 192.168.0.2:8082 to 192.168.0.4:8081
M: 4ec8059cc336222dc6805c9e03913c9a5a6c1a82 192.168.0.2:8081
slots:[0-5460] (5461 slots) master
M: c2592dd2dd102515a2592368e4a172ee834dd56d 192.168.0.3:8081
slots:[5461-10922] (5462 slots) master
M: 61781a535d89518171dad34c2b73e5bf7c362852 192.168.0.4:8081
slots:[10923-16383] (5461 slots) master
S: 175116147c09c0e9035288d37d19203587ffc14d 192.168.0.2:8082
replicates 61781a535d89518171dad34c2b73e5bf7c362852
S: 308f32fe43988cae664f953b6d072c77209d5f1f 192.168.0.3:8082
replicates 4ec8059cc336222dc6805c9e03913c9a5a6c1a82
S: e3df642f88a85663ad621374cf92161ee95b0c1f 192.168.0.4:8082
replicates c2592dd2dd102515a2592368e4a172ee834dd56d
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.2:8081)
M: 4ec8059cc336222dc6805c9e03913c9a5a6c1a82 192.168.0.2:8081
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: c2592dd2dd102515a2592368e4a172ee834dd56d 192.168.0.3:8081
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: e3df642f88a85663ad621374cf92161ee95b0c1f 192.168.0.4:8082
slots: (0 slots) slave
replicates c2592dd2dd102515a2592368e4a172ee834dd56d
S: 308f32fe43988cae664f953b6d072c77209d5f1f 192.168.0.3:8082
slots: (0 slots) slave
replicates 4ec8059cc336222dc6805c9e03913c9a5a6c1a82
S: 175116147c09c0e9035288d37d19203587ffc14d 192.168.0.2:8082
slots: (0 slots) slave
replicates 61781a535d89518171dad34c2b73e5bf7c362852
M: 61781a535d89518171dad34c2b73e5bf7c362852 192.168.0.4:8081
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.
[yyxt@localhost ~/redis_6.2.7]$ redis-cli -h 192.168.0.3 -p 8081
192.168.0.3:8081> gd@Redis123
(error) ERR unknown command `gd@Redis123`, with args beginning with:
192.168.0.3:8081> auth gd@Redis123
OK
192.168.0.3:8081> cluster info
cluster_state:ok
cluster_slots_assigned:16384
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:2
cluster_stats_messages_ping_sent:119
cluster_stats_messages_pong_sent:118
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:238
cluster_stats_messages_ping_received:118
cluster_stats_messages_pong_received:120
cluster_stats_messages_received:238
192.168.0.3:8081>
6. 集群完整性检查
集群完整性指所有的槽都分配到存活的主节点上,只要16384个槽中有一个没有分配给节点则表示集群不完整。
可以用redis-trib.rb check命令检测之前创建的集群是否成功,check命令只需要给出集群中任意一个节点地址就可以
完成整个集群的检查工作,命令使用如下所示:
IP:192.168.0.2
[yyxt@localhost ~/redis_6.2.7]$ redis-cli -h 192.168.0.3 -p 8081
192.168.0.3:8081> gd@Redis123
(error) ERR unknown command `gd@Redis123`, with args beginning with:
192.168.0.3:8081> auth gd@Redis123
OK
192.168.0.3:8081> cluster info
cluster_state:ok
cluster_slots_assigned:16384
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:2
cluster_stats_messages_ping_sent:119
cluster_stats_messages_pong_sent:118
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:238
cluster_stats_messages_ping_received:118
cluster_stats_messages_pong_received:120
cluster_stats_messages_received:238
192.168.0.3:8081>
7、查看集群信息
# 直接查询,任意节点
[yyxt@localhost ~/redis_6.2.7]$ redis-cli -h 192.168.0.3 -p 8081
192.168.0.3:8081> gd@Redis123
(error) ERR unknown command `gd@Redis123`, with args beginning with:
192.168.0.3:8081> auth gd@Redis123
OK
192.168.0.3:8081> cluster info
cluster_state:ok
cluster_slots_assigned:16384
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:2
cluster_stats_messages_ping_sent:119
cluster_stats_messages_pong_sent:118
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:238
cluster_stats_messages_ping_received:118
cluster_stats_messages_pong_received:120
cluster_stats_messages_received:238
192.168.0.3:8081> quit
8、集群常用命令
[yyxt@localhost ~/redis_6.2.7]$ redis-cli --cluster info -h 192.168.0.3 -p 8081
[ERR] Wrong number of arguments for specified --cluster sub command
[yyxt@localhost ~/redis_6.2.7]$ redis-cli -h 192.168.0.3 -p 8081 -c -a gd@Redis123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.0.3:8081> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
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:2
cluster_stats_messages_ping_sent:627
cluster_stats_messages_pong_sent:584
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:1212
cluster_stats_messages_ping_received:584
cluster_stats_messages_pong_received:628
cluster_stats_messages_received:1212
192.168.0.3:8081>
# -c 表示集群支持,支持自动重定向 myredis表示密码
#进入后即可使用相关命令
集群(cluster): CLUSTER INFO # 查看集群信息 CLUSTER NODES # 查看集群所有节点信息
192.168.0.3:8081> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
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:2
cluster_stats_messages_ping_sent:627
cluster_stats_messages_pong_sent:584
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:1212
cluster_stats_messages_ping_received:584
cluster_stats_messages_pong_received:628
cluster_stats_messages_received:1212
192.168.0.3:8081> CLUSTER NODES
e3df642f88a85663ad621374cf92161ee95b0c1f 192.168.0.4:8082@18082 slave c2592dd2dd102515a2592368e4a172ee834dd56d 0 1710169952000 2 connected
61781a535d89518171dad34c2b73e5bf7c362852 192.168.0.4:8081@18081 master - 0 1710169953166 3 connected 10923-16383
c2592dd2dd102515a2592368e4a172ee834dd56d 192.168.0.3:8081@18081 myself,master - 0 1710169951000 2 connected 5461-10922
308f32fe43988cae664f953b6d072c77209d5f1f 192.168.0.3:8082@18082 slave 4ec8059cc336222dc6805c9e03913c9a5a6c1a82 0 1710169952164 1 connected
175116147c09c0e9035288d37d19203587ffc14d 192.168.0.2:8082@18082 slave 61781a535d89518171dad34c2b73e5bf7c362852 0 1710169949000 3 connected
4ec8059cc336222dc6805c9e03913c9a5a6c1a82 192.168.0.2:8081@18081 master - 0 1710169951161 1 connected 0-5460
192.168.0.3:8081>
节点(node):
CLUSTER MEET IP PORT 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子
CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点
CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点
CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面
9、cluster重新创建
关闭所有已启动的redis节点
删除集群相关文件(在设置数据存储目录dir目录下) 删除每个节点下的appendonlydir 、dump.rdb和nodes.conf文件
启动所有Redis节点
10、重新创建cluster集群
redis-cli.exe --cluster create --cluster-replicas 1 10.41.170.30:6381 10.41.170.40:6381 10.41.170.130:6381 10.41.170.30:6382 10.41.170.40:6382 10.41.170.130:6382
若是创建失败,提示:
11、访问集群:
redis-cli -h 192.168.0.3 -p 8081 -c -a gd@Redis123
或者
redis-cli -c -a qaz@123 -h 11.11.11.160 -p 6379
注:-c 以集群方式登陆,cluster。-a 密码,authority
12、关闭集群
关闭集群则需要逐个进行关闭,使用命令:
redis-cli -a qaz@123 -c -h 11.11.11.160 -p 6379 shutdown
13、手动管理redis集群
将 ip 和 port 所指定的节点添加到集群中
CLUSTER MEET <ip> <port>
从集群中移除 node_id 指定的节点
CLUSTER FORGET <node_id>
将当前节点设置为 node_id 指定的节点的从节点
CLUSTER REPLICATE <node_id>
将节点的配置文件保存到硬盘里面
CLUSTER SAVECONFIG
将一个或多个槽(slot)指派(assign)给当前节点
CLUSTER ADDSLOTS <slot> [slot ...]
移除一个或多个槽对当前节点的指派
CLUSTER DELSLOTS <slot> [slot ...]
移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点
CLUSTER FLUSHSLOTS
将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派
CLUSTER SETSLOT <slot> NODE <node_id>
将本节点的槽 slot 迁移到 node_id 指定的节点中
CLUSTER SETSLOT <slot> MIGRATING <node_id>
从 node_id 指定的节点中导入槽 slot 到本节点
CLUSTER SETSLOT <slot> IMPORTING <node_id>
取消对槽 slot 的导入(import)或者迁移(migrate)
CLUSTER SETSLOT <slot> STABLE
计算键 key 应该被放置在哪个槽上
CLUSTER KEYSLOT <key>
返回槽 slot 目前包含的键值对数量
CLUSTER COUNTKEYSINSLOT <slot>
返回 count 个 slot 槽中的键
CLUSTER GETKEYSINSLOT <slot> <count>
14、设置个key,发现会失败的
set hg hello
错误信息
(error) CLUSTERDOWN Hash slot not served
意思是没有分配哈希槽
RedisCluster使用数据分片(sharding)来实现: 一个 Redis 集群包含16384个哈希槽(hash slot), 数据库中的每个键都属于这16384个哈希槽的其中一个,
集群使用公式HASH_SLOT = CRC16(key) % 16384来计算键key属于哪个槽。
1)分配hash槽
redis-cli --cluster fix 192.168.100.131:49001
不要有疑问,就是执行一个端口即可了;因为已经把集群关联起来了;所以会在集群中分配
2)重新用redis-cli连接,然后查看集群信息
cluster info
3)再试试set
set hg hello
4)这个时候集群就可以用了,用客户端连接下看看
15、数据验证:
IP:192.168.0.2
[yyxt@localhost ~]$ redis-cli -h 192.168.0.2 -p 8081 -c -a gd@Redis123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.0.2:8081> cluster info
cluster_state:ok
cluster_slots_assigned:16384
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:7045
cluster_stats_messages_pong_sent:6806
cluster_stats_messages_sent:13851
cluster_stats_messages_ping_received:6801
cluster_stats_messages_pong_received:7045
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:13851
192.168.0.2:8081> cluster nodes
c2592dd2dd102515a2592368e4a172ee834dd56d 192.168.0.3:8081@18081 master - 0 1710176098512 2 connected 5461-10922
4ec8059cc336222dc6805c9e03913c9a5a6c1a82 192.168.0.2:8081@18081 myself,master - 0 1710176093000 1 connected 0-5460
e3df642f88a85663ad621374cf92161ee95b0c1f 192.168.0.4:8082@18082 slave c2592dd2dd102515a2592368e4a172ee834dd56d 0 1710176095502 2 connected
308f32fe43988cae664f953b6d072c77209d5f1f 192.168.0.3:8082@18082 slave 4ec8059cc336222dc6805c9e03913c9a5a6c1a82 0 1710176097508 1 connected
175116147c09c0e9035288d37d19203587ffc14d 192.168.0.2:8082@18082 slave 61781a535d89518171dad34c2b73e5bf7c362852 0 1710176094498 3 connected
61781a535d89518171dad34c2b73e5bf7c362852 192.168.0.4:8081@18081 master - 0 1710176096505 3 connected 10923-16383
192.168.0.2:8081> ping
PONG
192.168.0.2:8081> set user zkm
-> Redirected to slot [5474] located at 192.168.0.3:8081
OK
192.168.0.3:8081> keys *
1) "user"
192.168.0.3:8081> get user
"zkm"
IP:192.168.0.3
[yyxt@localhost ~]$ redis-cli -h 192.168.0.3 -p 8081 -c -a gd@Redis123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.0.3:8081> keys *
1) "user"
192.168.0.3:8081> get user
"zkm"
192.168.0.3:8081>