docker 搭建伪分布模式redis机器

1、创建网络

创建:docker network create redis-net

查看:docker network inspect redis-net

Subnet": "172.19.0.0/16

Containers中无分配的ip

2、redis配置文件模板

#bind 172.38.0.1${NUMBER}
port ${PORT}
cluster-enabled yes
cluster-config-file nodes_${PORT}.conf
cluster-node-timeout 5000
#cluster-announce-ip 172.18.0.1${NUMBER}
#cluster-announce-ip 172.18.0.11
#cluster-announce-port ${PORT}
#cluster-announce-bus-port 1${PORT}
appendonly yes

3、根据模板创建目录和文件

#!/bin/bash
number=1
for port in $(seq 8010 8015); \
do \
  rm -rf ./${port} && \
  mkdir -p ./${port}/conf  \
  && PORT=${port} NUMBER=${number} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
  && mkdir -p ./${port}/data; \
  number=$(($number+1))
done

4、创建和启动节点

#!/bin/bash
number=1
for port in $(seq 8010 8015); \
do \
   docker run -it -d -p ${port}:${port} -p 1${port}:1${port} \
  --privileged=true -v /Users/上面3创建目录/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
  -v /Users/上面3创建目录/redis-cluster/${port}/data:/data \
  --restart always --name redis-${port} --net redis-net  \
  --sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; \
  number=$(($number+1))
done

5、进入单个接口创建集群(ip为Containers中无分配的ip分配的ip)

 docker exec -it redis-8010 /bin/bash

redis-cli --cluster create 172.19.0.2:8010 172.19.0.3:8011 172.19.0.4:8012 172.19.0.5:8013 172.19.0.6:8014 172.19.0.7:8015 --cluster-replicas 1

完成创建后显示

Adding replica 172.19.0.6:8014 to 172.19.0.2:8010
Adding replica 172.19.0.7:8015 to 172.19.0.3:8011
Adding replica 172.19.0.5:8013 to 172.19.0.4:8012
M: 5306c90636a4b7c2df5108caba6b6800cc859e3d 172.19.0.2:8010
   slots:[0-5460] (5461 slots) master
M: d6a28c87d83b39e11e9adad459a0cfcda4848199 172.19.0.3:8011
   slots:[5461-10922] (5462 slots) master
M: 2e4992b4a36168d63e48d8766fa85d37f0b70a93 172.19.0.4:8012
   slots:[10923-16383] (5461 slots) master
S: 9c3614c66ac64f8cec8c80a2d15979cfbc3f1f22 172.19.0.5:8013
   replicates 2e4992b4a36168d63e48d8766fa85d37f0b70a93
S: 0d362844457c8a7e4c54b8ee99d957a17877d815 172.19.0.6:8014
   replicates 5306c90636a4b7c2df5108caba6b6800cc859e3d
S: bb1f5ed102cdf157821946ee238148b83bd19716 172.19.0.7:8015
   replicates d6a28c87d83b39e11e9adad459a0cfcda4848199
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 172.19.0.2:8010)
M: 5306c90636a4b7c2df5108caba6b6800cc859e3d 172.19.0.2:8010
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: d6a28c87d83b39e11e9adad459a0cfcda4848199 172.19.0.3:8011
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 0d362844457c8a7e4c54b8ee99d957a17877d815 172.19.0.6:8014
   slots: (0 slots) slave
   replicates 5306c90636a4b7c2df5108caba6b6800cc859e3d
S: bb1f5ed102cdf157821946ee238148b83bd19716 172.19.0.7:8015
   slots: (0 slots) slave
   replicates d6a28c87d83b39e11e9adad459a0cfcda4848199
S: 9c3614c66ac64f8cec8c80a2d15979cfbc3f1f22 172.19.0.5:8013
   slots: (0 slots) slave
   replicates 2e4992b4a36168d63e48d8766fa85d37f0b70a93
M: 2e4992b4a36168d63e48d8766fa85d37f0b70a93 172.19.0.4:8012
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

6、连接集群

redis-cli -c -p 8010

你可能感兴趣的:(物联网,docker,java,容器)