redis 搭建cluster主从集群
2023-08-10 本文已影响0人
AGEGG
准备配置模板
- redis-cluster.tmpl
注意设置为虚拟机可连接的ip地址 cluster-announce-ip 192.168.184
port ${PORT}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.184
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes
生成配置文件
- 脚本redis-create-config.sh
8010 8015表示从8010端口到8015端口
for port in $(seq 8010 8015);
do
mkdir -p ./${port}/conf
PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf
mkdir -p ./${port}/data;
done
执行脚本后生成目录结构:
appendonly.aof, dump.rdb, nodes.conf为运行后产物
├── 8010
│ ├── conf
│ │ └── redis.conf
│ └── data
│ ├── appendonly.aof
│ ├── dump.rdb
│ └── nodes.conf
├── 8011
│ ├── conf
│ │ └── redis.conf
│ └── data
│ ├── appendonly.aof
│ ├── dump.rdb
│ └── nodes.conf
├── 8012
│ ├── conf
│ │ └── redis.conf
│ └── data
│ ├── appendonly.aof
│ ├── dump.rdb
│ └── nodes.conf
├── 8013
│ ├── conf
│ │ └── redis.conf
│ └── data
│ ├── appendonly.aof
│ ├── dump.rdb
│ └── nodes.conf
├── 8014
│ ├── conf
│ │ └── redis.conf
│ └── data
│ ├── appendonly.aof
│ ├── dump.rdb
│ └── nodes.conf
├── 8015
│ ├── conf
│ │ └── redis.conf
│ └── data
│ ├── appendonly.aof
│ ├── dump.rdb
│ └── nodes.conf
创建Redis容器
- 脚本redis-create-container.sh
其中/app/redis/为执行以上所有脚本的当前路径,为我当前计算机的路径,改成你自己的。
for port in $(seq 8010 8015); \
do \
docker run -it -d -p ${port}:${port} -p 1${port}:1${port} \
--privileged=true -v /app/redis/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
--privileged=true -v /app/redis/${port}/data:/data \
--restart always --name redis-${port} --net bridge \
--sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; \
done
- 创建完成
执行脚本,创建完成后,使用docker ps查看redis集群容器
[root@localhost redis]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
578a06cd0ba6 redis "docker-entrypoint.s…" 17 hours ago Up 16 hours 0.0.0.0:8015->8015/tcp, :::8015->8015/tcp, 6379/tcp, 0.0.0.0:18015->18015/tcp, :::18015->18015/tcp redis-8015
33e01a3fe399 redis "docker-entrypoint.s…" 17 hours ago Up 16 hours 0.0.0.0:8014->8014/tcp, :::8014->8014/tcp, 6379/tcp, 0.0.0.0:18014->18014/tcp, :::18014->18014/tcp redis-8014
1d2626318692 redis "docker-entrypoint.s…" 17 hours ago Up 16 hours 0.0.0.0:8013->8013/tcp, :::8013->8013/tcp, 6379/tcp, 0.0.0.0:18013->18013/tcp, :::18013->18013/tcp redis-8013
58ec802c4a9a redis "docker-entrypoint.s…" 17 hours ago Up 16 hours 0.0.0.0:8012->8012/tcp, :::8012->8012/tcp, 6379/tcp, 0.0.0.0:18012->18012/tcp, :::18012->18012/tcp redis-8012
4554b2db67aa redis "docker-entrypoint.s…" 17 hours ago Up 16 hours 0.0.0.0:8011->8011/tcp, :::8011->8011/tcp, 6379/tcp, 0.0.0.0:18011->18011/tcp, :::18011->18011/tcp redis-8011
831f5f8b94e8 redis "docker-entrypoint.s…" 17 hours ago Up 16 hours 0.0.0.0:8010->8010/tcp, :::8010->8010/tcp, 6379/tcp, 0.0.0.0:18010->18010/tcp, :::18010->18010/tcp redis-8010
其他脚本
本文使用到了创建配置文件脚本redis-create-config.sh,创建Redis容器脚本redis-create-container.sh。但是启动、关闭容器仍需手动命令,图方便,便提供以下脚本。
启动脚本
redis-start.sh
for port in $(seq 8010 8015); \
do \
docker start redis-${port}; \
done
关闭脚本
redis-stop.sh
for port in $(seq 8010 8015); \
do \
docker stop redis-${port}; \
done
重启脚本
redis-restart.sh
for port in $(seq 8010 8015); \
do \
docker restart redis-${port}; \
done
禁止开机自启动脚本
redis-disable-autostart.sh
for port in $(seq 8010 8015); \
do \
docker update --restart=no redis-${port}; \
done
删除容器脚本
redis-rm-container.sh
sh redis-stop.sh
for port in $(seq 8010 8015); \
do \
docker rm redis-${port}; \
done
进入容器设置主从
docker exec -it redis-8010 bash
// redis-cli --cluster create 172.17.0.1:8010 172.17.0.1:8011 172.17.0.1:8012 172.17.0.1:8013 172.17.0.1:8014 172.17.0.1:8015 --cluster-replicas 1
redis-cli --cluster create 192.168.184.131:8010 192.168.184.131:8011 192.168.184.131:8012 192.168.184.131:8013 192.168.184.131:8014 192.168.184.131:8015 --cluster-replicas 1