面试题答案
一键面试Redis 配置文件安全防护
- 认证配置:
- 在 Redis 主从服务器的
redis.conf
文件中,通过设置requirepass
参数来设置密码。例如:
requirepass your_strong_password
- 这样,客户端连接 Redis 时就需要提供正确的密码进行认证,防止未经授权的访问。
- 在 Redis 主从服务器的
- 绑定地址限制:
- 使用
bind
参数指定 Redis 服务器监听的 IP 地址。如果只想允许本地访问,可以设置为:
bind 127.0.0.1
- 如果需要允许其他特定网络访问,可以绑定对应的 IP 地址,如
bind 192.168.1.100
,这样可以限制只有指定 IP 范围内的客户端能连接。
- 使用
- 保护模式:
- 将
protected - mode
设置为yes
(默认值)。在这种模式下,如果没有设置bind
地址且没有设置requirepass
密码,Redis 只会监听本地回环地址(127.0.0.1),增加安全性。 - 如果设置了
requirepass
密码,即使没有明确设置bind
地址,外部连接也需要密码才能访问。
- 将
Sentinel 特有配置安全防护
- Sentinel 认证配置:
- 在 Sentinel 的配置文件
sentinel.conf
中,通过sentinel auth - pass <master - name> <password>
来设置 Sentinel 连接主从服务器的密码。其中<master - name>
是 Sentinel 监控的主服务器名称,<password>
是在 Redis 主从服务器redis.conf
中设置的requirepass
的密码。例如:
sentinel auth - pass mymaster your_strong_password
- 这样 Sentinel 在与主从服务器交互时,会使用密码进行认证,确保通信安全。
- 在 Sentinel 的配置文件
- Sentinel 监听地址限制:
- 和 Redis 服务器类似,可以在
sentinel.conf
中使用bind
参数指定 Sentinel 监听的 IP 地址。例如:
bind 192.168.1.100
- 限制只有指定 IP 范围内的客户端或其他 Sentinel 节点能连接到该 Sentinel 实例。
- 和 Redis 服务器类似,可以在
- 加密通信:
- Redis Sentinel 原生不支持内置加密通信,但可以通过在服务器之间使用 SSL/TLS 隧道(如 stunnel 或 OpenSSL)来加密 Redis Sentinel 与主从服务器之间的通信。
- 以 stunnel 为例,需要配置 stunnel 服务端和客户端,在服务端配置文件(如
/etc/stunnel/stunnel.conf
)中,指定监听端口并连接到 Redis 或 Sentinel 服务端口,同时配置 SSL 证书和密钥:
[redis] accept = 6380 connect = 127.0.0.1:6379 cert = /etc/stunnel/redis.pem
- 客户端同样配置 stunnel 连接到服务端的加密端口,这样就可以在传输层实现通信加密,保障数据在网络传输过程中的安全性。