1. Node planning:
Two ways: one master + many slaves, many to many slaves, the second way to share the master node, easy to expand and reduce capacity.
Container name container IP address Map port number Service Running Mode Redis-master1 172.1.50.11 6391->6379,16391->6379 master Redis-master2 172.1.50.12 6392->6379,16392->6379 master Redis-master3 172.1.50.13 6393->6379,16393->6379 master redis-slave1 172.1.30.11 6394->6379,16394->6379 Slave redis-slave2 172.1.30.12 6395->6379,16395->6379 Slave redis-slave3 172.1.30.13 6396->6379,16396->6379 Slave
Add a "bus-port" port of 10000+ to view the official redis.conf instructions:
# * cluster-announce-ip # * cluster-announce-port # * cluster-announce-bus-port # Explain that under the above paragraph, redundant ports are used for cluster auto-detection.
Official Reference Documents.
A. Create a custom network
For convenience, add redis to the previous mybridge network to facilitate the call of php and lua.High availability is accessed through the host network, so both master and slave have to open ports to the host.As above.
2. Preliminary Cluster Creation
a. Make container creation scripts
Simplified name:
clmx master server
clsx slave server
docker stop clm1 clm2 clm3 cls1 cls2 cls3 docker rm clm1 clm2 clm3 cls1 cls2 cls3 docker run --name clm1 \ -p 6391:6379 -p 16391:16379 \ --restart=always \ --network=mybridge --ip=172.1.50.11 \ -v /root/tmp/dk/cluster_redis/6391/data:/data \ -v /root/tmp/dk/cluster_redis/6391:/etc/redis \ -d cffycls/redis5:1.7 docker run --name clm2 \ -p 6392:6379 -p 16392:16379 \ --restart=always \ --network=mybridge --ip=172.1.50.12 \ -v /root/tmp/dk/cluster_redis/6392/data:/data \ -v /root/tmp/dk/cluster_redis/6392:/etc/redis \ -d cffycls/redis5:1.7 docker run --name clm3 \ -p 6393:6379 -p 16393:16379 \ --restart=always \ --network=mybridge --ip=172.1.50.13 \ -v /root/tmp/dk/cluster_redis/6393/data:/data \ -v /root/tmp/dk/cluster_redis/6393:/etc/redis \ -d cffycls/redis5:1.7 docker run --name cls1 \ -p 6394:6379 -p 16394:16379 \ --restart=always \ --network=mybridge --ip=172.1.30.11 \ -v /root/tmp/dk/cluster_redis/6394/data:/data \ -v /root/tmp/dk/cluster_redis/6394:/etc/redis \ -d cffycls/redis5:1.7 docker run --name cls2 \ -p 6395:6379 -p 16395:16379 \ --restart=always \ --network=mybridge --ip=172.1.30.12 \ -v /root/tmp/dk/cluster_redis/6395/data:/data \ -v /root/tmp/dk/cluster_redis/6395:/etc/redis \ -d cffycls/redis5:1.7 docker run --name cls3 \ -p 6396:6379 -p 16396:16379 \ --restart=always \ --network=mybridge --ip=172.1.30.13 \ -v /root/tmp/dk/cluster_redis/6396/data:/data \ -v /root/tmp/dk/cluster_redis/6396:/etc/redis \ -d cffycls/redis5:1.7
b. Test configuration files, build clusters
Search for modifications based on previous master-slave configurations
cluster-enabled yes
Restart all containers.
--The node finds it and needs to be set on the container command line:
#Enter 172.1.50.1.11 docker exec -it clm1 bash / # redis-cli 127.0.0.1:6379> auth 123456 127.0.0.1:6379> info cluster 127.0.0.1:6379> cluster meet 172.1.50.12 6379 127.0.0.1:6379> cluster meet 172.1.50.13 6379 127.0.0.1:6379> cluster meet 172.1.30.11 6379 127.0.0.1:6379> cluster meet 172.1.30.12 6379 127.0.0.1:6379> cluster meet 172.1.30.13 6379 127.0.0.1:6379> cluster nodes e6f4def93bb888c144c4db308b5a7846d95d257b 172.1.50.11:6379@16379 myself,master - 0 1562061736000 2 connected a0a5d4e10d97ba63fbf4f6eba3f4cf1f73d53423 172.1.30.12:6379@16379 master - 0 1562061738000 4 connected c9f17946ca2c22a6dd0269614293e1bf38ae869b 172.1.50.12:6379@16379 master - 0 1562061737000 1 connected 2c5395040cfb9611b515d0424f30c91eba1ec6e8 172.1.30.11:6379@16379 master - 0 1562061740000 3 connected eff5996aa6d5d9ab048a778246fcc1663322fe7d 172.1.50.13:6379@16379 master - 0 1562061739802 0 connected 67cf166b61e7d892affa6d754a563b5993a9c5a3 172.1.30.13:6379@16379 master - 0 1562061740802 5 connected
Here the cluster finds itself at once and feels fast.If not:
See if the server can ping and if the profile modifications are in place, where the profiles for the six nodes are consistent.
[The cluster is not working properly after the node establishes the handshake, and the cluster is offline, all data read and write are prohibited.]
c. Set up slave nodes
Use the cluster replicate {nodeId} command to make a node a slave node.Where command execution must be performed on the corresponding slave node, setting the current node as the slave node of the node specified by node_id.
# cls1 container operation 127.0.0.1:6379> cluster nodes a0a5d4e10d97ba63fbf4f6eba3f4cf1f73d53423 172.1.30.12:6379@16379 master - 0 1562063939000 4 connected eff5996aa6d5d9ab048a778246fcc1663322fe7d 172.1.50.13:6379@16379 master - 0 1562063942507 0 connected c9f17946ca2c22a6dd0269614293e1bf38ae869b 172.1.50.12:6379@16379 master - 0 1562063938000 1 connected 67cf166b61e7d892affa6d754a563b5993a9c5a3 172.1.30.13:6379@16379 master - 0 1562063941505 5 connected 2c5395040cfb9611b515d0424f30c91eba1ec6e8 172.1.30.11:6379@16379 myself,master - 0 1562063940000 3 connected e6f4def93bb888c144c4db308b5a7846d95d257b 172.1.50.11:6379@16379 master - 0 1562063940503 2 connected 127.0.0.1:6379> cluster REPLICATE e6f4def93bb888c144c4db308b5a7846d95d257b OK 127.0.0.1:6379> cluster nodes a0a5d4e10d97ba63fbf4f6eba3f4cf1f73d53423 172.1.30.12:6379@16379 master - 0 1562063991000 4 connected eff5996aa6d5d9ab048a778246fcc1663322fe7d 172.1.50.13:6379@16379 master - 0 1562063993000 0 connected c9f17946ca2c22a6dd0269614293e1bf38ae869b 172.1.50.12:6379@16379 master - 0 1562063994634 1 connected 67cf166b61e7d892affa6d754a563b5993a9c5a3 172.1.30.13:6379@16379 master - 0 1562063992000 5 connected 2c5395040cfb9611b515d0424f30c91eba1ec6e8 172.1.30.11:6379@16379 myself,slave e6f4def93bb888c144c4db308b5a7846d95d257b 0 1562063993000 3 connected e6f4def93bb888c144c4db308b5a7846d95d257b 172.1.50.11:6379@16379 master - 0 1562063993632 2 connected 127.0.0.1:6379> #Likewise, the final result on cls3 / # redis-cli -h 172.1.30.13 172.1.30.13:6379> cluster nodes c9f17946ca2c22a6dd0269614293e1bf38ae869b 172.1.50.12:6379@16379 master - 0 1562064342897 1 connected a0a5d4e10d97ba63fbf4f6eba3f4cf1f73d53423 172.1.30.12:6379@16379 slave c9f17946ca2c22a6dd0269614293e1bf38ae869b 0 1562064345000 4 connected 67cf166b61e7d892affa6d754a563b5993a9c5a3 172.1.30.13:6379@16379 myself,slave eff5996aa6d5d9ab048a778246fcc1663322fe7d 0 1562064343000 5 connected e6f4def93bb888c144c4db308b5a7846d95d257b 172.1.50.11:6379@16379 master - 0 1562064344000 2 connected eff5996aa6d5d9ab048a778246fcc1663322fe7d 172.1.50.13:6379@16379 master - 0 1562064345000 0 connected 2c5395040cfb9611b515d0424f30c91eba1ec6e8 172.1.30.11:6379@16379 slave e6f4def93bb888c144c4db308b5a7846d95d257b 0 1562064345904 3 connected
d. Allocation slots
[The Redis cluster maps all data to 16384 slots.Each key is mapped to a fixed slot, and the key commands associated with these slots can only be responded to if the nodes have slots assigned.]
Manual allocation is required in batch, Invalid or out of range slot, Baidu:
Redis-cli-h server IP-p port number cluster addslots {0..5460} Many of these were useless, and later found a god's redis ADDSLOTS Supports Interval Input Implementation And repackage the compiled mirror OK by replacing everything in ifelse{} with functions.Effect:
172.1.50.13:6379> cluster slots 1) 1) (integer) 0 2) (integer) 5461 3) 1) "172.1.50.11" 2) (integer) 6379 3) "e6f4def93bb888c144c4db308b5a7846d95d257b" 4) 1) "172.1.30.11" 2) (integer) 6379 3) "2c5395040cfb9611b515d0424f30c91eba1ec6e8" 2) 1) (integer) 5462 2) (integer) 10922 3) 1) "172.1.50.12" 2) (integer) 6379 3) "c9f17946ca2c22a6dd0269614293e1bf38ae869b" 4) 1) "172.1.30.12" 2) (integer) 6379 3) "a0a5d4e10d97ba63fbf4f6eba3f4cf1f73d53423" 3) 1) (integer) 10923 2) (integer) 16383 3) 1) "172.1.50.13" 2) (integer) 6379 3) "eff5996aa6d5d9ab048a778246fcc1663322fe7d" 4) 1) "172.1.30.13" 2) (integer) 6379 3) "67cf166b61e7d892affa6d754a563b5993a9c5a3" 172.1.50.13:6379> 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 ... ...
Other operations can see command help or Official Web.
e. Test to add nodes, remove nodes
Container Name Container IP Address Mapping Port Number Service Running Mode redis-master4 172.1.50.21 6381->6379,16381->6379 master redis-slave4 172.1.30.21 6382->6379,16382->6379 slave
docker stop clm4 cls4 docker rm clm4 cls4 docker run --name clm4 \ -p 6381:6379 -p 16381:16379 \ --restart=always \ --network=mybridge --ip=172.1.50.21 \ -v /root/tmp/dk/cluster_redis/6381/data:/data \ -v /root/tmp/dk/cluster_redis/6381:/etc/redis \ -d cffycls/redis5:cluster2 docker run --name cls4 \ -p 6382:6379 -p 16382:16379 \ --restart=always \ --network=mybridge --ip=172.1.30.21 \ -v /root/tmp/dk/cluster_redis/6382/data:/data \ -v /root/tmp/dk/cluster_redis/6382:/etc/redis \ -d cffycls/redis5:cluster2
Similarly, after meet, replicate, look at cluster info:cluster_state:ok.But cluster slots are still above, slot allocation has not changed, need to continue
[
CLUSTER setslot <slot> node <node_id> assigns a slot to the specified node. If a slot has already been assigned to another node, ask the other node to delete the slot before assigning it.
CLUSTER setslot <slot> migrating <node_id>Migrates the slot of this node to the specified node.
CLUSTER setslot <slot> importing <node_id>Import slot from node_id specified node to this node.
]