docker搭建Redis集群-主从复制
 编辑于 2021-08-04 19:38:31 阅读 1797
一主二从

代码目录
│ docker-compose.yml
├─apache
│	Dockerfile
│	index.php
├─follower
│	Dockerfile
│	run.sh
└─leader
	Dockerfile
docker-compose.yml
# docker-compose.yml
# tell docker what version of the docker-compose.yml we're using
version: '3'
# define the network
networks:
  web-network:
# start the services section
services:
  # define the name of our service
  # corresponds to the "--name" parameter
  apache:
    build:
      context: ./apache
    # defines the port mapping
    # corresponds to the "-p" flag
    ports:
      - 80:80
    tty: true
    volumes:
      - ./apache:/var/www/html
    networks:
      - web-network
  redis-leader:
    build:
      context: ./leader
    tty: true
    networks:
      - web-network
  redis-follower:
    build:
      context: ./follower
    tty: true
    networks:
      - web-network
    deploy:
      replicas: 2
apache/index.php
# apache/index.php
<?php
# 访问链接http://localhost/,测试结果
$redis=new Redis;
$redis->connect('redis-leader', '6379');
$redis->set('aa', 'aa123');
$redis->connect('redis-follower', '6379');
echo $redis->get('aa');
apache/Dockerfile
FROM php:7.4-apache
RUN pecl install redis-5.3.4 \
    && docker-php-ext-enable redis
# 将apache目录下的文件复制到容器内/var/www/html
# COPY . .
follower/Dockerfile
# follower/Dockerfile
FROM redis:6.2.5
ADD run.sh /run.sh
RUN chmod a+x /run.sh
CMD /run.sh
follower/run.sh
# follower/run.sh
#!/bin/bash
redis-server --replicaof redis-leader 6379
leader/Dockerfile
# leader/Dockerfile
FROM redis:6.2.5
