docker pull redis
# 创建一个文件 名字叫create.sh touch create.sh # 编辑文件的内容 vim create.sh # ---------------- 文件内容开始 ----------------- for i in `seq 7001 7006` do mkdir -p ${i}/data done # ---------------- 文件内容结束 ---------------- # 编辑保存完成之后,运行这个脚本,创建出所需要的文件夹。 sh create.sh
修改redis.conf
配置文件的主要参数有:
port # 修改每个redis中的端口号 bind 0.0.0.0 # 表示绑定到哪个ip,0.0.0.0表示不论以什么IP来访问都会接受。 protected-mode no # 关闭保护模式 daemonize no # 关闭以守护式进程启动 cluster-enabled yes # 开启集群功能 cluster-config-file nodes-7001.conf # 修改每一个redis的集群配置文件 cluster-node-timeout 5000 # 节点超时时间
接下来就是编写docker-compsoe.yml
version: "3.4" x-image: &default-image redis x-restart: &default-restart always x-netmode: &default-netmode host services: redis1: image: *default-image restart: *default-restart container_name: redis-7001 ports: - "7001:7001" command: redis-server /etc/redis/redis.conf volumes: - ${PWD}/7001/data:/data - ${PWD}/7001/redis.conf:/etc/redis/redis.conf network_mode: *default-netmode redis2: image: *default-image restart: *default-restart container_name: redis-7002 ports: - "7002:7002" command: redis-server /etc/redis/redis.conf volumes: - ${PWD}/7002/data:/data - ${PWD}/7002/redis.conf:/etc/redis/redis.conf network_mode: *default-netmode redis3: image: *default-image restart: *default-restart container_name: redis-7003 ports: - "7003:7003" command: redis-server /etc/redis/redis.conf volumes: - ${PWD}/7003/data:/data - ${PWD}/7003/redis.conf:/etc/redis/redis.conf network_mode: *default-netmode redis4: image: *default-image restart: *default-restart container_name: redis-7004 ports: - "7004:7004" command: redis-server /etc/redis/redis.conf volumes: - ${PWD}/7004/data:/data - ${PWD}/7004/redis.conf:/etc/redis/redis.conf network_mode: *default-netmode redis5: image: *default-image restart: *default-restart container_name: redis-7005 ports: - "7005:7005" command: redis-server /etc/redis/redis.conf volumes: - ${PWD}/7005/data:/data - ${PWD}/7005/redis.conf:/etc/redis/redis.conf network_mode: *default-netmode redis6: image: *default-image restart: *default-restart container_name: redis-7006 ports: - "7006:7006" command: redis-server /etc/redis/redis.conf volumes: - ${PWD}/7006/data:/data - ${PWD}/7006/redis.conf:/etc/redis/redis.conf network_mode: *default-netmode
docker-compose.yml
的目录中输入docker-compose up -d # 以守护式进程的方式运行容器 docker-compsoe ps # 查看由 docker-compose 工具创建出来的容器 # docker-compose stop 停止由 docker-compose 工具创建出来的容器运行 # docker-compose rm 删除由 docker-compose 工具创建出来的容器
docker exec -it redis-7001 redis-cli -p 7001 \ --cluster create 192.168.20.255:7001 192.168.20.255:7002 192.168.20.255:7003 \ 192.168.20.255:7004 192.168.20.255:7005 192.168.20.255:7006 \ --cluster-replicas 1
Can I set the above configuration? (type 'yes' to accept)
时,输入 yes
,完成集群的设置。至此,基于 Docker 部署的 redis-cluster 集群就已经完成