部署redis集群

  1. 创建网卡
[root@ls-bPtysZTW ~]# docker network create redis --subnet 172.38.0.0/16
75919ea55f07a816244610bd3c1aac298716dbf4d0be80b0b686b600c8165e77
  1. 通过脚本构建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
done
  1. 依次启动6个redis容器
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 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

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

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

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

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
  1. 进入到任意一个容器里
#进入一个redis
docker exec -it redis-1 /bin/sh #redis是shell命令,没有bash命令
#创建集群(创建时提示是否设置上面的配置,直接输入yes即可)
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 #查看集群节点

在这里插入图片描述


集群测试

测试

#向集群里写入数据
127.0.0.1:6379> set abc test  # 数据写进了节点2
-> Redirected to slot [7638] located at 172.38.0.12:6379
OK
172.38.0.12:6379> get abc
"test"

模拟节点故障

此时我们模拟故障,停掉一个节点(刚刚写入数据的节点)

[root@ls-bPtysZTW ~]# docker stop redis-2
redis-2
#我们再次访问数据
172.38.0.12:6379> get abc
^[^H^C
/data # redis-cli -c
127.0.0.1:6379> get abc
-> Redirected to slot [7638] located at 172.38.0.16:6379
"test"
#查看节点信息
172.38.0.16:6379> cluster nodes
d34a69ce4bce097b36c1620f31fbeaa90fe65a76 172.38.0.14:6379@16379 slave 2a76fc512720e761ccdbf36cbd44be0d7af3c928 0 1647851135371 4 connected
727214221642d8b2f226cd9cd1ea52b6e7a1a31f 172.38.0.11:6379@16379 master - 0 1647851135069 1 connected 0-5460
c058281886151ea80c7f9b27d71165f930878117 172.38.0.16:6379@16379 myself,master - 0 1647851135000 7 connected 5461-10922   #从节点成master节点
0defb3ab247cc004143f6cf191742577febe8ab6 172.38.0.12:6379@16379 master,fail - 1647850593353 1647850591849 2 connected  #节点2已经故障
2a76fc512720e761ccdbf36cbd44be0d7af3c928 172.38.0.13:6379@16379 master - 0 1647851135000 3 connected 10923-16383
5c9b5b2213935195af3b4fa482aa54ea4e4d2323 172.38.0.15:6379@16379 slave 727214221642d8b2f226cd9cd1ea52b6e7a1a31f 0 1647851136374 5 connected
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐