欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

docker compose部署主从复制的实现

程序员文章站 2022-06-25 22:37:44
目录compose file受限于 redis 单点性能,加上我们对数据天生就有的备份的需求,因此 redis 提供了主从复制的服务。本文记录了通过 docker compose 搭建一主双从的 re...

受限于 redis 单点性能,加上我们对数据天生就有的备份的需求,因此 redis 提供了主从复制的服务。

本文记录了通过 docker compose 搭建一主双从的 redis 服务。

配置解析

################################# replication #################################

# 【slave】连接 master 的配置
# slaveof 172.25.0.101 6379

# 【slave】只读模式
# slave-read-only yes

# 【slave】密码
# masterauth <master-password>

# 【slave】复制期间是否允许响应查询,可能会返回脏数据
# slave-serve-stale-data yes

# 【slave】slave 晋级为 master 的优先级,仅哨兵模式下生效
# slave-priority 100

# 【slave】slave 向 master 报告的自己的 ip
# slave-announce-ip 5.5.5.5

# 【slave】slave 向 master 报告的自己的端口
# slave-announce-port 1234

# 【slave】slave ping master 的时间间隔
# repl-ping-slave-period 10

# 【master/slave】超时时间
# repl-timeout 60

# 【master】diskless 就是直接将要复制的 rdb 文件写入到 socket 中,而不会先存储到磁盘上
repl-diskless-sync no

# 【master】若开启 diskless,会等待指定秒之后再进行复制,以便让更多客户端可以在窗口期内连接,并行传送
# repl-diskless-sync-delay 5

# 【master】是否开启 nagle 算法,可以减少流量占用,但会同步得慢些
repl-disable-tcp-nodelay no

# 【master】环形缓冲日志的大小,给 slave 断开之后重连使用,避免全量复制,默认 1mb
# repl-backlog-size 1mb

# 【master】当 master 断连所有 slave 指定时间后,master 会清空 backlog
# repl-backlog-ttl 3600

# 【master】当低于指定个 slave 连接时,master 拒绝所有写操作
# min-slaves-to-write 3

# 【master】当延迟高于指定秒数时,master 拒绝所有写操作
# min-slaves-max-lag 10

服务搭建

目录结构

replication/
├── docker-compose.yml
├── master
│   ├── data
│   └── redis.conf
├── slave1
│   ├── data
│   └── redis.conf
└── slave2
    ├── data
    └── redis.conf

compose file

定义了一个子网,方便操作,对外暴露 6371(master)、6372、6373 端口。

version: "3"

networks:
  redis-replication:
    driver: bridge
    ipam:
      config:
        - subnet: 172.25.0.0/24

services:
  master:
    image: redis
    container_name: redis-master
    ports:
      - "6371:6379"
    volumes:
      - "./master/redis.conf:/etc/redis.conf"
      - "./master/data:/data"
    command: ["redis-server", "/etc/redis.conf"]
    restart: always
    networks:
      redis-replication:
        ipv4_address: 172.25.0.101

  slave1:
    image: redis
    container_name: redis-slave-1
    ports:
      - "6372:6379"
    volumes:
      - "./slave1/redis.conf:/etc/redis.conf"
      - "./slave1/data:/data"
    command: ["redis-server", "/etc/redis.conf"]
    restart: always
    networks:
      redis-replication:
        ipv4_address: 172.25.0.102

  slave2:
    image: redis
    container_name: redis-slave-2
    ports:
      - "6373:6379"
    volumes:
      - "./slave2/redis.conf:/etc/redis.conf"
      - "./slave2/data:/data"
    command: ["redis-server", "/etc/redis.conf"]
    restart: always
    networks:
      redis-replication:
        ipv4_address: 172.25.0.103

实例配置

master:

基本不用配置,最简单的是指定一个端口就好了。

port 6379
protected-mode no

repl-diskless-sync no
repl-disable-tcp-nodelay no

slave:

实例的配置保持一致就可以了,因为定义了子网,不存在端口冲突。

port 6379
protected-mode no

slaveof 172.25.0.101 6379
slave-read-only yes
slave-serve-stale-data yes

启动服务

ocker-compose up -d
creating network "replication_redis-replication" with driver "bridge"
creating redis-slave-1 ... done
creating redis-master  ... done
creating redis-slave-2 ... done

查看 master 日志,可以看到接受了两个 slave 的复制请求:

1:m 18 aug 2021 15:50:31.772 * replica 172.25.0.102:6379 asks for synchronization
1:m 18 aug 2021 15:50:31.772 * full resync requested by replica 172.25.0.102:6379
1:m 18 aug 2021 15:50:31.772 * replication backlog created, my new replication ids are '5d27746f14ee9be9694d794f96de6ba14a669dd1' and '0000000000000000000000000000000000000000'
1:m 18 aug 2021 15:50:31.772 * starting bgsave for sync with target: disk
1:m 18 aug 2021 15:50:31.773 * background saving started by pid 19
19:c 18 aug 2021 15:50:31.777 * db saved on disk
19:c 18 aug 2021 15:50:31.777 * rdb: 0 mb of memory used by copy-on-write
1:m 18 aug 2021 15:50:31.822 * background saving terminated with success
1:m 18 aug 2021 15:50:31.823 * synchronization with replica 172.25.0.102:6379 succeeded
1:m 18 aug 2021 15:50:32.170 * replica 172.25.0.103:6379 asks for synchronization
1:m 18 aug 2021 15:50:32.170 * full resync requested by replica 172.25.0.103:6379
1:m 18 aug 2021 15:50:32.170 * starting bgsave for sync with target: disk
1:m 18 aug 2021 15:50:32.171 * background saving started by pid 20
20:c 18 aug 2021 15:50:32.175 * db saved on disk
20:c 18 aug 2021 15:50:32.175 * rdb: 0 mb of memory used by copy-on-write
1:m 18 aug 2021 15:50:32.225 * background saving terminated with success
1:m 18 aug 2021 15:50:32.226 * synchronization with replica 172.25.0.103:6379 succeeded

查看 slave 日志,可以看到连接建立的全过程:

1:s 18 aug 2021 15:50:31.771 * connecting to master 172.25.0.101:6379
1:s 18 aug 2021 15:50:31.771 * master <-> replica sync started
1:s 18 aug 2021 15:50:31.771 * non blocking connect for sync fired the event.
1:s 18 aug 2021 15:50:31.771 * master replied to ping, replication can continue...
1:s 18 aug 2021 15:50:31.772 * partial resynchronization not possible (no cached master)
1:s 18 aug 2021 15:50:31.773 * full resync from master: 5d27746f14ee9be9694d794f96de6ba14a669dd1:0
1:s 18 aug 2021 15:50:31.823 * master <-> replica sync: receiving 175 bytes from master to disk
1:s 18 aug 2021 15:50:31.823 * master <-> replica sync: flushing old data
1:s 18 aug 2021 15:50:31.823 * master <-> replica sync: loading db in memory
1:s 18 aug 2021 15:50:31.828 * loading rdb produced by version 6.2.5
1:s 18 aug 2021 15:50:31.828 * rdb age 0 seconds
1:s 18 aug 2021 15:50:31.828 * rdb memory usage when created 1.83 mb
1:s 18 aug 2021 15:50:31.829 * master <-> replica sync: finished with success

测试

登录 master,尝试写入新 key。

127.0.0.1:6371> set hello world
ok

登录 slave,查看能否读取到:

127.0.0.1:6372> get hello
"world"

slave 尝试写操作:

127.0.0.1:6372> set hello redis
(error) readonly you can't write against a read only replica.

到此这篇关于docker compose部署主从复制的实现的文章就介绍到这了,更多相关docker compose 主从复制内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!