Docker安装Redis5.0集群

这里要使用的镜像为Grokzen/docker-redis-cluster,该作者也说明了,仅可用于测试,不能上生产的。而且这个镜像的文档真心很简陋,我也是踩了两天的坑才弄明白一点,反正现在也是能够搭建起来了。

环境说明

  • CentOS 7
  • Docker 18.ce
  • 4C 24G
  • 宿主机IP 10.10.170.161

新建目录

在 宿主机上使用root创建目录 /data/redis-data

启动Docker镜像

使用如下命令启动docker镜像

1
docker run -d --name redis-cluster --net=host --privileged  -p 7000:7000 -p 7001:7001 -p 7002:7002 -p 7003:7003 -p 7004:7004 -p 7005:7005 -v /data/redis-data:/redis-data grokzen/redis-cluster:latest

此时,docker容器就启动起来了,但是这里只是启动了6个redis示例,如果按照官方的文档,虽然也是启动了容器,但是容器的IP很坑,如果配置了-e "IP=0.0.0.0"则redis集群的IP是127.0.0.1,如果没配置,则都是docker自己的IP。

创建集群

随便使用一个redis-cli,镜像创建容器,本次我是用的是我电脑本地的redis-cli

1
redis-cli --cluster create 10.10.170.161:7000 10.10.170.161:7001 10.10.170.161:7002 10.10.170.161:7003 10.10.170.161:7004 10.10.170.161:7005 --cluster-replicas 1

此时会输出如下日志:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.10.170.161:7004 to 10.10.170.161:7000
Adding replica 10.10.170.161:7005 to 10.10.170.161:7001
Adding replica 10.10.170.161:7003 to 10.10.170.161:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: c11bf88f9f468fcd5cf78aadb059ff5e053f5b2a 10.10.170.161:7000
slots:[0-5460] (5461 slots) master
M: 88d3dbe039d990206a16850bf5aa81235dd8c38b 10.10.170.161:7001
slots:[5461-10922] (5462 slots) master
M: bcb819fb97d5680ce15ab30fb2ecfb2f39649b8d 10.10.170.161:7002
slots:[10923-16383] (5461 slots) master
S: e3f1c162beb160852ac5dd9eae2c057d9eaf1954 10.10.170.161:7003
replicates bcb819fb97d5680ce15ab30fb2ecfb2f39649b8d
S: cdb63f7bcbc452ea5d853954ce66112f420e42c2 10.10.170.161:7004
replicates c11bf88f9f468fcd5cf78aadb059ff5e053f5b2a
S: 6be878a7ff3bca5990543e6d6808e326eb6c944e 10.10.170.161:7005
replicates 88d3dbe039d990206a16850bf5aa81235dd8c38b
Can I set the above configuration? (type 'yes' to accept): yes

输入yes后,接着输入如下日志:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
>>> 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 10.10.170.161:7000)
M: c11bf88f9f468fcd5cf78aadb059ff5e053f5b2a 10.10.170.161:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: e3f1c162beb160852ac5dd9eae2c057d9eaf1954 10.10.170.161:7003
slots: (0 slots) slave
replicates bcb819fb97d5680ce15ab30fb2ecfb2f39649b8d
M: 88d3dbe039d990206a16850bf5aa81235dd8c38b 10.10.170.161:7001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 6be878a7ff3bca5990543e6d6808e326eb6c944e 10.10.170.161:7005
slots: (0 slots) slave
replicates 88d3dbe039d990206a16850bf5aa81235dd8c38b
S: cdb63f7bcbc452ea5d853954ce66112f420e42c2 10.10.170.161:7004
slots: (0 slots) slave
replicates c11bf88f9f468fcd5cf78aadb059ff5e053f5b2a
M: bcb819fb97d5680ce15ab30fb2ecfb2f39649b8d 10.10.170.161:7002
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.

那么此时,Redis集群就已经搭建完毕,通过下面几个命令来测试下集群:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
10.10.170.161:7000> 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:59
cluster_stats_messages_pong_sent:74
cluster_stats_messages_sent:133
cluster_stats_messages_ping_received:69
cluster_stats_messages_pong_received:59
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:133

查看集群节点信息

1
2
3
4
5
6
7
10.10.170.161:7000> cluster nodes
e3f1c162beb160852ac5dd9eae2c057d9eaf1954 10.10.170.161:7003@17003 slave bcb819fb97d5680ce15ab30fb2ecfb2f39649b8d 0 1561536179552 4 connected
c11bf88f9f468fcd5cf78aadb059ff5e053f5b2a 10.10.170.161:7000@17000 myself,master - 0 1561536180000 1 connected 0-5460
88d3dbe039d990206a16850bf5aa81235dd8c38b 10.10.170.161:7001@17001 master - 0 1561536180955 2 connected 5461-10922
6be878a7ff3bca5990543e6d6808e326eb6c944e 10.10.170.161:7005@17005 slave 88d3dbe039d990206a16850bf5aa81235dd8c38b 0 1561536180554 6 connected
cdb63f7bcbc452ea5d853954ce66112f420e42c2 10.10.170.161:7004@17004 slave c11bf88f9f468fcd5cf78aadb059ff5e053f5b2a 0 1561536180000 5 connected
bcb819fb97d5680ce15ab30fb2ecfb2f39649b8d 10.10.170.161:7002@17002 master - 0 1561536179000 3 connected 10923-16383

登录任意一台机器,给集群添加一个key

1
2
3
10.10.170.161:7002> set bar foo
-> Redirected to slot [5061] located at 10.10.170.161:7000
OK

获取一个值

1
2
3
10.10.170.161:7002> get bar
-> Redirected to slot [5061] located at 10.10.170.161:7000
"foo"

因为我们指定了--cluster-replicas 1所以在哪个节点设置的值就会在哪个节点返回。

Cco.Xyz wechat
坚持原创技术分享,您的支持将鼓励我继续创作!
0%