本文介绍自己建立一个redis-cluster集群的实践,三主三从,使用docker搭建。
其实搭建很简单,就是建立6个容器,每个容器配置不同的端口号,其他的都是一样的。
假设建立6个端口号分别为 5555到5560。
配置文件
port 5555
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize no
bind 0.0.0.0
protected-mode no
pidfile /tmp/redis/redis-cluster/55555/redis.pid
上面的参数都是必要的。不过如果protected-mode设置为了no,bing可以不加。cluster-enabled是打开了cluster开关,允许使用集群。 cluster-config-file会自动生成,关于该节点的配置信息。 cluster-node-timeout是非常关键的参数,它的值直接影响到了集群通信的效率。看源码就会看到集群节点间的通信都会使用这个值,是单向通信超时的最大时间。
OK 配置好了,就直接编写docker-compose。就是配置各个service,不过下面的教法不准确,这时我们并没指定哪个是主,哪个是从。主从是redis替我们做的工作。
docker-compose文件:
version: '3'networks:redis-cluster-net:external:name: redis-cluster-netservices:redis-master-1:image: redis #依赖的基础镜像restart: always #是否遇到异常时自动自动重启container_name: redis-master-1command: [ "redis-server", "/home/redis/cluster/redis.conf" ] #启动命令networks:redis-cluster-net:ipv4_address: 192.168.200.11volumes: #redis数据挂载到宿主机- /tmp/redis/redis-cluster/55555/data:/data- /tmp/redis/redis-cluster/55555/redis.conf:/home/redis/cluster/redis.confports:- 5555:5555 #服务端口- 15555:15555 #集群端口redis-master-2:image: redisrestart: alwayscontainer_name: redis-master-2command: [ "redis-server", "/home/redis/cluster/redis.conf" ]networks:redis-cluster-net:ipv4_address: 192.168.200.12volumes:- /tmp/redis/redis-cluster/55557/data:/data- /tmp/redis/redis-cluster/55557/redis.conf:/home/redis/cluster/redis.confports:- 5557:5557 #服务端口- 15557:15557 #集群端口redis-master-3:image: redisrestart: alwayscontainer_name: redis-master-3command: [ "redis-server", "/home/redis/cluster/redis.conf" ]networks:redis-cluster-net:ipv4_address: 192.168.200.13volumes:- /tmp/redis/redis-cluster/55559/data:/data- /tmp/redis/redis-cluster/55559/redis.conf:/home/redis/cluster/redis.confports:- 5559:5559 #服务端口- 15559:15559 #集群端口redis-slave-1:image: redisrestart: alwayscontainer_name: redis-slave-1command: [ "redis-server", "/home/redis/cluster/redis.conf" ]networks:redis-cluster-net:ipv4_address: 192.168.200.14volumes:- /tmp/redis/redis-cluster/55556/data:/data- /tmp/redis/redis-cluster/55556/redis.conf:/home/redis/cluster/redis.confports:- 5556:5556 #服务端口- 15556:15556 #集群端口redis-slave-2:image: redisrestart: alwayscontainer_name: redis-slave-2command: [ "redis-server", "/home/redis/cluster/redis.conf" ]networks:redis-cluster-net:ipv4_address: 192.168.200.15volumes:- /tmp/redis/redis-cluster/55558/data:/data- /tmp/redis/redis-cluster/55558/redis.conf:/home/redis/cluster/redis.confports:- 5558:5558 #服务端口- 15558:15558 #集群端口redis-slave-3:image: redisrestart: alwayscontainer_name: redis-slave-3command: [ "redis-server", "/home/redis/cluster/redis.conf" ]networks:redis-cluster-net:ipv4_address: 192.168.200.16volumes:- /tmp/redis/redis-cluster/55510/data:/data- /tmp/redis/redis-cluster/55510/redis.conf:/home/redis/cluster/redis.confports:- 5510:5510 #服务端口- 15510:15510 #集群端口
启动:docker-compose up -d
容器启动之后,6个节点的redis还是完全独立的,需要手动建立cluster集群。直接使用redis-cli的命令即可。
/tmp/redis/redis-cluster$ redis-cli --cluster create 10.231.23.240:5555 10.231.23.240:5556 10.231.23.240:5557 10.231.23.240:5558 10.231.23.240:5559 10.231.23.240:5510 --cluster-replicas 1
>>> 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.231.23.240:5559 to 10.231.23.240:5555
Adding replica 10.231.23.240:5510 to 10.231.23.240:5556
Adding replica 10.231.23.240:5558 to 10.231.23.240:5557
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 69cf0edf78f88458535c05446deaad8bce0123eb 10.231.23.240:5555slots:[0-5460] (5461 slots) master
M: ef9eaf11a9664a023c2a686186b4d69c44415bb3 10.231.23.240:5556slots:[5461-10922] (5462 slots) master
M: 17debd2a9e30af742956428d959d87ad3b2f6fd9 10.231.23.240:5557slots:[10923-16383] (5461 slots) master
S: b5983582f3c04ff7639e09b405fb8ef82c6cbb5d 10.231.23.240:5558replicates 69cf0edf78f88458535c05446deaad8bce0123eb
S: 326176c22211e64b94697ddc12748e15ffd98402 10.231.23.240:5559replicates ef9eaf11a9664a023c2a686186b4d69c44415bb3
S: 738f0fe576d1433302c967d954fc09a92ccbba46 10.231.23.240:5510replicates 17debd2a9e30af742956428d959d87ad3b2f6fd9
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 10.231.23.240:5555)
M: 69cf0edf78f88458535c05446deaad8bce0123eb 10.231.23.240:5555slots:[0-5460] (5461 slots) master1 additional replica(s)
S: 738f0fe576d1433302c967d954fc09a92ccbba46 192.168.200.1:5510slots: (0 slots) slavereplicates 17debd2a9e30af742956428d959d87ad3b2f6fd9
S: b5983582f3c04ff7639e09b405fb8ef82c6cbb5d 192.168.200.1:5558slots: (0 slots) slavereplicates 69cf0edf78f88458535c05446deaad8bce0123eb
S: 326176c22211e64b94697ddc12748e15ffd98402 192.168.200.1:5559slots: (0 slots) slavereplicates ef9eaf11a9664a023c2a686186b4d69c44415bb3
M: 17debd2a9e30af742956428d959d87ad3b2f6fd9 192.168.200.1:5557slots:[10923-16383] (5461 slots) master1 additional replica(s)
M: ef9eaf11a9664a023c2a686186b4d69c44415bb3 192.168.200.1:5556slots:[5461-10922] (5462 slots) master1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
看上面的信息已经很清晰了,建立了三个master,并分别指派了slot范围。此外,还添加了三个从节点。
集群建立后,查看集群信息:
10.231.23.240:5555> 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:1872
cluster_stats_messages_pong_sent:1877
cluster_stats_messages_sent:3749
cluster_stats_messages_ping_received:1872
cluster_stats_messages_pong_received:1872
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:3749
查看集群节点:
10.231.23.240:5555> cluster nodes
738f0fe576d1433302c967d954fc09a92ccbba46 192.168.200.1:5510@15510 slave 17debd2a9e30af742956428d959d87ad3b2f6fd9 0 1605753009829 3 connected
b5983582f3c04ff7639e09b405fb8ef82c6cbb5d 192.168.200.1:5558@15558 slave 69cf0edf78f88458535c05446deaad8bce0123eb 0 1605753009527 1 connected
326176c22211e64b94697ddc12748e15ffd98402 192.168.200.1:5559@15559 slave ef9eaf11a9664a023c2a686186b4d69c44415bb3 0 1605753010834 2 connected
17debd2a9e30af742956428d959d87ad3b2f6fd9 192.168.200.1:5557@15557 master - 0 1605753009000 3 connected 10923-16383
ef9eaf11a9664a023c2a686186b4d69c44415bb3 192.168.200.1:5556@15556 master - 0 1605753010029 2 connected 5461-10922
69cf0edf78f88458535c05446deaad8bce0123eb 192.168.200.11:5555@15555 myself,master - 0 1605753010000 1 connected 0-5460
测试一下添加数据:
127.0.0.1:5555> set test1 haibo
OK
127.0.0.1:5555> set test2 haibo
-> Redirected to slot [8899] located at 192.168.200.1:5557
OK
192.168.200.1:5557> get test2
"haibo"
192.168.200.1:5557> get test1
-> Redirected to slot [4768] located at 192.168.200.1:5555
"haibo"
192.168.200.1:5555> get test2
-> Redirected to slot [8899] located at 192.168.200.1:5557
"haibo"
192.168.200.1:5557>
现在三主三从的Redis-cluster集群已经正常运行了,现在我们像其中加入两个节点,一主一从。首先还是用docker启动两个容器,端口5561,5562。
首先将5561加入到集群为master节点:
redis-cli --cluster add-node 10.231.23.240:5561 10.231.23.240:5555
>>> Adding node 10.231.23.240:5561 to cluster 10.231.23.240:5555
>>> Performing Cluster Check (using node 10.231.23.240:5555)
M: 69cf0edf78f88458535c05446deaad8bce0123eb 10.231.23.240:5555slots:[0-5460] (5461 slots) master1 additional replica(s)
S: 738f0fe576d1433302c967d954fc09a92ccbba46 192.168.200.1:5510slots: (0 slots) slavereplicates 17debd2a9e30af742956428d959d87ad3b2f6fd9
S: b5983582f3c04ff7639e09b405fb8ef82c6cbb5d 192.168.200.1:5558slots: (0 slots) slavereplicates 69cf0edf78f88458535c05446deaad8bce0123eb
S: 326176c22211e64b94697ddc12748e15ffd98402 192.168.200.1:5559slots: (0 slots) slavereplicates ef9eaf11a9664a023c2a686186b4d69c44415bb3
M: 17debd2a9e30af742956428d959d87ad3b2f6fd9 192.168.200.1:5557slots:[10923-16383] (5461 slots) master1 additional replica(s)
M: ef9eaf11a9664a023c2a686186b4d69c44415bb3 192.168.200.1:5556slots:[5461-10922] (5462 slots) master1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
加入之后,slot还没有分片,新加入节点的slot还是空的。
现在执行重新分片操作:
redis-cli --cluster reshard 10.231.23.240:5561 --cluster-from 69cf0edf78f88458535c05446deaad8bce0123eb,ef9eaf11a9664a023c2a686186b4d69c44415bb3,17debd2a9e30af742956428d959d87ad3b2f6fd9 --cluster-to 10cc93e09a91e0e9cfd749d58e4ae1220f5f412c --cluster-slots 4000
分片之后,可以看到新节点已经指派了slot:
192.168.200.1:5556> cluster nodes
17debd2a9e30af742956428d959d87ad3b2f6fd9 192.168.200.1:5557@15557 master - 0 1605756921113 3 connected 12256-16383
738f0fe576d1433302c967d954fc09a92ccbba46 192.168.200.1:5510@15510 slave 17debd2a9e30af742956428d959d87ad3b2f6fd9 0 1605756921615 3 connected
b5983582f3c04ff7639e09b405fb8ef82c6cbb5d 192.168.200.1:5558@15558 slave 69cf0edf78f88458535c05446deaad8bce0123eb 0 1605756920000 1 connected
326176c22211e64b94697ddc12748e15ffd98402 192.168.200.1:5559@15559 slave ef9eaf11a9664a023c2a686186b4d69c44415bb3 0 1605756921514 2 connected
10cc93e09a91e0e9cfd749d58e4ae1220f5f412c 192.168.200.1:5561@15561 master - 0 1605756920109 7 connected 0-1332 5461-6794 10923-12255
ef9eaf11a9664a023c2a686186b4d69c44415bb3 192.168.200.14:5556@15556 myself,master - 0 1605756920000 2 connected 6795-10922
69cf0edf78f88458535c05446deaad8bce0123eb 192.168.200.1:5555@15555 master - 0 1605756920000 1 connected 1333-5460
上面是使用docker部署Redis的简单过程。