文章目录
CentOS7部署Redis7集群
一、前言
- Linux 发行版:CentOS-7-x86_64-DVD-1804.iso
- Redis 版本:7.0.12
Redis Download:https://redis.io/download/
Redis Tag:https://github.com/redis/redis/tags
Redis cluster specification:https://redis.io/docs/reference/cluster-spec/
High availability with Redis Sentinel:https://redis.io/docs/management/sentinel/
CentOS7安装部署Redis7:https://blog.csdn.net/u011424614/article/details/132418619
Redis入门和使用实践v2018:https://blog.csdn.net/u011424614/article/details/100170313
[Windows] Redis使用记录:https://blog.csdn.net/u011424614/article/details/101531772
CentOS基础操作命令:https://blog.csdn.net/u011424614/article/details/94555916
二、正文
1.集群说明
1)Redis Cluster
Redis Cluster 是 Redis 的一个分布式部署模式,旨在提供高可用性、横向扩展性和分布式数据存储。Redis Cluster 的主要特点包括:
- 分布式数据存储: Redis Cluster 将数据分割成多个槽(slots)并将这些槽分布在多个节点上。这允许数据在多个节点之间分布存储,从而实现数据的分布式存储和负载均衡
- 高可用性: Redis Cluster 支持多个主节点和从节点,并自动处理节点故障。如果一个主节点发生故障,Redis Cluster 将自动选择一个从节点升级为新的主节点,以确保数据的可用性
- 自动故障转移: 当主节点发生故障时,Redis Cluster 可以自动执行故障转移操作,将一个从节点升级为新的主节点。这使得系统能够自动从故障中恢复
- 自动分片: Redis Cluster 提供自动分片功能,客户端可以连接到任何集群节点,并通过集群的路由功能自动将请求路由到正确的节点
- Redis Cluster 不支持多个数据库,即只有 1 个数据库 ;在 Redis Cluster 中,所有数据都分布在不同的槽(slots)上,并且每个节点只负责存储和管理自己负责的槽上的数据
2)Redis Sentinel
Redis Sentinel(主从复制模式) 是用于监控和管理 Redis 服务器高可用性的工具。它可以确保在 Redis 主节点发生故障时执行自动故障转移操作,将一个从节点升级为新的主节点,以维持系统的可用性。以下是 Redis Sentinel 的主要特点和功能:
- 监控主从复制: Sentinel 负责监控 Redis 主节点和从节点的状态。它会定期检查节点是否可用,包括主节点是否宕机或无法访问
- 自动故障检测: 如果 Sentinel 检测到主节点不可用,它会触发自动故障检测并选择一个从节点升级为新的主节点。这确保了系统在主节点故障时能够自动恢复
- 故障转移: Sentinel 负责协调故障转移操作,它会通知其他 Sentinel 进程和 Redis 客户端有关主节点故障和新主节点的信息。这确保了数据的连续性和可用性
- 多节点支持: 您可以在一个 Redis 集群中运行多个 Sentinel 进程,以提高监控的可靠性。如果一个 Sentinel 失败,其他 Sentinel 进程仍然可以继续监控和执行故障转移
- Redis Sentinel(主从复制模式) 支持多个数据库
2.硬件配置
机器名 | IP | 端口 |
---|---|---|
sys-cluster-01 | 192.168.249.131 | 3306、33061 |
sys-cluster-02 | 192.168.249.132 | 3306、33061 |
sys-cluster-03 | 192.168.249.133 | 3306、33061 |
3.部署Redis
在 3 台服务器上安装Redis
-
安装参考:《CentOS7安装部署Redis7 》
-
安装目录:
/opt/redis/redis-7.0.12/src
-
修改 3 台服务器的 hosts 文件
cat > /etc/hosts <<EOF
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.249.131 sys-cluster-01
192.168.249.132 sys-cluster-02
192.168.249.133 sys-cluster-03
EOF
- 创建日志目录和数据目录
mkdir -p /opt/redis/redis-7.0.12/{data,logs}
4.部署Cluster
1)集群配置文件
在 3 台服务器上使用相同的配置文件
- redis-cluster-6379.conf
cat > /opt/redis/redis-7.0.12/redis-cluster-6379.conf <<EOF
# 指定 Redis 服务器绑定的网络接口地址
bind 0.0.0.0
# Redis 服务器监听的端口号
port 6379
# 关闭保护模式
protected-mode no
# Redis 服务器以守护进程的方式运行
# daemonize no
# Redis 服务器的密码
requirepass "redis123456"
# Redis 日志文件的路径和文件名
logfile "/opt/redis/redis-7.0.12/logs/cluster-6379.log"
# 启用Redis的持久化日志
appendonly yes
# 持久化数据文件(快照文件)的文件名
dbfilename "cluster-6379.rdb"
# 持久化数据文件的存储目录
dir "/opt/redis/redis-7.0.12/data/"
# 从节点连接到主节点所需的密码
masterauth "redis123456"
# 是否开启集群
cluster-enabled yes
# 集群端口
cluster-port 16379
# 生成的node文件,记录集群节点信息,默认为 nodes.conf
cluster-config-file nodes-6379.conf
#节点连接超时时间
cluster-node-timeout 20000
EOF
2)配置自启动
在 3 台服务器上使用相同的配置文件
- redis-server.service
cat > /etc/systemd/system/redis-server.service <<EOF
[Unit]
Description=The redis-server Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/var/run/redis_6379.pid
ExecStart=/opt/redis/redis-7.0.12/src/redis-server /opt/redis/redis-7.0.12/redis-cluster-6379.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target
EOF
- 启动 redis 服务
# 重新加载服务配置文件
systemctl daemon-reload
# 启动服务
systemctl start redis-server.service
# 重启服务
systemctl restart redis-server.service
# 服务自启动
systemctl enable redis-server.service
# 停止服务
systemctl stop redis-server.service
# 服务状态
systemctl status redis-server.service
3)客户端连接
其中一个服务器执行
cd /opt/redis/redis-7.0.12
# 创建集群
# (备用) ./src/redis-cli --cluster create 192.168.249.131:6379 192.168.249.132:6379 192.168.249.133:6379 -a redis123456
./src/redis-cli --cluster create sys-cluster-01:6379 sys-cluster-02:6379 sys-cluster-03:6379 -a redis123456
# 集群访问
#(备用)./src/redis-cli -c -h 192.168.249.131 -p 6379 -a redis123456
./src/redis-cli -c -h sys-cluster-01 -p 6379 -a redis123456
# 查询集群状态
cluster info
# 测试
set name abc
get name
# 停止 redis 服务端
redis-cli shutdown
5.部署Sentinel
1)主从复制模式
(1)主节点配置
131 主服务器上修改配置文件
cat > /opt/redis/redis-7.0.12/redis-sentinel-6379.conf <<EOF
# 指定 Redis 服务器绑定的网络接口地址
bind 0.0.0.0
# Redis 服务器监听的端口号
port 6379
# 关闭保护模式
protected-mode no
# Redis 服务器以守护进程的方式运行
# daemonize no
# Redis 服务器的密码
requirepass "redis123456"
# Redis 日志文件的路径和文件名
logfile "/opt/redis/redis-7.0.12/logs/sentinel-6379.log"
# 启用Redis的持久化日志
appendonly yes
# 持久化数据文件(快照文件)的文件名
dbfilename "sentinel-6379.rdb"
# 持久化数据文件的存储目录
dir "/opt/redis/redis-7.0.12/data/"
EOF
(2)从节点配置
132 和133 从服务器上修改配置文件
- 根据实际情况,修改 replicaof 指向主节点
cat > /opt/redis/redis-7.0.12/redis-sentinel-6379.conf <<EOF
# 指定 Redis 服务器绑定的网络接口地址
bind 0.0.0.0
# Redis 服务器监听的端口号
port 6379
# 关闭保护模式
protected-mode no
# Redis 服务器以守护进程的方式运行
# daemonize no
# Redis 服务器的密码
requirepass "redis123456"
# 从节点连接到主节点所需的密码
masterauth "redis123456"
# Redis 日志文件的路径和文件名
logfile "/opt/redis/redis-7.0.12/logs/sentinel-6379.log"
# 启用Redis的持久化日志
appendonly yes
# 持久化数据文件(快照文件)的文件名
dbfilename "sentinel-6379.rdb"
# 持久化数据文件的存储目录
dir "/opt/redis/redis-7.0.12/data/"
# 指向主节点
replicaof 192.168.249.131 6379
EOF
(3)配置自启动
在 3 台服务器上使用相同的配置文件
- redis-server.service
cat > /etc/systemd/system/redis-server.service <<EOF
[Unit]
Description=The redis-server Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/var/run/redis_6379.pid
ExecStart=/opt/redis/redis-7.0.12/src/redis-server /opt/redis/redis-7.0.12/redis-sentinel-6379.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target
EOF
- 启动 redis 服务
# 重新加载服务配置文件
systemctl daemon-reload
# 启动服务
systemctl start redis-server.service
# 重启服务
systemctl restart redis-server.service
# 服务自启动
systemctl enable redis-server.service
# 停止服务
systemctl stop redis-server.service
# 服务状态
systemctl status redis-server.service
2)Sentinel节点
(1)Sentinel配置文件
在 3 台服务器上使用相同的配置文件
- 根据实际情况,修改 sentinel auth-pass mymaster 指向主节点
cat > /opt/redis/redis-7.0.12/redis-sentinel-26379.conf <<EOF
# 指定 Redis 服务器绑定的网络接口地址
bind 0.0.0.0
# Redis 服务器监听的端口号
port 26379
# 关闭保护模式
protected-mode no
# Redis 服务器的密码
requirepass "redis123456"
# 从节点连接到主节点所需的密码
masterauth "redis123456"
# Redis 日志文件的路径和文件名
logfile "/opt/redis/redis-7.0.12/logs/sentinel-26379.log"
# 持久化数据文件的存储目录
dir "/opt/redis/redis-7.0.12/data/"
# 设置 Sentinel 监视的 Redis 主节点 mymaster 的认证密码
sentinel auth-pass mymaster redis123456
# Sentinel 监视名为 mymaster 的 Redis 主节点
sentinel monitor mymaster 192.168.249.131 6379 2
# 配置指定了 Sentinel 在认为主节点不可用之前等待的毫秒数
sentinel down-after-milliseconds mymaster 10000
# 定了在进行故障转移时,同时同步的从节点数量
sentinel parallel-syncs mymaster 1
# 配置定义了执行故障转移的超时时间
sentinel failover-timeout mymaster 60000
EOF
(2)Sentinel节点自启动
在 3 台服务器上使用相同的配置文件
cat > /etc/systemd/system/redis-sentinel.service <<EOF
[Unit]
Description=The redis-sentinel Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/var/run/redis_sentinel_6379.pid
ExecStart=/opt/redis/redis-7.0.12/src/redis-sentinel /opt/redis/redis-7.0.12/redis-sentinel-26379.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target
EOF
- 启动 redis 服务
# 重新加载服务配置文件
systemctl daemon-reload
# 启动服务
systemctl start redis-sentinel.service
# 重启服务
systemctl restart redis-sentinel.service
# 服务自启动
systemctl enable redis-sentinel.service
# 停止服务
systemctl stop redis-sentinel.service
# 服务状态
systemctl status redis-sentinel.service
3)客户端连接
其中一个服务器执行
cd /opt/redis/redis-7.0.12
# 客户端连接 redis 服务
./src/redis-cli -h 192.168.249.131 -p 6379 -a redis123456
# 查询主从模式状态
info replication
# 测试
set name abc
get name
# 客户端连接 redis sentinel 服务
./src/redis-cli -h 192.168.249.131 -p 26379 -a redis123456
# 查询 sentinel 状态
info sentinel