redis-搭一个主从redis集群

Redis安装和基础见:菜鸟教程-redis

Docker相关见:docker学习[1]:什么是docker?docker学习[2]:从0开始创建一个容器

环境介绍:

[root@localhost ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
9ba8230c24a8        redis               "/bin/bash"         12 days ago         Up 12 days          0.0.0.0:6382->6379/tcp   redis-slave2
7632e8ffadfd        redis               "/bin/bash"         12 days ago         Up 12 days          0.0.0.0:6381->6379/tcp   redis-slave1
216c5903656e        redis               "/bin/bash"         12 days ago         Up 12 days          0.0.0.0:6380->6379/tcp   redis-master

虚拟机内安装了3个redis的docker容器

[root@localhost ~]# docker network inspect redisNet
[
    {
        "Name": "redisNet",
        "Id": "791dd02e8b506e2ad39d7a13652d246bb3c3abfaf786e1fdb20aecc302224477",
        "Created": "2019-01-03T17:39:45.429474363+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.29.0.0/16"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "216c5903656e4ea27b9c0849df375be7afd0352dfe410af2d3f2b97b4a158869": {
                "Name": "redis-master",
                "EndpointID": "1c890aa1a1f7379b4b3478eb8134bf664373ba8a2d2a2b48569f46e6cccd2617",
                "MacAddress": "02:42:ac:1d:00:0a",
                "IPv4Address": "172.29.0.10/16",
                "IPv6Address": ""
            },
            "7632e8ffadfd7af87c6d7e8f56342544ca0b00f96864288c6fbb38d721ba34ff": {
                "Name": "redis-slave1",
                "EndpointID": "2fb5540606e7fd3fb269c93837d7a57502566c566f0d14accc4c485badde0c9c",
                "MacAddress": "02:42:ac:1d:00:0b",
                "IPv4Address": "172.29.0.11/16",
                "IPv6Address": ""
            },
            "9ba8230c24a807f29820a107574b46159fd0be69e00fa58165146fbd8f22d503": {
                "Name": "redis-slave2",
                "EndpointID": "0e5706da99c53b32518cd4d5ed1a059074fe01f928115a9603698673412382ab",
                "MacAddress": "02:42:ac:1d:00:0c",
                "IPv4Address": "172.29.0.12/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

ip见上

进入redis-master容器,开启redis

[root@216c5903656e /]# redis-server /etc/redis.conf &
[1] 211
[root@216c5903656e /]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_repl_offset:141
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:140

默认情况下,每台Redis服务器都是主节点;且一个主节点可以有多个从节点(或没有从节点),但一个从节点只能有一个主节点。

相同方法打开另一个redis

[root@7632e8ffadfd /]# redis-server /etc/redis.conf &
[1] 245
[root@7632e8ffadfd /]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

查看日志:/var/log/redis/redis.log

245:M 16 Jan 06:25:55.610 * DB loaded from disk: 0.000 seconds
245:M 16 Jan 06:25:55.610 * The server is now ready to accept connections on port 6379

从磁盘内读取了快照,服务器现在可以接受6379端口的连接了。但是默认情况下redis都是以主服务启动的,如果需要变成子节点,有以下三种方法:

  1. 通过配置文件配置: slaveof 《masterip》《masterport》

  2. redis-server --slaveof 《masterip》 《masterport》

  3. 命令行内使用:slaveof 《masterip》 《masterport》

既然服务已经起了,先试试命令行集群

127.0.0.1:6379> slaveof 172.29.0.10 6379
OK
 
## 日志
 
245:S 16 Jan 06:36:41.245 * MASTER <-> SLAVE sync started
245:S 16 Jan 06:36:41.245 * Non blocking connect for SYNC fired the event.
245:S 16 Jan 06:36:41.246 * Master replied to PING, replication can continue...
245:S 16 Jan 06:36:41.248 # Unable to AUTH to MASTER: -ERR Client sent AUTH, but no password is set

日志解读:主从服务开始,从服务器发送ping命令到主服务器并有返回,开始复制。无法发送AUTH命令到主服务器,客户端发送了AUTH命令,但是主服务器没有设置密码。查看一下redis配置

################################## SECURITY ###################################
 
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass 123456
 
# 大概就是,配置了密码后其他地方在操作这台redis的时候需要经过auth验证。
# 这样就可以拦截其他的连接只让知道密码的服务连接这台redis了
 
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth 123456
 
# 如果主节点被密码保护,配置这个就可以发送AUTH验证

找到了上面两个配置,我们先看看在以下几种情况会出现什么事情。

1、主服务器没配置requirepass,但是从服务器配置了masterauth

此时会出现上面的Unable to AUTH to MASTER: -ERR Client sent AUTH, but no password is set

大概意思就是,因为配置了masterauth,从节点会发送AUTH命令到主节点验证密码。但是主节点一脸懵逼,我并没有配置密码啊,你这个操作我不懂啊。从节点收到这个消息也一脸懵逼,大兄弟你这个不按套路出牌啊,帮我验证一下呗。然后两个人二脸懵逼对这个密码验证来验证去疯狂等待。

2、主服务器配置了requirepass,但是从服务器没有配置masterauth

306:S 16 Jan 07:15:45.636 * Connecting to MASTER 172.29.0.10:6379
306:S 16 Jan 07:15:45.637 * MASTER <-> SLAVE sync started
306:S 16 Jan 07:15:45.637 * Non blocking connect for SYNC fired the event.
306:S 16 Jan 07:15:45.637 * Master replied to PING, replication can continue...
306:S 16 Jan 07:15:45.638 * (Non critical) Master does not understand REPLCONF listening-port: -NOAUTH Authentication required.
306:S 16 Jan 07:15:45.639 * (Non critical) Master does not understand REPLCONF capa: -NOAUTH Authentication required.
306:S 16 Jan 07:15:45.639 * Partial resynchronization not possible (no cached master)
306:S 16 Jan 07:15:45.640 # Unexpected reply to PSYNC from master: -NOAUTH Authentication required.
306:S 16 Jan 07:15:45.640 * Retrying with SYNC...
306:S 16 Jan 07:15:45.641 # MASTER aborted replication with an error: NOAUTH Authentication required.

日志如上。连接主服务器,主从复制开始。从服务器ping主服务器并且得到了回复,主从复制继续。master节点要求验证。无法使用PSYN连接主节点,没有密码。尝试重新主从复制,master取消了复制:没有发送AUTH命令。

3、主服务器配置了requirepass,从服务器配置了masterauth,但是masterauth配置的不对

312:S 16 Jan 07:21:12.402 * Connecting to MASTER 172.29.0.10:6379
312:S 16 Jan 07:21:12.403 * MASTER <-> SLAVE sync started
312:S 16 Jan 07:21:12.403 * Non blocking connect for SYNC fired the event.
312:S 16 Jan 07:21:12.403 * Master replied to PING, replication can continue...
312:S 16 Jan 07:21:12.403 # Unable to AUTH to MASTER: -ERR invalid password

这种情况就比较简单了,从节点发送了AUTH命令去主节点,报错:无效的密码。

4、正常情况

317:S 16 Jan 07:22:50.425 * Connecting to MASTER 172.29.0.10:6379
317:S 16 Jan 07:22:50.427 * MASTER <-> SLAVE sync started
317:S 16 Jan 07:22:50.427 * Non blocking connect for SYNC fired the event.
317:S 16 Jan 07:22:50.428 * Master replied to PING, replication can continue...
317:S 16 Jan 07:22:50.431 * Partial resynchronization not possible (no cached master)
317:S 16 Jan 07:22:50.436 * Full resync from master: e82fb9b5a0bc13eb23fc1151bf4ccb8430fe19c0:1639
317:S 16 Jan 07:22:50.489 * MASTER <-> SLAVE sync: receiving 77 bytes from master
317:S 16 Jan 07:22:50.489 * MASTER <-> SLAVE sync: Flushing old data
317:S 16 Jan 07:22:50.489 * MASTER <-> SLAVE sync: Loading DB in memory
317:S 16 Jan 07:22:50.490 * MASTER <-> SLAVE sync: Finished with success

连接主节点,主从复制开始。从服务器发送ping命令到主服务器并且主服务器有回应,主从复制继续。无法进行部分同步,因为该没有缓存过该主节点。从主节点进行全量复制。从主节点接受了77字节的数组,清空旧的数据,读取快照到内存,成功结束。

主从配置完成了,尝试一下看看在主节点set一个key是否能成功同步到子节点

主节点

127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set test a
OK
127.0.0.1:6379> 

从节点

127.0.0.1:6379> keys *
1) "test"
127.0.0.1:6379> get test
"a"
127.0.0.1:6379>

日志

317:S 16 Jan 07:37:28.007 * 1 changes in 900 seconds. Saving...   # 过去90秒有一个改变,开始保存快照
317:S 16 Jan 07:37:28.010 * Background saving started by pid 321  # 执行BGSAVE
321:C 16 Jan 07:37:28.014 * DB saved on disk                      # 快照保存完毕
321:C 16 Jan 07:37:28.015 * RDB: 6 MB of memory used by copy-on-write
317:S 16 Jan 07:37:28.115 * Background saving terminated with success

其他两种配置如法炮制。

程序幼儿员-龚学鹏
请先登录后发表评论
  • latest comments
  • 总共0条评论