以下内容皆参考至狂神说docker学习视频
1. 先创建一个自定义网络redis-net用于redis集群使用 docker network create redis --subnet 172.38.0.0/16
2. 配置redis需要对每个redis进行文件配置,编写shell脚本进行配置:
for port in $(seq 1 6); \
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
依次创建6个redis节点:
# 创建结点1
docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
-v /mydata/redis/node-1/data:/data \
-v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#创建结点2
docker run -p 6372:6379 -p 16372:16379 --name redis-2 \
-v /mydata/redis/node-2/data:/data \
-v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#创建结点3
docker run -p 6373:6379 -p 16373:16379 --name redis-3 \
-v /mydata/redis/node-3/data:/data \
-v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#创建结点4
docker run -p 6374:6379 -p 16374:16379 --name redis-4 \
-v /mydata/redis/node-4/data:/data \
-v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#创建结点5
docker run -p 6375:6379 -p 16375:16379 --name redis-5 \
-v /mydata/redis/node-5/data:/data \
-v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
#创建结点6
docker run -p 6376:6379 -p 16376:16379 --name redis-6 \
-v /mydata/redis/node-6/data:/data \
-v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
然后开始创建集群:
root@程哥哥Redmibook:~# docker network ls
NETWORK ID NAME DRIVER SCOPE
d1e115493bdb bridge bridge local
9cbacd058f0e host host local
7febd5f666b6 none null local
c4422238ac74 redis bridge local
root@程哥哥Redmibook:~# docker network inspect c4422238ac74
"ConfigOnly": false,
"Containers": {
"20c0a04a6b8bb58d1a28c709337aadf754b44fd422db3fc012aaaa2343994637": {
"Name": "redis-4",
"EndpointID": "69b341e65a2cf6e13a56d2a21d8681b1fa7380258cee327523ba3d8d93fa4510",
"MacAddress": "02:42:ac:26:00:0e",
"IPv4Address": "172.38.0.14/16",
"IPv6Address": ""
},
"54e448a5552b9c57560c4bbdfc02b708afb12f69354e83305c93b4f20c240113": {
"Name": "redis-3",
"EndpointID": "0a6caac3693a6a94bd89508c4f125716cb43ef664220557d7d168cdd90d2b2cd",
"MacAddress": "02:42:ac:26:00:0d",
"IPv4Address": "172.38.0.13/16",
"IPv6Address": ""
},
"6140f6e0e5c16fa7388682f99b09e7638223a8e057a860e7ec730cae33f9153a": {
"Name": "redis-2",
"EndpointID": "ffdcad369edfb42b5b40ac62ba5d3d4421987a6114488407c08cea252dbd82c6",
"MacAddress": "02:42:ac:26:00:0c",
"IPv4Address": "172.38.0.12/16",
"IPv6Address": ""
},
"c563a8c2c8cf2138320f49c657570ac5e031d8c2a1a263ddadf681bb073c9097": {
"Name": "redis-1",
"EndpointID": "b5a040e08a5fba46bf5d1424748a8d89942c44c1fd991049a869df1767688b4a",
"MacAddress": "02:42:ac:26:00:0b",
"IPv4Address": "172.38.0.11/16",
"IPv6Address": ""
},
"f4590e817fc3cf071dc208d55b799a8b4c2dacd05c7c181c5a5bfbdcc178d116": {
"Name": "redis-6",
"EndpointID": "673320acb8a70f70d7affefb4d1f7ba002b4253f49a3e9325998aeadeb42a8ba",
"MacAddress": "02:42:ac:26:00:10",
"IPv4Address": "172.38.0.16/16",
"IPv6Address": ""
},
"fb95ab1c92bc7f6b04021d24f75bf198615ae0077e596f181c1b4e02cbb95c94": {
"Name": "redis-5",
"EndpointID": "77cddef2d34bdac8d4066d26838b10766521c0be5459d1e10eb728c86b387d0c",
"MacAddress": "02:42:ac:26:00:0f",
"IPv4Address": "172.38.0.15/16",
"IPv6Address": ""
}
}
root@程哥哥Redmibook:~# docker exec -it redis-1 /bin/sh
/data # ls
appendonly.aof nodes.conf
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --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 172.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: bf7332080a68439649528ad4c982b6ef48cf3911 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: 917d7daa98b95714c96a224f4b3e9a4ddd31b50c 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: ccf417c98c8267b7a58466ccc6af965012d4263d 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: 4238513bb17cd4b3eb802f5b3ab04c6811ca7722 172.38.0.14:6379
replicates ccf417c98c8267b7a58466ccc6af965012d4263d
S: 04def0f423502014e410e90815c83d6e4462cd9b 172.38.0.15:6379
replicates bf7332080a68439649528ad4c982b6ef48cf3911
S: de97c500f4fded682781ef7d6e04919c6180808b 172.38.0.16:6379
replicates 917d7daa98b95714c96a224f4b3e9a4ddd31b50c
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 172.38.0.11:6379)
M: bf7332080a68439649528ad4c982b6ef48cf3911 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: de97c500f4fded682781ef7d6e04919c6180808b 172.38.0.16:6379
slots: (0 slots) slave
replicates 917d7daa98b95714c96a224f4b3e9a4ddd31b50c
S: 04def0f423502014e410e90815c83d6e4462cd9b 172.38.0.15:6379
slots: (0 slots) slave
replicates bf7332080a68439649528ad4c982b6ef48cf3911
S: 4238513bb17cd4b3eb802f5b3ab04c6811ca7722 172.38.0.14:6379
slots: (0 slots) slave
replicates ccf417c98c8267b7a58466ccc6af965012d4263d
M: 917d7daa98b95714c96a224f4b3e9a4ddd31b50c 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: ccf417c98c8267b7a58466ccc6af965012d4263d 172.38.0.13:6379
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.
docker搭建redis集群完成!
测试
部署成功!!!
root@程哥哥Redmibook:~# docker exec -it redis-1 /bin/sh
/data # ls
appendonly.aof nodes.conf
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379
172.38.0.16:6379 --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 172.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: bf7332080a68439649528ad4c982b6ef48cf3911 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: 917d7daa98b95714c96a224f4b3e9a4ddd31b50c 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: ccf417c98c8267b7a58466ccc6af965012d4263d 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: 4238513bb17cd4b3eb802f5b3ab04c6811ca7722 172.38.0.14:6379
replicates ccf417c98c8267b7a58466ccc6af965012d4263d
S: 04def0f423502014e410e90815c83d6e4462cd9b 172.38.0.15:6379
replicates bf7332080a68439649528ad4c982b6ef48cf3911
S: de97c500f4fded682781ef7d6e04919c6180808b 172.38.0.16:6379
replicates 917d7daa98b95714c96a224f4b3e9a4ddd31b50c
Can I set the above configuration? (type 'yes' to accept): yes
/data # redis-cli -c
127.0.0.1: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
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:708
cluster_stats_messages_pong_sent:722
cluster_stats_messages_sent:1430
cluster_stats_messages_ping_received:717
cluster_stats_messages_pong_received:708
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1430
127.0.0.1:6379> cluster nodes
de97c500f4fded682781ef7d6e04919c6180808b 172.38.0.16:6379@16379 slave 917d7daa98b95714c96a224f4b3e9a4ddd31b50c 0 1650879465132 6 connected
04def0f423502014e410e90815c83d6e4462cd9b 172.38.0.15:6379@16379 slave bf7332080a68439649528ad4c982b6ef48cf3911 0 1650879465000 5 connected
4238513bb17cd4b3eb802f5b3ab04c6811ca7722 172.38.0.14:6379@16379 slave ccf417c98c8267b7a58466ccc6af965012d4263d 0 1650879465634 4 connected
917d7daa98b95714c96a224f4b3e9a4ddd31b50c 172.38.0.12:6379@16379 master - 0 1650879465533 2 connected 5461-10922
bf7332080a68439649528ad4c982b6ef48cf3911 172.38.0.11:6379@16379 myself,master - 0 1650879464000 1 connected 0-5460
ccf417c98c8267b7a58466ccc6af965012d4263d 172.38.0.13:6379@16379 master - 0 1650879464028 3 connected 10923-16383
127.0.0.1:6379> set a b
-> Redirected to slot [15495] located at 172.38.0.13:6379
OK
172.38.0.13:6379> get a
^C
/data # redis-cli -c
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 172.38.0.14:6379
"b"
172.38.0.14:6379> cluster nodes
de97c500f4fded682781ef7d6e04919c6180808b 172.38.0.16:6379@16379 slave 917d7daa98b95714c96a224f4b3e9a4ddd31b50c 0 1650880013721 6 connected
ccf417c98c8267b7a58466ccc6af965012d4263d 172.38.0.13:6379@16379 master,fail - 1650879945358 1650879943000 3 connected
917d7daa98b95714c96a224f4b3e9a4ddd31b50c 172.38.0.12:6379@16379 master - 0 1650880012516 2 connected 5461-10922
bf7332080a68439649528ad4c982b6ef48cf3911 172.38.0.11:6379@16379 master - 0 1650880013000 1 connected 0-5460
4238513bb17cd4b3eb802f5b3ab04c6811ca7722 172.38.0.14:6379@16379 myself,master - 0 1650880013000 7 connected 10923-16383
04def0f423502014e410e90815c83d6e4462cd9b 172.38.0.15:6379@16379 slave bf7332080a68439649528ad4c982b6ef48cf3911 0 1650880014223 5 connected
172.38.0.14:6379>