Docker compose 快速构建redis-cluster
2021-03-21 本文已影响0人
灬臣独秀灬
前言:本环境为虚拟机搭建作为本地开发的redis-cluster 环境
(1)、软件说明:
1、redis 版本为 5.0.12
2、docker network 为host 模式
3、redis.sh 借助脚本动态配置redis端口
4、redis.conf 配置redis 集群
5、Dockerfile 制作自定义的redis 镜像
6、docker-compose 一键启动/卸载/重启 集群环境
(2)修改 redis.conf
protected-mode no
port $REDIS_PORT # 这里很重要,变量脚本会读取环境变量替换成配置的port
appendonly yes
cluster-enabled yes
cluster-config-file nodes-6379.conf
protected‐mode no
(3)编写 Dockerfile 文件
FROM redis:5.0.12
COPY redis.conf /usr/local/etc/redis/redis.conf
COPY start.sh /start.sh
RUN chmod 777 /start.sh
CMD ["/./start.sh"]
(4)编写 redis.sh 脚本
sed -i 's/$REDIS_PORT/'+$REDIS_PORT+'/g' /usr/local/etc/redis/redis.conf
redis-server /usr/local/etc/redis/redis.conf
(5)编写 docker-compose.yml脚本
version: '3.8'
services:
redis-7000:
image: redis-cluster:latest # 注意这里的版本号 是自己构建docker 镜像版本
container_name: redis-7000
environment:
- REDIS_PORT=7000
network_mode: host
privileged: true
redis-7001:
image: redis-cluster:latest
container_name: redis-7001
network_mode: host
environment:
- REDIS_PORT=7001
privileged: true
redis-7002:
image: redis-cluster:latest
network_mode: host
container_name: redis-7002
environment:
- REDIS_PORT=7002
privileged: true
redis-7003:
image: redis-cluster:latest
container_name: redis-7003
network_mode: host
environment:
- REDIS_PORT=7003
privileged: true
redis-7004:
image: redis-cluster:latest
container_name: redis-7004
network_mode: host
environment:
- REDIS_PORT=7004
privileged: true
redis-7005:
image: redis-cluster:latest
container_name: redis-7005
network_mode: host
privileged: true
environment:
- REDIS_PORT=7005
(6)所有文件上传服务器
image.png(7)依次执行命令
1 、docker build -t redis-cluster . # 构建redis 镜像 , 注意命令后面有个小数点,表示当前目录查找Dockerfile
2、docker-compose up -d
3、docker run -it --rm redis:5.0.12 redis-cli -h 192.168.137.132 -p 7000 --cluster create 192.168.137.132:7000 192.168.137.132:7001 192.168.137.132:7002 192.168.137.132:7003 192.168.137.132:7004 192.168.137.132:7005 --cluster-replicas 1 # 启动集群,注意改成自己的虚拟机的ip
4、查看集群信息 docker run -it --rm redis:5.0.12 redis-cli -h 192.168.137.132 -p 7000 info replication