Docker builds a redis cluster (3 hosts and 3 slaves)

1. Create a network card

docker network create redis --subnet 172.38.0.0/16

by

docker network ls

Can be seen
Insert picture description here
View details

docker network inspect redis

Create a cluster

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

You can see that a node has appeared.
Insert picture description here
Check the configuration file and find that it has been written.
Insert picture description here
Start 6 services

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

Enter a cluster

docker exec -it redis-1 /bin/sh

Insert picture description here
Create a cluster

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

选择的时候输入yes

Connect to the cluster

redis-cli -c

Insert picture description here

The test
Insert picture description here
is 13 this host comes to save

We stop the host of 13 and
Insert picture description here
go to get a, and the slave will get to this value at 14.
Insert picture description here
View the details.
Insert picture description here
Here, 13 is written and the host is down 14 instead

Guess you like

Origin blog.csdn.net/qq_45432665/article/details/114268799