[root@--- ~]# docker network create redis --subnet 172.38.0.0/16 0350d84612bef09bc32b39f15ac6b608dae17cbd75d04b282c0bd37c0283bb7c [root@iZm5e23n3ueobwkjtfartxZ ~]# docker network ls NETWORK ID NAME DRIVER SCOPE feafa30d4051 bridge bridge local e8bf4fced9e2 host host local 0096a971fd2c mynet bridge local 6263db0933b9 none null local 0350d84612be redis bridge local 799426d70aa2 test-network bridge local [root@--- ~]#
创建6个redis服务
通过编写脚本创建6个redis服务
cluster-enabled:是否启动集群,选值:yes 、no
cluster-config-file 配置文件.conf :指定节点信息,自动生成
cluster-node-timeout 毫秒值: 配置节点连接超时时间
appendonly:是否开启持久化,选值:yes、no
daemonize: 守护进程启动(后台启动)
在/var目录下创建脚本文件create_redis_script.sh
1
touch create_redis_script.sh
编写脚本内容
vim create_redis_script.sh,写入下面的内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
for port in $(seq 1 6); \ do \ mkdir -p /var/redisNode/node-${port}/conf touch /var/redisNode/node-${port}/conf/redis.conf cat << EOF >/var/redisNode/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 #daemonize yes EOF done
for port in $(seq 1 6); \ do \ echo${port} docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \ -v /var/redisNode/node-${port}/data:/data \ -v /var/redisNode/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
赋予start_redis_script.sh文件可执行权限
1
[root@--- ~]# chmod o+x start_redis_script.sh
执行脚本,启动redis容器服务
./start_redis_script.sh
查看redis容器
1 2 3 4 5 6 7 8 9
[root@--- var]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0d3c18b61098 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 2 seconds ago Up 1 second 0.0.0.0:6376->6379/tcp, 0.0.0.0:16376->16379/tcp redis-6 ee896fb093da redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 13 seconds ago Up 12 seconds 0.0.0.0:6375->6379/tcp, 0.0.0.0:16375->16379/tcp redis-5 a83bb7b16dff redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 48 seconds ago Up 47 seconds 0.0.0.0:6374->6379/tcp, 0.0.0.0:16374->16379/tcp redis-4 ffe4c9619bed redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 58 seconds ago Up 57 seconds 0.0.0.0:6373->6379/tcp, 0.0.0.0:16373->16379/tcp redis-3 a316a31e273f redis:5.0.9-alpine3.11 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6372->6379/tcp, 0.0.0.0:16372->16379/tcp redis-2 71c072f57c29 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 0.0.0.0:6371->6379/tcp, 0.0.0.0:16371->16379/tcp redis-1 [root@--- var]#
172.38.0.13:6379> get a Could not connect to Redis at 172.38.0.13:6379: Host is unreachable (34.04s) not connected> exit # 重连集群 /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>