# 018. 在项目中部署 redis 的读写分离架构(包含节点间认证口令)

之前几讲都是在铺垫各种 redis replication 的原理和知识,那么关键是怎么搭建呢?

本章讲解 一主一从,往主节点去写,在从节点去读,可以读到,主从架构就搭建成功了

# 启用复制,部署 slave node

在 eshop-cache02 上安装 redis,可参考之前的章节 中的「安装单机版 redis」与 「redis 的生产环境启动方案」

安装好之后,开启 slaveof <masterip> <masterport> 属性,把该机器变成 slave node

/etc/redis/6379.conf

slaveof eshop-cache01 6379
1

# 强制读写分离

基于主从复制架构,实现读写分离

/etc/redis/6379.conf

# 该属性已经默认开启,
slave-read-only yes
1
2

开启了只读的 redis slave node,会拒绝所有的写操作,这样可以强制搭建成读写分离的架构

# 集群安全认证

master 上启用安全认证:requirepass

eshop-cache01/etc/redis/6379.conf

requirepass redis-pass
1

slave 上使用连接口令:masterauth

eshop-cache02/etc/redis/6379.conf

masterauth redis-pass
1

也就是 master 启用密码,slave 要持有相同的密码才能连接上

配置完成后,记得重启

cd /etc/init.d/
redis-cli shutdown
./redis_6379 start
1
2
3

WARNING

由于配置名称都一样,不要上传错了。

# 读写分离架构的测试

  • 先启动主节点,eshop-cache01 上的 redis实例
  • 再启动从节点,eshop-cache02 上的 redis实例

在 eshop-cache01 上尝试获取数据

[root@eshop-cache01 init.d]# redis-cli
127.0.0.1:6379> get k1
(error) NOAUTH Authentication required.
1
2
3

会发现报错了,原因是之前我们开启了密码,这个时候要怎么连接 redis-cli 呢?

[root@eshop-cache01 init.d]# redis-cli -h
redis-cli 3.2.8

Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
  -h <hostname>      Server hostname (default: 127.0.0.1).
  -p <port>          Server port (default: 6379).
  -s <socket>        Server socket (overrides hostname and port).
  -a <password>      Password to use when connecting to the server.
1
2
3
4
5
6
7
8

可以看到帮助命令需要使用 -a 来指定密码

[root@eshop-cache01 init.d]# redis-cli -a redis-pass
127.0.0.1:6379> get k1
(nil)

# 注意,在关闭 redis 的时候同样也需要使用密码

redis-cli -a redis-pass shutdown
1
2
3
4
5
6
7

# 在 master 上写数据

[root@eshop-cache01 init.d]# redis-cli -a redis-pass
127.0.0.1:6379> set k1 123456
OK
1
2
3

# 在 slave 上获取数据

[root@eshop-cache02 init.d]# redis-cli
127.0.0.1:6379> get k1
(nil)
1
2
3

发现没有获取到数据,这是怎么回事呢?那么一般说明我们的 slave 可能配置有问题。

这个时候要是能看到日志就好了,在 eshop-cache02/etc/redis/6379.conf中, 配置上 logfile /etc/redis/log.log 重启后可以看到日志中出现不能连接到 master

[root@eshop-cache02 redis]# ll
total 52
-rw-r--r-- 1 root root 46774 Mar 23  2019 6379.conf
-rw-r--r-- 1 root root  2719 Mar 19 05:14 log.log
[root@eshop-cache02 redis]# tail -f log.log
24489:S 19 Mar 05:14:28.768 # Error condition on socket for SYNC: Connection refused
24489:S 19 Mar 05:14:29.789 * Connecting to MASTER eshop-cache01:6379
1
2
3
4
5
6
7

原因是:/etc/redis/6379.conf 中的 bind 属性配置没有放开

默认是绑定的 127.0.0.1,只能本机访问 redis。改成本机的内外 ip 地址就可以对外提供服务了,这里由于之前配置了 hosts 映射,使用 hostname

# bind 127.0.0.1   
bind 127.0.0.1 eshop-cache01
1
2

TIP

bind 可以写多条,如果没有 127 的ip,使用 redis-cli 会默认连接 127 的 ip,这样你自己也不能使用这个本机简便的登录方式了

如果已经使用 redis-cli 连接不上怎么办?需要自己带上 ip 地址访问,如下: redis-cli -h eshop-cache02 shutdown

记得是每个 redis 节点上都要修改成绑定自己机器的 hontname

对外开放访问后(bind 127.0.0.1 eshop-cache01) 终于连接上了

[root@eshop-cache02 init.d]# ./redis_6379 start
Starting Redis server...
[root@eshop-cache02 init.d]# redis-cli
127.0.0.1:6379> get k1   # 能获取到 master 上的数据
"123456"
127.0.0.1:6379> info replication   # 查看信息
# Replication
role:slave
master_host:eshop-cache01     # 可以看到 master 的信息
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:253
slave_priority:100
slave_read_only:1
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22