OS:CentOS Linux release 7.9.2009 (Core)
docker-ce-20.10.12-3.el7.x86_64
1. 创建集群网卡
docker network create redis --subnet 172.38.0.0/16
2. 创建配置文件
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
3.启动容器? 依次启动 ,共计启动6个:
docker run -p6371:6379 ?-p16371: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
docker run -p6376:6379 ?-p16376: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
修改各端口 容器名 各路径 ip 继续启动
....
4.进入容器 比如进入第一个?
? 执行集群创建
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
5. redis-cli? -c 连接集群? cluster info 查看集群信息? cluster nodes 查看节点信息
/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:46
cluster_stats_messages_pong_sent:50
cluster_stats_messages_sent:96
cluster_stats_messages_ping_received:45
cluster_stats_messages_pong_received:46
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:96
127.0.0.1:6379> cluster nodes
93d42a90c8f38dc1d54853806171b1ba2b61b17a 172.38.0.14:6379@16379 slave d01439921951667f493ec379dec2f20e408433fc 0 16456043315
70 4 connected3669b6f8745d3be3ee01305dd11d17bb5dd0d16c 172.38.0.16:6379@16379 slave 9fbfdcb3060724cede5cdbf954df82ee30a1b208 0 16456043316
73 6 connected8f80b7f091a8d08cd9d3648679a94d75408d5dca 172.38.0.15:6379@16379 slave 30bceadfb2a6174197430275563a1eba507377c7 0 16456043311
64 5 connected30bceadfb2a6174197430275563a1eba507377c7 172.38.0.11:6379@16379 myself,master - 0 1645604330000 1 connected 0-5460
d01439921951667f493ec379dec2f20e408433fc 172.38.0.13:6379@16379 master - 0 1645604332182 3 connected 10923-16383
9fbfdcb3060724cede5cdbf954df82ee30a1b208 172.38.0.12:6379@16379 master - 0 1645604332688 2 connected 5461-10922
6. set a b? ?宕机掉存储的节点? ?重新get a 可以查看到信息? 可以查看到节点变化
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
93d42a90c8f38dc1d54853806171b1ba2b61b17a 172.38.0.14:6379@16379 myself,master - 0 1645604481000 7 connected 10923-16383
30bceadfb2a6174197430275563a1eba507377c7 172.38.0.11:6379@16379 master - 0 1645604482551 1 connected 0-5460
8f80b7f091a8d08cd9d3648679a94d75408d5dca 172.38.0.15:6379@16379 slave 30bceadfb2a6174197430275563a1eba507377c7 0 16456044820
00 5 connectedd01439921951667f493ec379dec2f20e408433fc 172.38.0.13:6379@16379 master,fail - 1645604413286 1645604412267 3 connected
9fbfdcb3060724cede5cdbf954df82ee30a1b208 172.38.0.12:6379@16379 master - 0 1645604481133 2 connected 5461-10922
3669b6f8745d3be3ee01305dd11d17bb5dd0d16c 172.38.0.16:6379@16379 slave 9fbfdcb3060724cede5cdbf954df82ee30a1b208 0 16456044818
40 6 connected
|