Docker搭建Redis集群

Docker搭建Redis集群

创建一个专属redis的网络

docker network create redis --subnet 172.38.0.0/16

通过shell脚本创建并启动6个redis服务

#通过脚本一次创建6个redis配置
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
# 通过脚本一次启动6个redis容器
docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
-v /mydata/redis/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
done

Docker搭建Redis集群_第1张图片
进入某个redis中

docker exec -it redis-1 /bin/sh

创建集群配置

 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

Docker搭建Redis集群_第2张图片
进入集群

/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:399
cluster_stats_messages_pong_sent:424
cluster_stats_messages_sent:823
cluster_stats_messages_ping_received:419
cluster_stats_messages_pong_received:399
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:823
127.0.0.1:6379> cluster nodes
0a6f57dd64c4fa6199bb8b50134301f9e444eb1b 172.38.0.13:6379@16379 master - 0 1700491426631 3 connected 10923-16383
870383b25a12474bcf5cc63464bdd29d5a4dbf58 172.38.0.14:6379@16379 slave 0a6f57dd64c4fa6199bb8b50134301f9e444eb1b 0 1700491426132 4 connected
3b3ecd8426c8314b9a0ffc4be2f4bc0e5adafa31 172.38.0.12:6379@16379 master - 0 1700491427533 2 connected 5461-10922
de3f56bd83ad82dcd9dc091f29a8c99e662fb7df 172.38.0.11:6379@16379 myself,master - 0 1700491426000 1 connected 0-5460
46d53036f6f3c411f2878da5d5e0f3a8f577bb88 172.38.0.16:6379@16379 slave 3b3ecd8426c8314b9a0ffc4be2f4bc0e5adafa31 0 1700491426532 6 connected
aca624d266ba3c0683e889e7491df4ac6a813909 172.38.0.15:6379@16379 slave de3f56bd83ad82dcd9dc091f29a8c99e662fb7df 0 1700491425630 5 connected

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
"b"


你可能感兴趣的:(docker,redis,java)