redis cluster集群构建流程:
1.准备节点(docker-compose 编排构建redis主从节点)
2.节点握手(通过Gssip[流言协议]握手)–>(cluster meet IP port–>cluster meet 192.168.11.43 6391)–>(redis-cli客户端执行)
3.配置节点的主从关系(redis-cli -h ip port[从节点端口] cluster replicate 主节点id)(redis-cli -h 192.168.11.43 6394 cluster replicate fe74467be98b)
4.为主节点分配槽(redis-cli -h 192.168.11.43 -p 6391[主节点端口] cluster addslots {0…5461})
1.安装前准备
[root@localhost ~]# mkdir -p /usr/local/docker-compose-redis-cluster
[root@localhost ~]# cd /usr/local/docker-compose-redis-cluster
将准备文件全部上传到docker-compose-redis-cluster目录下
文件地址:https://github.com/Yinjinshui/docker-compose-redis-cluster
[root@localhost docker-compose-redis-cluster]# ll
total 8
drwxr-xr-x. 2 root root 181 Dec 10 22:32 config
-rw-r--r--. 1 root root 3357 Dec 10 22:32 docker-compose.yaml
-rw-r--r--. 1 root root 559 Dec 10 22:32 Dockerfile
2.构建redis镜像
[root@localhost docker-compose-redis-cluster]# docker build -t rediscluster .
[root@localhost docker-compose-redis-cluster]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
rediscluster latest 2124d2e5475b 16 hours ago 392MB
3.执行docker-compose编排
查看构建编排文件的版本
[root@localhost docker-compose-redis-cluster]# docker-compose version
docker-compose version 1.18.0, build 8dd22a9
docker-py version: 2.6.1
CPython version: 3.6.8
OpenSSL version: OpenSSL 1.0.2k-fips 26 Jan 2017
【注意:安装的时候如果版本不对会自动提示版本】
例子:
[root@localhost docker-compose-redis-cluster]# docker-compose up -d
ERROR: Version in "./docker-compose.yaml" is unsupported. You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/
【如若版本不对自行修改docker-compose.yaml的版本】
demo :version: “3.3”
【执行编排操作】
[root@localhost docker-compose-redis-cluster]# docker-compose up -d
Creating network "dockercomposerediscluster_redis-master" with driver "bridge"
Creating network "dockercomposerediscluster_redis-slave" with driver "bridge"
Creating redis-salve2 ... done
Creating redis-salve2 ...
Creating redis-slave3 ...
Creating redis-slave1 ...
Creating redis-master2 ...
Creating redis-master1 ...
4.查看构建的容器
[root@localhost docker-compose-redis-cluster]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------
redis-master1 /bin/bash redis.sh Up 0.0.0.0:16391->16391/tcp, 0.0.0.0:6391->6391/tcp
redis-master2 /bin/bash redis.sh Up 0.0.0.0:16392->16392/tcp, 0.0.0.0:6392->6392/tcp
redis-master3 /bin/bash redis.sh Up 0.0.0.0:16393->16393/tcp, 0.0.0.0:6393->6393/tcp
redis-salve2 /bin/bash redis.sh Up 0.0.0.0:16395->16395/tcp, 0.0.0.0:6395->6395/tcp
redis-slave1 /bin/bash redis.sh Up 0.0.0.0:16394->16394/tcp, 0.0.0.0:6394->6394/tcp
redis-slave3 /bin/bash redis.sh Up 0.0.0.0:16396->16396/tcp, 0.0.0.0:6396->6396/tcp
5.进入容器【目前节点与节点之间是没有握手】
[root@localhost docker-compose-redis-cluster]# docker exec -it redis-master1 bash
查看当前节点集群中唯一标识:
[root@2ae5865153b8 config]# less /var/lib/redis/nodes-6379.conf
[root@2ae5865153b8 config]# redis-cli -p 6391
127.0.0.1:6391> cluster nodes
e36fb3c3396563011556a495a7f4aaf21ca7c315 :6391@16391 myself,master - 0 0 0 connected
6.通过流言协议执行节点握手操作
127.0.0.1:6391> cluster meet 192.168.11.43 6392 [此处利用局域网或公网执行握手]
OK
127.0.0.1:6391> cluster meet 192.168.11.43 6393
OK
127.0.0.1:6391> cluster meet 192.168.11.43 6394
OK
127.0.0.1:6391> cluster meet 192.168.11.43 6395
OK
127.0.0.1:6391> cluster meet 192.168.11.43 6396
OK
cluster nodes:列出集群当前已知的所有节点(node)的相关信息
127.0.0.1:6391> cluster nodes
16faacf6fab18d620db2e62abcec5bec628d26dd 172.50.0.1:6394@16394 master - 0 1576037291000 3 connected
bad6b3fb3316a79bcc2fc108de76367d810b4486 172.50.0.1:6396@16396 master - 0 1576037289764 5 connected
b9ae3b769804bc6db06a04bb970a8ea5810e4af2 172.50.0.1:6395@16395 master - 0 1576037288757 4 connected
bd6000cb1fd66bed187f4119e26de152ebaa6587 172.50.0.1:6392@16392 master - 0 1576037289000 1 connected
e36fb3c3396563011556a495a7f4aaf21ca7c315 172.50.0.2:6391@16391 myself,master - 0 1576037290000 2 connected
f70c42b9995b5814ef170b7147d794468cf66a00 172.50.0.1:6393@16393 master - 0 1576037291782 0 connected
打印集群的信息
127.0.0.1:6391> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:0
cluster_current_epoch:5
cluster_my_epoch:2
cluster_stats_messages_ping_sent:193
cluster_stats_messages_pong_sent:195
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:393
cluster_stats_messages_ping_received:195
cluster_stats_messages_pong_received:198
cluster_stats_messages_received:393
7.设置从节点
[root@2ae5865153b8 config]# redis-cli -h 192.168.11.43 -p 6394 cluster replicate e36fb3c3396563011556a495a7f4aaf21ca7c315 [主节点的集群ID标识]
OK
[root@2ae5865153b8 config]# redis-cli -h 192.168.11.43 -p 6395 cluster replicate bd6000cb1fd66bed187f4119e26de152ebaa6587
OK
[root@2ae5865153b8 config]# redis-cli -h 192.168.11.43 -p 6396 cluster replicate f70c42b9995b5814ef170b7147d794468cf66a00
OK
查看节点设置:
127.0.0.1:6391> cluster nodes
16faacf6fab18d620db2e62abcec5bec628d26dd 172.50.0.1:6394@16394 slave e36fb3c3396563011556a495a7f4aaf21ca7c315 0 1576038324847 3 connected
bad6b3fb3316a79bcc2fc108de76367d810b4486 172.50.0.1:6396@16396 slave f70c42b9995b5814ef170b7147d794468cf66a00 0 1576038324000 5 connected
b9ae3b769804bc6db06a04bb970a8ea5810e4af2 172.50.0.1:6395@16395 slave bd6000cb1fd66bed187f4119e26de152ebaa6587 0 1576038325856 4 connected
bd6000cb1fd66bed187f4119e26de152ebaa6587 172.50.0.1:6392@16392 master - 0 1576038325000 1 connected
e36fb3c3396563011556a495a7f4aaf21ca7c315 172.50.0.2:6391@16391 myself,master - 0 1576038323000 2 connected
f70c42b9995b5814ef170b7147d794468cf66a00 172.50.0.1:6393@16393 master - 0 1576038326862 0 connected
8.为主节点分配槽
[root@2ae5865153b8 config]# redis-cli -h 192.168.11.43 -p 6391 cluster addslots {0..5461}
OK
[root@2ae5865153b8 config]# redis-cli -h 192.168.11.43 -p 6392 cluster addslots {5462..10922}
OK
[root@2ae5865153b8 config]# redis-cli -h 192.168.11.43 -p 6393 cluster addslots {10923..16383}
OK
查看槽节点分配
127.0.0.1:6391> cluster nodes
16faacf6fab18d620db2e62abcec5bec628d26dd 172.50.0.1:6394@16394 slave e36fb3c3396563011556a495a7f4aaf21ca7c315 0 1576038637323 3 connected
bad6b3fb3316a79bcc2fc108de76367d810b4486 172.50.0.1:6396@16396 slave f70c42b9995b5814ef170b7147d794468cf66a00 0 1576038638331 5 connected
b9ae3b769804bc6db06a04bb970a8ea5810e4af2 172.50.0.1:6395@16395 slave bd6000cb1fd66bed187f4119e26de152ebaa6587 0 1576038638000 4 connected
bd6000cb1fd66bed187f4119e26de152ebaa6587 172.50.0.1:6392@16392 master - 0 1576038638000 1 connected 5462-10922
e36fb3c3396563011556a495a7f4aaf21ca7c315 172.50.0.2:6391@16391 myself,master - 0 1576038637000 2 connected 0-5461
f70c42b9995b5814ef170b7147d794468cf66a00 172.50.0.1:6393@16393 master - 0 1576038639334 0 connected 10923-16383
10.操作redis执行set操作【没有指定集群模式】:
在redis-master1 6391 执行set操作
127.0.0.1:6391> set name 111
(error) MOVED 5798 172.50.0.1:6392--》提示该key需要存储到5798的槽中
127.0.0.1:6391> get name
(error) MOVED 5798 172.50.0.1:6392
127.0.0.1:6391>
【提示该key需要到6392的节点执行操作】
在redis-master2 6392 执行set操作
127.0.0.1:6392> set name 111
OK
127.0.0.1:6392> get name
"111"
【name信息只有在6392的节点才能操作】
set,get操作都得到指定的槽中操作,不然key无法执行set,get操作
【说明:数据插入一定要到到对应的槽区执行数据插入的操作】
11.集群模式下获取数据:
[root@d8b6958531ad redis]# redis-cli -c -h 192.168.11.43 -p 6391 set name 123456
OK
[root@d8b6958531ad redis]# redis-cli -c -h 192.168.11.43 -p 6391 get name
"123456"
[root@d8b6958531ad redis]# rredis-cli -c -h 192.168.11.43 -p 6391 set nameof 1111
OK
[root@d8b6958531ad redis]# redis-cli -c -h 192.168.11.43 -p 6391 get nameof
"1111"
【注意:集群模式下的操作无需指定到对应的槽节点执行数据操作】
12.cluster 命令
CLUSTER info:打印集群的信息。
CLUSTER nodes:列出集群当前已知的所有节点(node)的相关信息。
CLUSTER meet :将ip和port所指定的节点添加到集群当中。
CLUSTER addslots [slot ...]:将一个或多个槽(slot)指派(assign)给当前节点。
CLUSTER delslots [slot ...]:移除一个或多个槽对当前节点的指派。
CLUSTER slots:列出槽位、节点信息。
CLUSTER slaves :列出指定节点下面的从节点信息。
CLUSTER replicate :将当前节点设置为指定节点的从节点。
CLUSTER saveconfig:手动执行命令保存保存集群的配置文件,集群默认在配置修改的时候会自动保存配置文件。
CLUSTER keyslot :列出key被放置在哪个槽上。
CLUSTER flushslots:移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
CLUSTER countkeysinslot :返回槽目前包含的键值对数量。
CLUSTER getkeysinslot :返回count个槽中的键。
CLUSTER setslot node 将槽指派给指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽,然后再进行指派。
CLUSTER setslot migrating 将本节点的槽迁移到指定的节点中。
CLUSTER setslot importing 从 node_id 指定的节点中导入槽 slot 到本节点。
CLUSTER setslot stable 取消对槽 slot 的导入(import)或者迁移(migrate)。
CLUSTER failover:手动进行故障转移。
CLUSTER forget :从集群中移除指定的节点,这样就无法完成握手,过期时为60s,60s后两节点又会继续完成握手。
CLUSTER reset [HARD|SOFT]:重置集群信息,soft是清空其他节点的信息,但不修改自己的id,hard还会修改自己的id,不传该参数则使用soft方式。
CLUSTER count-failure-reports :列出某个节点的故障报告的长度。
CLUSTER SET-CONFIG-EPOCH:设置节点epoch,只有在节点加入集群前才能设置。