前言
如果你也因为centos的gcc版本落后导致redis编译安装不通过而烦恼,大可以试一试用docker去搭建redis集群。本文以单节点搭建redis-cluster三主三从为例。
- ip:192.168.0.10
- redis信息:
- 客户端连接端口:7001 -7006
- 集群间通信端口:17001 - 17006
- 密码:123456
- 版本:
- docker:18.06
- docker-compose:1.24
- redis:6.2.4
- centos: 7.4
准备
- 下载redis镜像
docker pull redis:6.2.4
- 准备redis的配置文件:redis.conf。主要修改端口和ip
port 7001 cluster-enabled yes cluster-config-file nodes-7001.conf cluster-node-timeout 5000 appendonly yes protected-mode no requirepass 123456 masterauth 123456 cluster-announce-ip 192.168.0.10 cluster-announce-port 7001 cluster-announce-bus-port 17001
- 准备持久化目录
mkdir -p /home/web/redis cd /home/web/redis for i in $(seq 7001 7006); do mkdir -p /home/web/redis/node_${i}/{conf,data};done # 或者: for i in $(seq 7001 7006); do mkdir -p ./node_${i}/{conf,data};done # 假设redis.conf在 /home/web/redis,且当前目录为/home/web/redis for i in $(seq 7001 7006); do sed "s/7001/${i}/g" /home/web/redis/redis.conf > /home/web/redis/node_${i}/conf/redis.conf;done # 或者: for i in $(seq 7001 7006); do sed "s/7001/${i}/g" ./redis.conf > ./node_${i}/conf/redis.conf;done
配置docker-compose.yaml
- 编写docker-compose.yaml(注意路径、密码、IP和端口号)
version: "3" # 定义服务,可以多个 services: redis-7001: # 服务名称 image: redis:6.2.4 # 创建容器时所需的镜像 container_name: redis-7001 # 容器名称 restart: always # 容器总是重新启动 volumes: # 数据卷,目录挂载 - /home/web/redis/node_7001/conf/redis.conf:/usr/local/etc/redis/redis.conf - /home/web/redis/node_7001/data:/data ports: - 7001:7001 - 17001:17001 command: redis-server /usr/local/etc/redis/redis.conf redis-7002: # 服务名称 image: redis:6.2.4 # 创建容器时所需的镜像 container_name: redis-7002 # 容器名称 restart: always # 容器总是重新启动 volumes: # 数据卷,目录挂载 - /home/web/redis/node_7002/conf/redis.conf:/usr/local/etc/redis/redis.conf - /home/web/redis/node_7002/data:/data ports: - 7002:7002 - 17002:17002 command: redis-server /usr/local/etc/redis/redis.conf redis-7003: # 服务名称 image: redis:6.2.4 # 创建容器时所需的镜像 container_name: redis-7003 # 容器名称 restart: always # 容器总是重新启动 volumes: # 数据卷,目录挂载 - /home/web/redis/node_7003/conf/redis.conf:/usr/local/etc/redis/redis.conf - /home/web/redis/node_7003/data:/data ports: - 7003:7003 - 17003:17003 command: redis-server /usr/local/etc/redis/redis.conf redis-7004: # 服务名称 image: redis:6.2.4 # 创建容器时所需的镜像 container_name: redis-7004 # 容器名称 restart: always # 容器总是重新启动 volumes: # 数据卷,目录挂载 - /home/web/redis/node_7004/conf/redis.conf:/usr/local/etc/redis/redis.conf - /home/web/redis/node_7004/data:/data ports: - 7004:7004 - 17004:17004 command: redis-server /usr/local/etc/redis/redis.conf redis-7005: # 服务名称 image: redis:6.2.4 # 创建容器时所需的镜像 container_name: redis-7005 # 容器名称 restart: always # 容器总是重新启动 volumes: # 数据卷,目录挂载 - /home/web/redis/node_7005/conf/redis.conf:/usr/local/etc/redis/redis.conf - /home/web/redis/node_7005/data:/data ports: - 7005:7005 - 17005:17005 command: redis-server /usr/local/etc/redis/redis.conf redis-7006: # 服务名称 image: redis:6.2.4 # 创建容器时所需的镜像 container_name: redis-7006 # 容器名称 restart: always # 容器总是重新启动 volumes: # 数据卷,目录挂载 - /home/web/redis/node_7006/conf/redis.conf:/usr/local/etc/redis/redis.conf - /home/web/redis/node_7006/data:/data ports: - 7006:7006 - 17006:17006 command: redis-server /usr/local/etc/redis/redis.conf
- 启动
docker-compose -f ./docker-compose.yaml up -d # 如果docker-compose.yaml文件在当前路径: # docker-compose up -d
搭建集群
- 进入任意一个redis容器
docker exec -it redis-7001 bash
- 创建集群
# 注意IP、端口号和密码 echo -e 'yes' | redis-cli -a 123456 --cluster create 192.168.0.10:7001 192.168.0.10:7002 192.168.0.10:7003 192.168.0.10:7004 192.168.0.10:7005 192.168.0.10:7006 --cluster-replicas 1
- 检查集群状态
# 查看集群状态 redis-cli -a 123456 -h 192.168.0.10 -p 7001 cluster info # 查看集群节点 redis-cli -a 123456 -h 192.168.0.10 -p 7001 cluster nodes