分片+高可用+负载均衡
其中一个挂了,备用的顶上
redis集群
启动6个容器
创建redis集群网卡
docker network create redis --subnet 172.38.0.0/16
查看redis网卡的详细信息
docker network inspect redis
配置redis集群
通过脚本创建6个redis配置
for port in $(seq 1 6); \
do \
mkdir -p /data/redis/node-${port}/conf
touch /data/redis/node-${port}/conf/redis.conf
cat << EOF >/data/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
查看创建的文件
启动容器
#脚本
docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
-v /data/redis/node-${port}/data:/data \
-v /data/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1${port} redis:5.0.10-alpine3.12 redis-server /etc/redis/redis.conf; \
#手动
docker run -p 6371:6379 -p 16371:16379 --name redis-1 -v /data/redis/node-1/data:/data -v /data/redis/node-1/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.11 redis:5.0.10-alpine3.12 redis-server /etc/redis/redis.conf
查看启动容器
docker ps
进入redis容器
redis中没有bash命令,redis进入使用/bin/sh
docker exec -it IDNAME /bin/sh
进入后默认的目录为/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: 290353fdbf5a7345e66fee97be33fa4e352eadfb 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: 2d341c0a2aac0cd99a4cec112292bc7c48e608e2 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: 4d1882d9fa8f86d794bfad393ae982be4350338c 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: 8319c1e759a38114d5208ab329dc241b35289d28 172.38.0.14:6379
replicates 4d1882d9fa8f86d794bfad393ae982be4350338c
S: 8b81002b5dcef5041c5ca70605c8cc30a53d05e0 172.38.0.15:6379
replicates 290353fdbf5a7345e66fee97be33fa4e352eadfb
S: 530887eef54e59efe34b2a98f131fdc5cc6e1937 172.38.0.16:6379
replicates 2d341c0a2aac0cd99a4cec112292bc7c48e608e2
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.38.0.11:6379)
M: 290353fdbf5a7345e66fee97be33fa4e352eadfb 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 2d341c0a2aac0cd99a4cec112292bc7c48e608e2 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 4d1882d9fa8f86d794bfad393ae982be4350338c 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 8319c1e759a38114d5208ab329dc241b35289d28 172.38.0.14:6379
slots: (0 slots) slave
replicates 4d1882d9fa8f86d794bfad393ae982be4350338c
S: 8b81002b5dcef5041c5ca70605c8cc30a53d05e0 172.38.0.15:6379
slots: (0 slots) slave
replicates 290353fdbf5a7345e66fee97be33fa4e352eadfb
S: 530887eef54e59efe34b2a98f131fdc5cc6e1937 172.38.0.16:6379
slots: (0 slots) slave
replicates 2d341c0a2aac0cd99a4cec112292bc7c48e608e2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
连接集群
redis-cli -c
查看集群信息
6379>cluster info
查看节点信息
127.0.0.1:6379>cluster nodes
注:3主3从
set a b
主节点在处理
从机应该也有一个这样的值,主节点挂了,从节点应该替代上来。(高可用)
查看运行的容器
docker ps
停掉主节点
docker stop ID
再次get值,连接超时
再次连接,get a
master失败,从172.38.0.14从节点自动升为主节点
Docker搭建redis集群完成!
使用docker之后,所有的技术慢慢变的简单起来!