Docker28:Redis集群

Docker28:Redis集群

Created
Nov 18, 2021 02:26 AM
Last Edited
Last updated December 2, 2021
Tags

1. 说明

分片+ 高可用+ 负载均衡
三主三从
# 拉取redis镜像 docker pull redis:latest

2. 创建Redis网络

# 创建自定义redis网络 root@docker:/home/henggao/Desktop# docker network create redis --subnet 172.38.0.0/16 a186dbfeae7e588590e3eea18f8e4eda3ff3a9c5134cc02a984d766835249a7e # 查看网络 root@docker:/home/henggao/Desktop# docker network ls NETWORK ID NAME DRIVER SCOPE bca0a8305f26 bridge bridge local 9d5d46f3b050 host host local adc9329dead3 mynet bridge local 591d8f64be59 none null local a186dbfeae7e redis bridge local 5d91f2e85a2b somenetwork bridge local # 查看自定义redis网络信息,目前没有网络 root@docker:/home/henggao/Desktop# docker network inspect redis [ { "Name": "redis", "Id": "a186dbfeae7e588590e3eea18f8e4eda3ff3a9c5134cc02a984d766835249a7e", "Created": "2021-11-18T11:06:06.036879264+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.38.0.0/16" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": {} } ] root@docker:/home/henggao/Desktop#
 

3. 创建配置文件

  • 本机创建文件
# 使用脚本创建六个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
notion image
 
查看信息
root@docker:/home# cd /mydata/ root@docker:/mydata# ls redis root@docker:/home# cd /mydata/redis/ root@docker:/mydata/redis# ls node-1 node-2 node-3 node-4 node-5 node-6 root@docker:/mydata/redis# cd node-1 root@docker:/mydata/redis/node-1# ls conf root@docker:/mydata/redis/node-1# cd conf/ root@docker:/mydata/redis/node-1/conf# ls redis.conf root@docker:/mydata/redis/node-1/conf# cat 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.11 cluster-announce-port 6379 cluster-announce-bus-port 16379 appendonly yes root@docker:/mydata/redis/node-1/conf#
 

4. 启动集群

4.1 创建容器

# 启动第1个redis服务:外部暴露端口号:6371、 卷挂载、 网络为:172.38.0.11 docker run -p 6371:6379 -p 16371:16379 --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 # 使用脚本启动6个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 redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf done
成功启动如图
notion image
 

4.2 进入容器

docker exec -it redis-1 /bin/sh

4.3 创建集群

# 搭建集群 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
notion image
创建时选择 yes 即可
notion image
连接redis,查看信息:
# 集群连接 redis-cli -c # 查看信息 cluster info # 查看节点 cluster nodes
notion image
 

4.4. 测试

  1. 我们创建一个k,v
set k1 v1
notion image
可以发现是 redis-3 处理了我们的命令.
  1. 此时我们模拟redis-3宕机,直接停掉 redis-3 容器,查看刚才的k1,是否可以获得?
root@docker:/home/henggao# docker stop redis-3 redis-3 root@docker:/home/henggao#
notion image
重新连接集群
# 1. Ctrl + c退出,重新集群 /data # redis-cli -c # 2. 查看数据 127.0.0.1:6379> get k1 -> Redirected to slot [12706] located at 172.38.0.14:6379 "v1" 172.38.0.14:6379>
我们可以发现,从redis-4获得了k1!!
notion image
查看nodes:
cluster nodes
notion image
redis-3启动失败,redis-4已经成为主节点了。