單臺伺服器使用 docker 學習 redis 主從複製

php_yt發表於2021-12-27

準備兩個redis容器(一主一從)

Dockerfile

FROM centos:latest
RUN groupadd -r redis \
&& useradd  -r -g redis redis
RUN yum -y update \
&&  yum -y install epel-release \
&& yum -y install redis \
&& yum -y install net-tools
EXPOSE 6379

建立映象

docker build -t redis:n1 .
[root@VM-0-8-centos docker]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED         SIZE
redis             n1        1c45d8f479c4   5 minutes ago   570MB
centos            latest    5d0da3dc9764   3 months ago    231MB

自定義網路

docker network create --subnet=172.10.0.0/16 mynetwork
[root@VM-0-8-centos docker]# docker network ls
NETWORK ID     NAME        DRIVER    SCOPE
a97e473101f2   bridge      bridge    local
0cbd477b0468   host        host      local
60d857da579f   mynetwork   bridge    local
8d749f5fca60   none        null      local

建立兩個容器

docker run -itd --name redis-master --net mynetwork --ip 172.10.0.2 - p 6380:6379 redis:n1
docker run -itd --name redis-slave --net mynetwork --ip 172.10.0.3 -p 6381:6379 redis:n1
[root@VM-0-8-centos docker]# docker ps
CONTAINER    ID    IMAGE    COMMAND        CREATED        STATUS        PORTS    NAMES
d4fce2508b76   redis:n1   "/bin/bash"   5 seconds ago   Up 2 seconds   0.0.0.0:6381->6379/tcp   redis-slave
a591743386ac   redis:n1   "/bin/bash"   21 hours ago    Up 21 hours    0.0.0.0:6380->6379/tcp   redis-master

配置主從

進入主節點容器

docker exec -it redis-master bash
vi /etc/redis.conf

搜尋 :/bind :/requirepass

#bind 127.0.0.1
#yyp複製一行,修改為
bind 0.0.0.0

# requirepass foobared
requirepass mypassword

進入從節點容器

docker exec -it redis-slave bash
vi /etc/redis.conf

搜尋 :/bind :/repli

bind 0.0.0.0

# replicaof <masterip> <masterport>
#yyp複製一行,修改為
replicaof 172.10.0.2 6379

# masterauth <master-password>
masterauth mypassword

啟動主從節點

redis-server /etc/redis.conf
[root@a591743386ac /]# redis-server /etc/redis.conf &
[1] 130
[root@a591743386ac /]# ps ef | grep redis
  130 pts/2    Sl     0:00  \_ redis-server 0.0.0.0:6379 
[root@d4fce2508b76 /]# redis-server /etc/redis.conf &
[1] 86
[root@d4fce2508b76 /]# ps ef | grep redis
   86 pts/1    Sl     0:00  \_ redis-server 0.0.0.0:6379

向主節點新增資料

[root@a591743386ac /]# redis-cli
127.0.0.1:6379> auth mypassword
OK
127.0.0.1:6379> set name xiaoming EX 3600
OK
127.0.0.1:6379> get name
"xiaoming"

從節點讀取資料

[root@d4fce2508b76 /]# redis-cli
127.0.0.1:6379> get name
"xiaoming"

主節點複製資訊

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.10.0.3,port=6379,state=online,offset=6709,lag=1
master_replid:60453fd95d89c190c6d2818941bc359d42a003e1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:6709
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:6709

從節點複製資訊

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:172.10.0.2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:6504
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:60453fd95d89c190c6d2818941bc359d42a003e1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:6504
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:6504

通過日誌檢視複製過程

重新建立主從,通過日誌 /var/log/redis/redis.log檢視複製過程

#redis-slave 斷開
slaveof no one

redis-cli shutdown
echo "" > redis.log
redis-server /etc/redis.conf &
[root@a591743386ac redis]# cat redis.log

13:24:49.593 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
13:24:49.593 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=188, just started
13:24:49.593 # Configuration loaded
13:24:49.594 # Server initialized
13:24:49.594 * DB loaded from disk: 0.000 seconds
13:24:49.594 * Ready to accept connections
127.0.0.1:6379> set name xiaoming
OK

從節點複製主節點

[root@d4fce2508b76 redis]# redis-cli
127.0.0.1:6379> slaveof 172.10.0.2 6379
OK

從節點日誌 PID(86):S(slave) time *

[root@d4fce2508b76 redis]# tail -n 100 redis.log

86:S 26 Dec 2021 13:32:35.407 * Before turning into a replica, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
86:S 26 Dec 2021 13:32:35.407 * REPLICAOF 172.10.0.2:6379 enabled (user request from 'id=6 addr=127.0.0.1:44080 fd=7 name= age=19 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=44 qbuf-free=32724 obl=0 oll=0 omem=0 events=r cmd=slaveof')
86:S 26 Dec 2021 13:32:35.798 * Connecting to MASTER 172.10.0.2:6379
86:S 26 Dec 2021 13:32:35.798 * MASTER <-> REPLICA sync started
86:S 26 Dec 2021 13:32:35.798 * Non blocking connect for SYNC fired the event.
86:S 26 Dec 2021 13:32:35.799 * Master replied to PING, replication can continue...
86:S 26 Dec 2021 13:32:35.799 * Trying a partial resynchronization (request 6047b492dc93472747ffe1886df7be45cf4ee652:16496).
86:S 26 Dec 2021 13:32:35.800 * Full resync from master: e48fb81c4a47727d99b3e3d42fbabc0d4dbc2b84:0
86:S 26 Dec 2021 13:32:35.800 * Discarding previously cached master state.
86:S 26 Dec 2021 13:32:35.899 * MASTER <-> REPLICA sync: receiving 195 bytes from master
86:S 26 Dec 2021 13:32:35.899 * MASTER <-> REPLICA sync: Flushing old data
86:S 26 Dec 2021 13:32:35.899 * MASTER <-> REPLICA sync: Loading DB in memory
86:S 26 Dec 2021 13:32:35.899 * MASTER <-> REPLICA sync: Finished with success

主節點日誌 PID:M(master) Time *

188:M 26 Dec 2021 13:32:35.799 * Replica 172.10.0.3:6379 asks for synchronization
188:M 26 Dec 2021 13:32:35.799 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for '6047b492dc93472747ffe1886df7be45cf4ee652', my replication IDs are '3d39fde66ec20d44ff771afd46c3c57b547dcfc3' and '0000000000000000000000000000000000000000')
188:M 26 Dec 2021 13:32:35.799 * Starting BGSAVE for SYNC with target: disk
188:M 26 Dec 2021 13:32:35.799 * Background saving started by pid 197
197:C 26 Dec 2021 13:32:35.805 * DB saved on disk
197:C 26 Dec 2021 13:32:35.805 * RDB: 0 MB of memory used by copy-on-write
188:M 26 Dec 2021 13:32:35.899 * Background saving terminated with success
188:M 26 Dec 2021 13:32:35.899 * Synchronization with replica 172.10.0.3:6379 succeeded

然後主節點新寫入

127.0.0.1:6379> set age 18
OK

主節點日誌變化

188:M 27 Dec 2021 15:31:05.518 * 1 changes in 900 seconds. Saving...
188:M 27 Dec 2021 15:31:05.518 * Background saving started by pid 221
221:C 27 Dec 2021 15:31:05.526 * DB saved on disk
221:C 27 Dec 2021 15:31:05.526 * RDB: 0 MB of memory used by copy-on-write
188:M 27 Dec 2021 15:31:05.619 * Background saving terminated with success

從節點日誌變化

86:S 27 Dec 2021 15:31:05.565 * 1 changes in 900 seconds. Saving...
86:S 27 Dec 2021 15:31:05.565 * Background saving started by pid 154
154:C 27 Dec 2021 15:31:05.571 * DB saved on disk
154:C 27 Dec 2021 15:31:05.571 * RDB: 0 MB of memory used by copy-on-write
86:S 27 Dec 2021 15:31:05.666 * Background saving terminated with success

斷開復制127.0.0.1:6379> slaveof no one主節點日誌

188:M 27 Dec 2021 15:40:31.867 # Connection with replica 172.10.0.3:6379 lost.

斷開復制從節點日誌

86:M 27 Dec 2021 15:40:31.867 # Setting secondary replication ID to e48fb81c4a47727d99b3e3d42fbabc0d4dbc2b84, valid up to offset: 131206. New replication ID is d4408978063bbd9d424c4dbc91f7708144387de8
86:M 27 Dec 2021 15:40:31.867 # Connection with master lost.
86:M 27 Dec 2021 15:40:31.867 * Caching the disconnected master state.
86:M 27 Dec 2021 15:40:31.867 * Discarding previously cached master state.
86:M 27 Dec 2021 15:40:31.867 * MASTER MODE enabled (user request from 'id=11 addr=127.0.0.1:49134 fd=7 name= age=27 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=34 qbuf-free=32734 obl=0 oll=0 omem=0 events=r cmd=slaveof')
本作品採用《CC 協議》,轉載必須註明作者和本文連結
focus

相關文章