docker网络+docker部署redis集群
自定义网络
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
--dridge :网络模式,桥接模式
--subnet:192.168.0.0/16设置子网
--gateway:192.168.0.1 设置网关
创建两个自定义镜像的容器
docker run -d -P --name tomcat-net-01 --net mynet tomcat
docker run -d -P --name tomcat-net-02 --net mynet tomcat
docker exec -it tomcat-net-01 ping tomcat-net-02 (测试结果自定义ping的通ip地址名字都随意使用)
docker exec -it tomcat-net-01 ping 192.168.0.3
当不同网关的容器的时候,无法直接进行联通,需要用到一个参数connect将容器添加到我们到网络中来
docker network connect mynet tomcat01 (添加自定义网关到这个容器内,实现容器互通)
实战部署redis集群
配置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
启动redis服务器
for port in $(seq 1 6); \
do \
docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
-v /mydata/redis/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net net-redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
done
构建cluster
docker exec -it redis-1 /bin/sh
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: 86169e30f33cac42da03be81ee9c08989319b6fc 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: dcd1c4b77e3ee6f76ced7efce49206a5cf650a04 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: 551e33a7071f0551d61341a06a31855fcd4fb393 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: 4053ac0a06cd54f4e0c10e3e9180c08deeab96c9 172.38.0.14:6379
replicates 551e33a7071f0551d61341a06a31855fcd4fb393
S: c9fbc7d4ce247fbc65461b7551a034a027b56f33 172.38.0.15:6379
replicates 86169e30f33cac42da03be81ee9c08989319b6fc
S: e37138802e45f47ea525cc3878ff87252561556b 172.38.0.16:6379
replicates dcd1c4b77e3ee6f76ced7efce49206a5cf650a04
Can I set the above configuration? (type 'yes' to accept): yes (这里会交互输入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.38.0.11:6379)
M: 86169e30f33cac42da03be81ee9c08989319b6fc 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: c9fbc7d4ce247fbc65461b7551a034a027b56f33 172.38.0.15:6379
slots: (0 slots) slave
replicates 86169e30f33cac42da03be81ee9c08989319b6fc
M: 551e33a7071f0551d61341a06a31855fcd4fb393 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: dcd1c4b77e3ee6f76ced7efce49206a5cf650a04 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: e37138802e45f47ea525cc3878ff87252561556b 172.38.0.16:6379
slots: (0 slots) slave
replicates dcd1c4b77e3ee6f76ced7efce49206a5cf650a04
S: 4053ac0a06cd54f4e0c10e3e9180c08deeab96c9 172.38.0.14:6379
slots: (0 slots) slave
replicates 551e33a7071f0551d61341a06a31855fcd4fb393
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
redis集群测试
172.38.0.13:6379> get a (我们的13主集群)
"b"
[root@localhost ~]# docker stop redis-3 (我们将13停止,测试一下这个高可用集群效果)
redis-3
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 172.38.0.14:6379 (14是13的从库,发现get到了键值在从库上,高可用架构搭建完毕!)
"b"
172.38.0.14:6379>