1.上传离线镜像包并解压导入
[root@localhost ~]# cd redis-cluster/
[root@localhost redis-cluster]# ls
redis-cluster.tar redis-trib.tar
[root@localhost redis-cluster]# docker load -i redis-cluster.tar
[root@localhost redis-cluster]# docker load -i redis-trib.tar
[root@localhost ~]# docker images|grep redis
redis-cluster latest 29e4f38e4475 2 years ago 94.9MB
redis-trib latest 0f7b910114d5 3 years ago 32MB
redis-cluster 镜像为开启cluster功能的Redis镜像
redis-trib 镜像为创建集群的工具
2. 编写compose文件
注:700* 为Redis服务所监听的端口,1700* 为集群节点之间通讯所用的的端口
文件内容为:
[root@localhost redis]# cat docker-compose.yml
version: "3"
services:
redis1:
image: redis-cluster
environment:
REDIS_PORT: 7001
ports:
- "7001:7001"
- "17001:17001"
redis2:
image: redis-cluster
environment:
REDIS_PORT: 7002
ports:
- "7002:7002"
- "17002:17002"
redis3:
image: redis-cluster
environment:
REDIS_PORT: 7003
ports:
- "7003:7003"
- "17003:17003"
redis4:
image: redis-cluster
environment:
REDIS_PORT: 7004
ports:
- "7004:7004"
- "17004:17004"
redis5:
image: redis-cluster
environment:
REDIS_PORT: 7005
ports:
- "7005:7005"
- "17005:17005"
redis6:
image: redis-cluster
environment:
REDIS_PORT: 7006
ports:
- "7006:7006"
- "17006:17006"
[root@localhost redis]#
3. 基于compose文件运行容器
[root@localhost redis]# docker-compose up -d
Creating network "redis_default" with the default driver
Creating redis_redis1_1 ... done
Creating redis_redis2_1 ... done
Creating redis_redis5_1 ... done
Creating redis_redis3_1 ... done
Creating redis_redis6_1 ... done
Creating redis_redis4_1 ... done
4.查看运行的容器
[root@localhost redis]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------------------------------
redis_redis1_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:17001->17001/tcp, 6379/tcp, 7000/tcp,
0.0.0.0:7001->7001/tcp
redis_redis2_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:17002->17002/tcp, 6379/tcp, 7000/tcp,
0.0.0.0:7002->7002/tcp
redis_redis3_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:17003->17003/tcp, 6379/tcp, 7000/tcp,
0.0.0.0:7003->7003/tcp
redis_redis4_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:17004->17004/tcp, 6379/tcp, 7000/tcp,
0.0.0.0:7004->7004/tcp
redis_redis5_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:17005->17005/tcp, 6379/tcp, 7000/tcp,
0.0.0.0:7005->7005/tcp
redis_redis6_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:17006->17006/tcp, 6379/tcp, 7000/tcp,
0.0.0.0:7006->7006/tcp
5. 基于 redis-trib 镜像运行容器并创建集群
–replicas 每个master的slave个数
[root@localhost redis]# docker run --rm -it redis-trib create --replicas 1 192.168.153.188:7001 192.168.153.188:7002 192.168.153.188:7003 192.168.153.188:7004 192.168.153.188:7005 192.168.153.188:7006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.153.188:7001
192.168.153.188:7002
192.168.153.188:7003
Adding replica 192.168.153.188:7004 to 192.168.153.188:7001
Adding replica 192.168.153.188:7005 to 192.168.153.188:7002
Adding replica 192.168.153.188:7006 to 192.168.153.188:7003
M: 0df61abb1a4e8fe387fb17d8b1791bc0da6e2e61 192.168.153.188:7001
slots:0-5460 (5461 slots) master
M: 0835b2910581f50ec4f242c46228548b02c94b57 192.168.153.188:7002
slots:5461-10922 (5462 slots) master
M: 220f82aaa6cd0ef91e009d7841c1508fa305423b 192.168.153.188:7003
slots:10923-16383 (5461 slots) master
S: 3ca45a0c709b6c380a371de03e15b6abd3e34096 192.168.153.188:7004
replicates 0df61abb1a4e8fe387fb17d8b1791bc0da6e2e61
S: ec0a4756ad58bf4e24597eb5df8132b05cdf1d31 192.168.153.188:7005
replicates 0835b2910581f50ec4f242c46228548b02c94b57
S: eb21f6cbc09c6cd0e1ec0592ce2bbb81378e21a4 192.168.153.188:7006
replicates 220f82aaa6cd0ef91e009d7841c1508fa305423b
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.153.188:7001)
M: 0df61abb1a4e8fe387fb17d8b1791bc0da6e2e61 192.168.153.188:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 3ca45a0c709b6c380a371de03e15b6abd3e34096 172.22.0.1:7004@17004
slots: (0 slots) slave
replicates 0df61abb1a4e8fe387fb17d8b1791bc0da6e2e61
M: 0835b2910581f50ec4f242c46228548b02c94b57 172.22.0.1:7002@17002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 220f82aaa6cd0ef91e009d7841c1508fa305423b 172.22.0.1:7003@17003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: eb21f6cbc09c6cd0e1ec0592ce2bbb81378e21a4 172.22.0.1:7006@17006
slots: (0 slots) slave
replicates 220f82aaa6cd0ef91e009d7841c1508fa305423b
S: ec0a4756ad58bf4e24597eb5df8132b05cdf1d31 172.22.0.1:7005@17005
slots: (0 slots) slave
replicates 0835b2910581f50ec4f242c46228548b02c94b57
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis]#
6. 连接容器测试
连接容器redis1
[root@localhost redis]# docker-compose exec redis1 bash
连接集群中的一个Redis服务
参数解释
-c Enable cluster mode (follow -ASK and -MOVED redirections). 表示连接集群
-h <hostname> Server hostname (default: 127.0.0.1).
-p <port> Server port (default: 6379).
root@8214dd99c27e:/data# redis-cli -c -h 192.168.153.188 -p 7001
//查看集群信息
192.168.153.188:7001> 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:515
cluster_stats_messages_pong_sent:514
cluster_stats_messages_sent:1029
cluster_stats_messages_ping_received:509
cluster_stats_messages_pong_received:515
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1029
//查看节点信息
192.168.153.188:7001> cluster nodes
0df61abb1a4e8fe387fb17d8b1791bc0da6e2e61 172.22.0.5:7001@17001 myself,master - 0 1608607239000 1 connected 0-5460
3ca45a0c709b6c380a371de03e15b6abd3e34096 172.22.0.1:7004@17004 slave 0df61abb1a4e8fe387fb17d8b1791bc0da6e2e61 0 1608607241253 4 connected
0835b2910581f50ec4f242c46228548b02c94b57 172.22.0.1:7002@17002 master - 0 1608607240550 2 connected 5461-10922
220f82aaa6cd0ef91e009d7841c1508fa305423b 172.22.0.1:7003@17003 master - 0 1608607241054 3 connected 10923-16383
eb21f6cbc09c6cd0e1ec0592ce2bbb81378e21a4 172.22.0.1:7006@17006 slave 220f82aaa6cd0ef91e009d7841c1508fa305423b 0 1608607240246 6 connected
ec0a4756ad58bf4e24597eb5df8132b05cdf1d31 172.22.0.1:7005@17005 slave 0835b2910581f50ec4f242c46228548b02c94b57 0 1608607241000 5 connected
192.168.153.188:7001>
创建键值对测试
192.168.153.188:7001> set name tom
-> Redirected to slot [5798] located at 172.22.0.1:7002
OK
172.22.0.1:7002> get name
"tom"
172.22.0.1:7002> set city beijing
-> Redirected to slot [11479] located at 172.22.0.1:7003
OK
172.22.0.1:7003> get city
"beijing"
172.22.0.1:7003>
7.高可用测试
停掉3 集群仍然可用 数据没有丢失
root@8214dd99c27e:/data# redis-cli -c -h 192.168.153.188 -p 7003 shutdown
root@8214dd99c27e:/data# redis-cli -c -h 192.168.153.188 -p 7001
192.168.153.188:7001> 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:7
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1628
cluster_stats_messages_pong_sent:1324
cluster_stats_messages_fail_sent:4
cluster_stats_messages_auth-ack_sent:1
cluster_stats_messages_sent:2957
cluster_stats_messages_ping_received:1319
cluster_stats_messages_pong_received:1336
cluster_stats_messages_meet_received:5
cluster_stats_messages_fail_received:1
cluster_stats_messages_auth-req_received:1
cluster_stats_messages_received:2662
192.168.153.188:7001> cluster nodes
0df61abb1a4e8fe387fb17d8b1791bc0da6e2e61 172.22.0.5:7001@17001 myself,master - 0 1608607669000 1 connected 0-5460
3ca45a0c709b6c380a371de03e15b6abd3e34096 172.22.0.1:7004@17004 slave 0df61abb1a4e8fe387fb17d8b1791bc0da6e2e61 0 1608607670354 4 connected
0835b2910581f50ec4f242c46228548b02c94b57 172.22.0.1:7002@17002 master - 0 1608607670000 2 connected 5461-10922
220f82aaa6cd0ef91e009d7841c1508fa305423b 172.22.0.1:7003@17003 master,fail - 1608607630805 1608607629902 3 disconnected
eb21f6cbc09c6cd0e1ec0592ce2bbb81378e21a4 172.22.0.1:7006@17006 master - 0 1608607668330 7 connected 10923-16383
ec0a4756ad58bf4e24597eb5df8132b05cdf1d31 172.22.0.1:7005@17005 slave 0835b2910581f50ec4f242c46228548b02c94b57 0 1608607669851 5 connected
192.168.153.188:7001> get city
-> Redirected to slot [11479] located at 172.22.0.1:7006
"beijing"
172.22.0.1:7006>
再停掉1和2
停掉1之后退出
root@8214dd99c27e:/data# redis-cli -c -h 192.168.153.188 -p 7001 shutdown
root@8214dd99c27e:/data#
[root@localhost redis]#
[root@localhost redis]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------------------------------------------------
redis_redis1_1 /usr/local/bin/entrypoint. ... Exit 0
redis_redis2_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:17002->17002/tcp, 6379/tcp, 7000/tcp, 0.0.0.0:7002->7002/tcp
redis_redis3_1 /usr/local/bin/entrypoint. ... Exit 0
redis_redis4_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:17004->17004/tcp, 6379/tcp, 7000/tcp, 0.0.0.0:7004->7004/tcp
redis_redis5_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:17005->17005/tcp, 6379/tcp, 7000/tcp, 0.0.0.0:7005->7005/tcp
redis_redis6_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:17006->17006/tcp, 6379/tcp, 7000/tcp, 0.0.0.0:7006->7006/tcp
连接4停掉2
[root@localhost redis]# docker-compose exec redis4 bash
root@39dcd8bd7bad:/data# redis-cli -c -h 192.168.153.188 -p 7002 shutdown
查看数据仍然没有丢失
root@39dcd8bd7bad:/data# redis-cli -c -h 192.168.153.188 -p 7004
192.168.153.188:7004> get name
-> Redirected to slot [5798] located at 172.22.0.1:7005
"tom"
172.22.0.1:7005> get city
-> Redirected to slot [11479] located at 172.22.0.1:7006
"beijing"
172.22.0.1:7006> exit
root@39dcd8bd7bad:/data# exit
exit
[root@localhost redis]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------------------------------------------------
redis_redis1_1 /usr/local/bin/entrypoint. ... Exit 0
redis_redis2_1 /usr/local/bin/entrypoint. ... Exit 0
redis_redis3_1 /usr/local/bin/entrypoint. ... Exit 0
redis_redis4_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:17004->17004/tcp, 6379/tcp, 7000/tcp, 0.0.0.0:7004->7004/tcp
redis_redis5_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:17005->17005/tcp, 6379/tcp, 7000/tcp, 0.0.0.0:7005->7005/tcp
redis_redis6_1 /usr/local/bin/entrypoint. ... Up 0.0.0.0:17006->17006/tcp, 6379/tcp, 7000/tcp, 0.0.0.0:7006->7006/tcp
[root@localhost redis]#