Redis集群部署实战

 

 

 

 

 

 

 

1,创建Redis网络

 

[root@CentOs ~]# docker network create redis --subnet 172.38.0.0/16
1488802e6579f29b2818f2dae9f921d9ae1ad70011684e7784542cc0e24ce357
[root@CentOs ~]#

2,

创建Redis脚本

 

[root@CentOs ~]# 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
[root@CentOs ~]#

 

查看一下创建的

[root@CentOs ~]# cd /mydata/redis/node-1/conf/
[root@CentOs conf]# ls
redis.conf
[root@CentOs conf]# cat 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.11
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
[root@CentOs conf]#

 

3,启动运行容器,redis-1到redis-6

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

 

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

 

 

 

 

4,创建集群

 

进入redis-1中

[root@CentOs ~]# docker exec -it redis-1 /bin/sh
/data # ls
appendonly.aof  nodes.conf
/data #

/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

redis-cli --cluster(连接)

 

 

 

 

5,docker搭建redis集群完成!

 


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:174
cluster_stats_messages_pong_sent:179
cluster_stats_messages_sent:353
cluster_stats_messages_ping_received:174
cluster_stats_messages_pong_received:174
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:353
127.0.0.1:6379>

127.0.0.1:6379> cluster nodes
fd7a4c52dacfcd8dd852818a141414a79c5b4820 172.38.0.13:6379@16379 master - 0 1648132773108 3 connected 10923-16383
a35fd8970f117ad9e4bd3f02ad5c93ce424b98f3 172.38.0.11:6379@16379 myself,master - 0 1648132770000 1 connected 0-5460
fe5e752542f36dfca17321fb3d3bc6dc4f07ce6d 172.38.0.15:6379@16379 slave a35fd8970f117ad9e4bd3f02ad5c93ce424b98f3 0 1648132772506 5 connected
c0c7c068acef18a7fc155549b352bf381de60adc 172.38.0.14:6379@16379 slave fd7a4c52dacfcd8dd852818a141414a79c5b4820 0 1648132771599 4 connected
b941df7ec3a50189ef6488b7fa2a4228b0e398c6 172.38.0.12:6379@16379 master - 0 1648132772102 2 connected 5461-10922
4ae624fcc892dd7d63eedd0061fcf8b4962a5e4a 172.38.0.16:6379@16379 slave b941df7ec3a50189ef6488b7fa2a4228b0e398c6 0 1648132771095 6 connected
127.0.0.1:6379>

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>

 

测试:把redis-3停止了。再获取a,可以获取到吗

 

[root@CentOs ~]# docker stop redis-3
redis-3
[root@CentOs ~]#

 

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>

发现是可以的,是从机那里获取到的

 

再次查看:

172.38.0.14:6379> cluster nodes
fe5e752542f36dfca17321fb3d3bc6dc4f07ce6d 172.38.0.15:6379@16379 slave a35fd8970f117ad9e4bd3f02ad5c93ce424b98f3 0 1648135211000 5 connected
b941df7ec3a50189ef6488b7fa2a4228b0e398c6 172.38.0.12:6379@16379 master - 0 1648135211174 2 connected 5461-10922
a35fd8970f117ad9e4bd3f02ad5c93ce424b98f3 172.38.0.11:6379@16379 master - 0 1648135211000 1 connected 0-5460
fd7a4c52dacfcd8dd852818a141414a79c5b4820 172.38.0.13:6379@16379 master,fail - 1648135055064 1648135053957 3 connected
4ae624fcc892dd7d63eedd0061fcf8b4962a5e4a 172.38.0.16:6379@16379 slave b941df7ec3a50189ef6488b7fa2a4228b0e398c6 0 1648135212181 6 connected
c0c7c068acef18a7fc155549b352bf381de60adc 172.38.0.14:6379@16379 myself,master - 0 1648135210000 7 connected 10923-16383
172.38.0.14:6379>

我们可以看到13故障转移了,14变成master!

 

 

6,总结

我们使用docker之后,所有的技术都会慢慢的变得简单起来!

 

posted @ 2022-03-24 23:25  屯子里唯一的架构师  阅读(345)  评论(0编辑  收藏  举报