---------------------redis三主三从---------------------------------------------------------------
自定义network :
docker network create redis --subnet 172.38.0.0/16 利用脚本写六个配置文件 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 拉取并启动redis docker run -p 6371:6379 -p 16371:13679 --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:13679 --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 637${port}:6379 -p 1637${port}:13679 --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; \ 查看启动 [root@lvym ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eec266e38510 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 0.0.0.0:6376->6379/tcp, 0.0.0.0:16376->13679/tcp redis-6 b05e247481ac redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 35 seconds ago Up 35 seconds 0.0.0.0:6375->6379/tcp, 0.0.0.0:16375->13679/tcp redis-5 2911e0e1c29d redis:5.0.9-alpine3.11 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6374->6379/tcp, 0.0.0.0:16374->13679/tcp redis-4 d2de98b48e8a redis:5.0.9-alpine3.11 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6373->6379/tcp, 0.0.0.0:16373->13679/tcp redis-3 0c8717cf691a redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:6372->6379/tcp, 0.0.0.0:16372->13679/tcp redis-2 a4fc26eb694e redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:6371->6379/tcp, 0.0.0.0:16371->13679/tcp redis-1 进入容器 [root@lvym ~]# docker exec -it redis-1 /bin/sh /data # ls appendonly.aof nodes.conf 配置集群 /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 >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 172.38.0.15:6379 to 172.38.0.11:6379 Adding replica 172.38.0.16:6379 to 172.38.0.12:6379 Adding replica 172.38.0.14:6379 to 172.38.0.13:6379 M: a792a6c9ee085b880d4721f176d20a14dc96d93c 172.38.0.11:6379 slots:[0-5460] (5461 slots) master M: 0cbdb29c2e70dd460fa0eb303d9f9540ad8abd9e 172.38.0.12:6379 slots:[5461-10922] (5462 slots) master M: aa2fda8fa96ecbff1ab9da3ceea757f68c24a542 172.38.0.13:6379 slots:[10923-16383] (5461 slots) master S: 4a01710d60dfdc2a0e301ddac5e6e6daf8b45a5f 172.38.0.14:6379 replicates aa2fda8fa96ecbff1ab9da3ceea757f68c24a542 S: 20fa296ea250b1e5080c8deec20c71a55dafd273 172.38.0.15:6379 replicates a792a6c9ee085b880d4721f176d20a14dc96d93c S: 7997346a77f8f4d0bbc05585480f1da27e0d81b0 172.38.0.16:6379 replicates 0cbdb29c2e70dd460fa0eb303d9f9540ad8abd9e Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated ............ 登录集群 [OK] All 16384 slots covered. /data # redis-cli -c 127.0.0.1:6379> cluster nodes 4a01710d60dfdc2a0e301ddac5e6e6daf8b45a5f 172.38.0.14:6379@16379 slave aa2fda8fa96ecbff1ab9da3ceea757f68c24a542 0 1593489128518 4 connected 0cbdb29c2e70dd460fa0eb303d9f9540ad8abd9e 172.38.0.12:6379@16379 master - 0 1593489128000 2 connected 5461-10922 7997346a77f8f4d0bbc05585480f1da27e0d81b0 172.38.0.16:6379@16379 slave 0cbdb29c2e70dd460fa0eb303d9f9540ad8abd9e 0 1593489128926 6 connected 20fa296ea250b1e5080c8deec20c71a55dafd273 172.38.0.15:6379@16379 slave a792a6c9ee085b880d4721f176d20a14dc96d93c 0 1593489128518 5 connected a792a6c9ee085b880d4721f176d20a14dc96d93c 172.38.0.11:6379@16379 myself,master - 0 1593489128000 1 connected 0-5460 aa2fda8fa96ecbff1ab9da3ceea757f68c24a542 172.38.0.13:6379@16379 master - 0 1593489129941 3 connected 10923-16383 ........