CentOS7部署Redis7集群

13 篇文章 0 订阅
12 篇文章 0 订阅

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 specification | Redis

Redis Cluster 是 Redis 的一个分布式部署模式,旨在提供高可用性、横向扩展性和分布式数据存储。Redis Cluster 的主要特点包括:

  1. 分布式数据存储: Redis Cluster 将数据分割成多个槽(slots)并将这些槽分布在多个节点上。这允许数据在多个节点之间分布存储,从而实现数据的分布式存储和负载均衡
  2. 高可用性: Redis Cluster 支持多个主节点和从节点,并自动处理节点故障。如果一个主节点发生故障,Redis Cluster 将自动选择一个从节点升级为新的主节点,以确保数据的可用性
  3. 自动故障转移: 当主节点发生故障时,Redis Cluster 可以自动执行故障转移操作,将一个从节点升级为新的主节点。这使得系统能够自动从故障中恢复
  4. 自动分片: Redis Cluster 提供自动分片功能,客户端可以连接到任何集群节点,并通过集群的路由功能自动将请求路由到正确的节点
  5. Redis Cluster 不支持多个数据库,即只有 1 个数据库 ;在 Redis Cluster 中,所有数据都分布在不同的槽(slots)上,并且每个节点只负责存储和管理自己负责的槽上的数据

2)Redis Sentinel

High availability with Redis Sentinel | Redis

Redis Sentinel(主从复制模式) 是用于监控和管理 Redis 服务器高可用性的工具。它可以确保在 Redis 主节点发生故障时执行自动故障转移操作,将一个从节点升级为新的主节点,以维持系统的可用性。以下是 Redis Sentinel 的主要特点和功能:

  1. 监控主从复制: Sentinel 负责监控 Redis 主节点和从节点的状态。它会定期检查节点是否可用,包括主节点是否宕机或无法访问
  2. 自动故障检测: 如果 Sentinel 检测到主节点不可用,它会触发自动故障检测并选择一个从节点升级为新的主节点。这确保了系统在主节点故障时能够自动恢复
  3. 故障转移: Sentinel 负责协调故障转移操作,它会通知其他 Sentinel 进程和 Redis 客户端有关主节点故障和新主节点的信息。这确保了数据的连续性和可用性
  4. 多节点支持: 您可以在一个 Redis 集群中运行多个 Sentinel 进程,以提高监控的可靠性。如果一个 Sentinel 失败,其他 Sentinel 进程仍然可以继续监控和执行故障转移
  5. Redis Sentinel(主从复制模式) 支持多个数据库

2.硬件配置

机器名IP端口
sys-cluster-01192.168.249.1313306、33061
sys-cluster-02192.168.249.1323306、33061
sys-cluster-03192.168.249.1333306、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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在CentOS 7上部署Redis集群,可以按照以下步骤进行操作: 1. 在所有节点上安装Redis:使用以下命令在每个节点上安装Redis: ``` sudo yum install epel-release sudo yum install redis ``` 2. 配置主节点:选择一个节点作为主节点,并在其配置文件中进行以下更改: ``` sudo vi /etc/redis.conf ``` 找到并修改以下行: ``` bind 0.0.0.0 protected-mode no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes ``` 保存并关闭文件。 3. 启动主节点:使用以下命令启动Redis服务器: ``` sudo systemctl start redis ``` 4. 添加从节点:选择其他节点作为从节点,并在其配置文件中进行以下更改: ``` sudo vi /etc/redis.conf ``` 找到并修改以下行(确保bind行与主节点相同): ``` bind 0.0.0.0 protected-mode no slaveof <主节点IP> <主节点端口> ``` 保存并关闭文件。 5. 启动从节点:使用以下命令启动Redis服务器: ``` sudo systemctl start redis ``` 6. 创建Redis集群:在任一节点上执行以下命令来创建Redis集群: ``` redis-cli --cluster create <主节点IP>:<主节点端口> <从节点IP>:<从节点端口> --cluster-replicas 1 ``` 如果有更多的从节点,可以继续添加它们的IP和端口。 7. 验证Redis集群:使用以下命令验证Redis集群是否成功创建: ``` redis-cli -c -p <主节点端口> ``` 进入Redis命令行之后,尝试执行一些Redis命令(例如:`SET key value`,`GET key`)来确保集群正常工作。 这样就完成了在CentOS 7上部署Redis集群的过程。请注意,这只是基本的集群配置,你可能需要根据你的需求进行其他配置和安全性增强。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

趴着喝可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值