Redis基础:容器化方式的主从模式
程序员文章站
2022-05-17 22:41:00
...
Redis有主从、哨兵和集群三种常见模式,这篇文章使用Docker来说明Redis的主从模式的使用方式。
主从模式
主从模式之下,一般常见的方式Master负责写,SLAVE负责读。Redis提供了复制(Replication)功能用于数据同步,一个常见的流程如下所示:
- 从数据库连接主数据库,发送SYNC命令;
- 主数据库接收到SYNC命令后,开始执行BGSAVE命令生成RDB文件并使用缓冲区记录此后执行的所有写命令;
- 主数据库BGSAVE执行完后,向所有从数据库发送快照文件,并在发送期间继续记录被执行的写命令;
- 从数据库收到快照文件后丢弃所有旧数据,载入收到的快照;
- 主数据库快照发送完毕后开始向从数据库发送缓冲区中的写命令;
- 从数据库完成对快照的载入,开始接收命令请求,并执行来自主数据库缓冲区的写命令;(从数据库初始化完成)
- 主数据库每执行一个写命令就会向从数据库发送相同的写命令,从数据库接收并执行收到的写命令(从数据库初始化完成后的操作)
- 出现断开重连后,2.8之后的版本会将断线期间的命令传给重数据库,增量复制。
- 主从刚刚连接的时候,进行全量同步;全同步结束后,进行增量同步。当然,如果有需要,slave 在任何时候都可以发起全量同步。Redis 的策略是,无论如何,首先会尝试进行增量同步,如不成功,要求从机进行全量同步。
构建主从模式的Redis环境
本文将使用6.0.4的Redis的官方镜像为例,使用docker-compose一键生成一个一主两丛的Redis集群。
docker-compose.yml
liumiaocn:redis liumiao$ cat docker-compose.yml
version: '2'
services:
# redis master
master:
image: redis:6.0.4
container_name: redis-master
restart: always
command: redis-server --port 6379 --requirepass aaa@qq.com --appendonly yes
ports:
- 6379:6379
volumes:
- ./data:/data
# redis slave 1
slave1:
image: redis:6.0.4
container_name: redis-slave-1
restart: always
command: redis-server --slaveof 192.168.31.242 6379 --port 6380 --requirepass aaa@qq.com --masterauth aaa@qq.com --appendonly yes
ports:
- 6380:6380
volumes:
- ./data:/data
# redis slave 2
slave2:
image: redis:6.0.4
container_name: redis-slave-2
restart: always
command: redis-server --slaveof 192.168.31.242 6379 --port 6381 --requirepass aaa@qq.com --masterauth aaa@qq.com --appendonly yes
ports:
- 6381:6381
volumes:
- ./data:/data
liumiaocn:redis liumiao$
代码说明
- requirepass:指定密码,使用redis-cli连接进行操作时需要首先使用auth进行验证
- appendonly:开启redis的数据持久化
- masterauth:Slave端设定,需要与Master指定的密码一致,否则无法连接成功
- slaveof:Slave端设定,指定连接的Master的IP和端口号,也需要与Master保持一致
启动
liumiaocn:redis liumiao$ docker-compose up -d
Creating network "redis_default" with the default driver
Creating redis-master ... done
Creating redis-slave-1 ... done
Creating redis-slave-2 ... done
liumiaocn:redis liumiao$
启动确认
liumiaocn:redis liumiao$ docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------
redis-master docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
redis-slave-1 docker-entrypoint.sh redis ... Up 6379/tcp, 0.0.0.0:6380->6380/tcp
redis-slave-2 docker-entrypoint.sh redis ... Up 6379/tcp, 0.0.0.0:6381->6381/tcp
liumiaocn:redis liumiao$
liumiaocn:redis liumiao$
liumiaocn:redis liumiao$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
313a21d3c602 redis:6.0.4 "docker-entrypoint.s…" 14 seconds ago Up 13 seconds 6379/tcp, 0.0.0.0:6380->6380/tcp redis-slave-1
50243006a963 redis:6.0.4 "docker-entrypoint.s…" 14 seconds ago Up 13 seconds 6379/tcp, 0.0.0.0:6381->6381/tcp redis-slave-2
d4b577332c5f redis:6.0.4 "docker-entrypoint.s…" 14 seconds ago Up 13 seconds 0.0.0.0:6379->6379/tcp redis-master
liumiaocn:redis liumiao$
连接过程确认
Master日志
从如下日志的最后两行可以看到,Master端已经成功与两个Slave进行了连接
liumiaocn:redis liumiao$ docker-compose logs master
Attaching to redis-master
redis-master | 1:C 06 Jun 2020 00:44:10.566 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-master | 1:C 06 Jun 2020 00:44:10.566 # Redis version=6.0.4, bits=64, commit=00000000, modified=0, pid=1, just started
redis-master | 1:C 06 Jun 2020 00:44:10.566 # Configuration loaded
redis-master | 1:M 06 Jun 2020 00:44:10.573 * Running mode=standalone, port=6379.
redis-master | 1:M 06 Jun 2020 00:44:10.573 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis-master | 1:M 06 Jun 2020 00:44:10.573 # Server initialized
redis-master | 1:M 06 Jun 2020 00:44:10.573 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis-master | 1:M 06 Jun 2020 00:44:10.576 * Ready to accept connections
redis-master | 1:M 06 Jun 2020 00:44:10.606 * Replica 192.168.240.1:6380 asks for synchronization
redis-master | 1:M 06 Jun 2020 00:44:10.606 * Full resync requested by replica 192.168.240.1:6380
redis-master | 1:M 06 Jun 2020 00:44:10.606 * Replication backlog created, my new replication IDs are '543ebff4abe27ada53fd7d1f3c0cf1cf55dc0476' and '0000000000000000000000000000000000000000'
redis-master | 1:M 06 Jun 2020 00:44:10.606 * Starting BGSAVE for SYNC with target: disk
redis-master | 1:M 06 Jun 2020 00:44:10.607 * Background saving started by pid 19
redis-master | 1:M 06 Jun 2020 00:44:10.612 * Replica 192.168.240.1:6381 asks for synchronization
redis-master | 1:M 06 Jun 2020 00:44:10.612 * Full resync requested by replica 192.168.240.1:6381
redis-master | 1:M 06 Jun 2020 00:44:10.612 * Waiting for end of BGSAVE for SYNC
redis-master | 19:C 06 Jun 2020 00:44:10.615 * DB saved on disk
redis-master | 19:C 06 Jun 2020 00:44:10.615 * RDB: 0 MB of memory used by copy-on-write
redis-master | 1:M 06 Jun 2020 00:44:10.679 * Background saving terminated with success
redis-master | 1:M 06 Jun 2020 00:44:10.686 * Synchronization with replica 192.168.240.1:6380 succeeded
redis-master | 1:M 06 Jun 2020 00:44:10.688 * Synchronization with replica 192.168.240.1:6381 succeeded
liumiaocn:redis liumiao$
Slave日志
以Slave 1为例,从如下日志中可以看到其与Master节点连接和数据同步的过程。
liumiaocn:redis liumiao$ docker-compose logs slave1
Attaching to redis-slave-1
redis-slave-1 | 1:C 06 Jun 2020 00:44:10.588 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-slave-1 | 1:C 06 Jun 2020 00:44:10.588 # Redis version=6.0.4, bits=64, commit=00000000, modified=0, pid=1, just started
redis-slave-1 | 1:C 06 Jun 2020 00:44:10.588 # Configuration loaded
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.591 * Running mode=standalone, port=6380.
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.591 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.591 # Server initialized
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.591 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.595 * Ready to accept connections
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.595 * Connecting to MASTER 192.168.31.242:6379
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.595 * MASTER <-> REPLICA sync started
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.596 * Non blocking connect for SYNC fired the event.
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.599 * Master replied to PING, replication can continue...
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.604 * Partial resynchronization not possible (no cached master)
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.610 * Full resync from master: 543ebff4abe27ada53fd7d1f3c0cf1cf55dc0476:0
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.684 * MASTER <-> REPLICA sync: receiving 175 bytes from master to disk
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.902 * MASTER <-> REPLICA sync: Flushing old data
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.902 * MASTER <-> REPLICA sync: Loading DB in memory
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.907 * Loading RDB produced by version 6.0.4
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.908 * RDB age 0 seconds
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.908 * RDB memory usage when created 1.88 Mb
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.908 * MASTER <-> REPLICA sync: Finished with success
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.910 * Background append only file rewriting started by pid 19
redis-slave-1 | 1:S 06 Jun 2020 00:44:10.941 * AOF rewrite child asks to stop sending diffs.
redis-slave-1 | 19:C 06 Jun 2020 00:44:10.941 * Parent agreed to stop sending diffs. Finalizing AOF...
redis-slave-1 | 19:C 06 Jun 2020 00:44:10.941 * Concatenating 0.00 MB of AOF diff received from parent.
redis-slave-1 | 19:C 06 Jun 2020 00:44:10.944 * SYNC append only file rewrite performed
redis-slave-1 | 19:C 06 Jun 2020 00:44:10.944 * AOF rewrite: 0 MB of memory used by copy-on-write
redis-slave-1 | 1:S 06 Jun 2020 00:44:11.001 * Background AOF rewrite terminated with success
redis-slave-1 | 1:S 06 Jun 2020 00:44:11.003 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
redis-slave-1 | 1:S 06 Jun 2020 00:44:11.004 * Background AOF rewrite finished successfully
redis-slave-1 | 1:S 06 Jun 2020 00:52:18.352 # Connection with master lost.
redis-slave-1 | 1:S 06 Jun 2020 00:52:18.352 * Caching the disconnected master state.
redis-slave-1 | 1:S 06 Jun 2020 00:58:53.300 * Connecting to MASTER 192.168.31.242:6379
redis-slave-1 | 1:S 06 Jun 2020 00:58:53.300 * MASTER <-> REPLICA sync started
redis-slave-1 | 1:S 06 Jun 2020 00:58:53.300 * Non blocking connect for SYNC fired the event.
redis-slave-1 | 1:S 06 Jun 2020 00:58:53.302 * Master replied to PING, replication can continue...
redis-slave-1 | 1:S 06 Jun 2020 00:58:53.306 * Trying a partial resynchronization (request 543ebff4abe27ada53fd7d1f3c0cf1cf55dc0476:659).
redis-slave-1 | 1:S 06 Jun 2020 00:58:53.308 * Successful partial resynchronization with master.
redis-slave-1 | 1:S 06 Jun 2020 00:58:53.308 * MASTER <-> REPLICA sync: Master accepted a Partial Resynchronization.
liumiaocn:redis liumiao$
验证主从
连接确认
因为上述启动的环境中,Master在6379提供服务,使用Redis客户端连接Master
liumiaocn:redis liumiao$ redis-cli -p 6379
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379>
提示需要进行认证,使用auth命令结合设定的密码进行认证,并确认当前的keys的状态
127.0.0.1:6379> auth aaa@qq.com
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>
确认一下slave 1,发现此时信息也为空
liumiaocn:redis liumiao$ redis-cli -p 6380
127.0.0.1:6380> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6380> auth aaa@qq.com
OK
127.0.0.1:6380> keys *
(empty list or set)
127.0.0.1:6380>
设定key
在Master中设定名为greeting的key,内容设定为“hello liumiao"
127.0.0.1:6379> set greeting "hello liumiao"
OK
127.0.0.1:6379> keys *
1) "greeting"
127.0.0.1:6379>
确认同步
在Slave节点中确认此key的内容
127.0.0.1:6380> keys *
1) "greeting"
127.0.0.1:6380> get greeting
"hello liumiao"
127.0.0.1:6380>
上一篇: 简历这样写,HR一眼看中你
下一篇: PHP开发者该知道的多进程消费队列
推荐阅读