docker compose部署主從復(fù)制的實(shí)現(xiàn)
受限于 Redis 單點(diǎn)性能,加上我們對(duì)數(shù)據(jù)天生就有的備份的需求,因此 Redis 提供了主從復(fù)制的服務(wù)。
本文記錄了通過(guò) docker compose 搭建一主雙從的 Redis 服務(wù)。
配置解析
################################# REPLICATION ################################# # 【Slave】連接 Master 的配置 # slaveof 172.25.0.101 6379 # 【Slave】只讀模式 # slave-read-only yes # 【Slave】密碼 # masterauth <master-password> # 【Slave】復(fù)制期間是否允許響應(yīng)查詢(xún),可能會(huì)返回臟數(shù)據(jù) # slave-serve-stale-data yes # 【Slave】Slave 晉級(jí)為 Master 的優(yōu)先級(jí),僅哨兵模式下生效 # slave-priority 100 # 【Slave】Slave 向 Master 報(bào)告的自己的 IP # slave-announce-ip 5.5.5.5 # 【Slave】Slave 向 Master 報(bào)告的自己的端口 # slave-announce-port 1234 # 【Slave】Slave ping Master 的時(shí)間間隔 # repl-ping-slave-period 10 # 【Master/Slave】超時(shí)時(shí)間 # repl-timeout 60 # 【Master】Diskless 就是直接將要復(fù)制的 RDB 文件寫(xiě)入到 Socket 中,而不會(huì)先存儲(chǔ)到磁盤(pán)上 repl-diskless-sync no # 【Master】若開(kāi)啟 Diskless,會(huì)等待指定秒之后再進(jìn)行復(fù)制,以便讓更多客戶端可以在窗口期內(nèi)連接,并行傳送 # repl-diskless-sync-delay 5 # 【Master】是否開(kāi)啟 Nagle 算法,可以減少流量占用,但會(huì)同步得慢些 repl-disable-tcp-nodelay no # 【Master】環(huán)形緩沖日志的大小,給 Slave 斷開(kāi)之后重連使用,避免全量復(fù)制,默認(rèn) 1mb # repl-backlog-size 1mb # 【Master】當(dāng) Master 斷連所有 Slave 指定時(shí)間后,Master 會(huì)清空 backlog # repl-backlog-ttl 3600 # 【Master】當(dāng)?shù)陀谥付▊€(gè) Slave 連接時(shí),Master 拒絕所有寫(xiě)操作 # min-slaves-to-write 3 # 【Master】當(dāng)延遲高于指定秒數(shù)時(shí),Master 拒絕所有寫(xiě)操作 # min-slaves-max-lag 10
服務(wù)搭建
目錄結(jié)構(gòu)
replication/
├── docker-compose.yml
├── master
│ ├── data
│ └── redis.conf
├── slave1
│ ├── data
│ └── redis.conf
└── slave2
├── data
└── redis.conf
Compose File
定義了一個(gè)子網(wǎng),方便操作,對(duì)外暴露 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
實(shí)例配置
Master:
基本不用配置,最簡(jiǎn)單的是指定一個(gè)端口就好了。
port 6379 protected-mode no repl-diskless-sync no repl-disable-tcp-nodelay no
Slave:
實(shí)例的配置保持一致就可以了,因?yàn)槎x了子網(wǎng),不存在端口沖突。
port 6379 protected-mode no slaveof 172.25.0.101 6379 slave-read-only yes slave-serve-stale-data yes
啟動(dòng)服務(wù)
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 日志,可以看到接受了兩個(gè) Slave 的復(fù)制請(qǐng)求:
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 日志,可以看到連接建立的全過(guò)程:
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
測(cè)試
登錄 Master,嘗試寫(xiě)入新 Key。
127.0.0.1:6371> set hello world OK
登錄 Slave,查看能否讀取到:
127.0.0.1:6372> get hello "world"
Slave 嘗試寫(xiě)操作:
127.0.0.1:6372> set hello redis (error) READONLY You can't write against a read only replica.
到此這篇關(guān)于docker compose部署主從復(fù)制的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)docker compose 主從復(fù)制內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信