跳至主要內容

Redis主从/哨兵/集群架构搭建

Yaien Blog原创大约 5 分钟DBRedis

Redis主从/哨兵/集群架构搭建

记录Redis主从复制架构、哨兵架构以及集群(cluster)架构的搭建。本次搭建使用不同云服务器厂商公网ip搭建。

环境

服务器环境

服务器IP版本节点
腾讯云159.159.159.159CentOS 7.9master
腾讯云139.139.139.139CentOS 7.9slave
华为云101.101.101.101CentOS 7.9slave

开放防火墙端口

  1. 关闭服务器防火墙
  2. 开放云服务提供商服务器实例防火墙端口:6379、6380、6381、16379、16380、16381、26379、26380、26381

Redis版本

redis-7.2.0

# 安装路径
 cd /usr/local/src/redis-7.2.0/

主从复制搭建

  1. 进入安装目录

    # 安装路径
    cd /usr/local/src/redis-7.2.0/
    
  2. 复制redis.conf配置文件

    # 复制redis.conf -> redis.conf.bak
    cp redis.conf redis.conf.bak
    
  3. 修改redis.conf配置文件

    #./redis.conf
    
    # 指定日志文件名称
    logfile "6379.log"
    
    #指定数据存放目录
    dir /usr/local/src/redis-7.2.0/data
    
    # 允许所有网卡访问
    # bind 127.0.0.1 -::1
    
    # 关闭安全模式,允许外网访问
    protected-mode no
    
    # 使用守护线程允许
    daemonize yes
    
    ## slave 从节点还需要配置master地址(redis5.0之前使用slaveof)以及打开只读配置
    replicaof 159.159.159.159 6379
    # 配置从节点只读
    replica-read-omly yes 
    
  4. 通过配置文件启动各节点

    # 启动redis服务端
    ./src/redis-service ./redis.conf
    
  5. 查看服务允许状态

    ps -ef| grep redis
    
    #[root@node2 ~]# ps -ef| grep redis
    #root     21574 18189  0 10:34 pts/1    00:00:00 grep --color=auto redis
    #root     32447     1  0 Dec21 ?        00:01:27 src/redis-server *:6379
    
  6. 各节点启动完成后,master节点使用客户端连接redis

    # 启动redis客户端
    ./src/redis-cli
    
  7. info查看主从复制节点信息

    127.0.0.1:6379> info
    
    #  # Replication
    # role:master
    # connected_slaves:2
    # slave0:ip=139.139.139.139,port=6379,state=online,offset=87258,lag=1
    # slave1:ip=101.101.101.101,port=6379,state=online,offset=87258,lag=0
    # master_failover_state:no-failover
    # master_replid:a2ecc83726907a2c639fd8cfdb50c0b162637ff0
    # master_replid2:0000000000000000000000000000000000000000
    # master_repl_offset:87258
    # second_repl_offset:-1
    # repl_backlog_active:1
    # repl_backlog_size:1048576
    # repl_backlog_first_byte_offset:1
    # repl_backlog_histlen:87258
    

自此,Redis主从复制结构以搭建完成。

哨兵架构搭建

哨兵模式搭建是在主从模式的基础上,添加一个哨兵集群,监听主从复制架构主节点的监控状态,在主节点不可用时选取子节点为主节点继续提供服务。

  1. 进入安装目录
    # 安装路径
    cd /usr/local/src/redis-7.2.0/
    
  2. 复制sentinel.conf配置文件
    # 复制sentinel.conf -> sentinel.conf.bak
    cp sentinel.conf sentinel.conf.bak
    
  3. 修改sentinel.conf配置文件
    # ./sentinel.conf
    
    # 使用守护线程允许
    daemonize yes
    
    # 指定日志文件名称
    logfile "26379.log"
    
    #指定数据存放目录
    dir /usr/local/src/redis-7.2.0/data
    
    # 修改主节点地址 
    # sentinel monitor < master - name > < ip > < redis - port > < quorum >
    # quorum是一个数字,指明当有多少个sentinel认为一个master失效时(值一般为:sentinel / 2 + 1),master才算真正失效
    sentinel monitor mymaster 159.159.159.159 6379 2
    
  4. 通过配置文件启动Sentinel
    # 启动sentinel
    ./src/redis-sentinel ./sentinel.conf
    
  5. 各节点启动完成后,连接Sentinel
    # 启动redis客户端
    ./src/redis-cli -P 26379
    
  6. info查看Sentinel集群状态
    127.0.0.1:26379> info sentinel
    
    # # Sentinel
    # sentinel_masters:1
    # sentinel_tilt:0
    # sentinel_tilt_since_seconds:-1
    # sentinel_running_scripts:0
    # sentinel_scripts_queue_length:0
    # sentinel_simulate_failure_flags:0
    # master0:name=mymaster,status=ok,address=159.159.159.159:6379,slaves=2,sentinels=3
    
  7. 验证Sentinel哨兵(下线master节点一段时间后)
    127.0.0.1:26379> info sentinel
    
    # # Sentinel
    # sentinel_masters:1
    # sentinel_tilt:0
    # sentinel_tilt_since_seconds:-1
    # sentinel_running_scripts:0
    # sentinel_scripts_queue_length:0
    # sentinel_simulate_failure_flags:0
    # master0:name=mymaster,status=ok,address=139.139.139.139:6379,slaves=2,sentinels=3
    

自此,Redis哨兵(Sentinel)集群结构搭建完成。

集群架构搭建

  1. 进入安装目录

    # 安装路径
    cd /usr/local/src/redis-7.2.0/
    
  2. 单机下创建主从复制集群文件夹

    # 创建三个文件夹,分别保存三个redis节点实例数据
    mkdir -p cluster/6379/data cluster/6380/data cluster/6381/data
    
  3. 复制redis.conf

    cp redis.conf.bak cluster/6379/redis.conf
    
  4. 修改 ./cluster/6379/redis.conf 文件

    # ./redis.conf
    
    # 开启守护线程
    daemonize yes
    
    # 设置端口号(各节点端口号不同)
    port 6379
    
    # 指定日志文件明(各节点位置不同)
    logfile "6379.log"
    
    # 指定数据文件存放位置(各节点位置不同)
    dir /usr/local/src/redis-7.2.0/cluster/6379/data
    
    # 开启集群模式
    cluster-enabled yes
    
    # 集群节点信息文件 (各节点文件不同)
    cluster-config-file nodes-6379.config
    
    # 节点超时时间
    custer-node-timeout 5000
    
    # bind绑定的是自己机器的网卡ip,注释掉即可
    # bind 127.0.0.1
    
    # 关闭保护模式,允许外网访问
    protected-mode no
    
    # 开启aof持久化
    appendonly yes
    
    # 设置redis访问密码
    requirepass xxx
    
    # 设置集群节点间访问密码
    masterauth xxx
    
  5. 复制 ./cluster/6379/redis.conf 文件到6380、6381目录,并修改配置

    cp cluster/6379/redis.conf cluster/6380/redis.conf
    cp cluster/6379/redis.conf cluster/6381/redis.conf
    
  6. 创建集群

    # 每个master节点配置2个复制节点,最终形成 9/1+2 = 3 三个master节点的集群
    src/redis-cli -a yanggl123 --cluster create 159.159.159.159:6379 159.159.159.159:6380 159.159.159.159:6381 139.139.139.139:6379 139.139.139.139:6380 139.139.139.139:6381 101.101.101.101:6379 101.101.101.101:6380 101.101.101.101:6381 --cluster-replicas 2
    
    # Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    # >>> Performing hash slots allocation on 9 nodes...
    # Master[0] -> Slots 0 - 5460
    # Master[1] -> Slots 5461 - 10922
    # Master[2] -> Slots 10923 - 16383
    # Adding replica 139.139.139.139:6380 to 159.159.159.159:6379
    # Adding replica 101.101.101.101:6380 to 159.159.159.159:6379
    # Adding replica 159.159.159.159:6381 to 139.139.139.139:6379
    # Adding replica 101.101.101.101:6381 to 139.139.139.139:6379
    # Adding replica 139.139.139.139:6381 to 101.101.101.101:6379
    # Adding replica 159.159.159.159:6380 to 101.101.101.101:6379
    # M: aed34372434119e9ea5fd9332ac6b13776da04f5 159.159.159.159:6379
    # slots:[0-5460] (5461 slots) master
    # S: 2c309fc808e3c8effab01d9b751fc650cfebd721 159.159.159.159:6380
    # replicates 9fcbba06ff95de4a5352591d81e0a57b52f7ebe2
    # S: 136ddf18be30a78b91b3a2234c51b6c48f701c7a 159.159.159.159:6381
    # replicates 24ace9daa23390120c6196f2bfd4a4fb012a25db
    # M: 24ace9daa23390120c6196f2bfd4a4fb012a25db 139.139.139.139:6379
    # slots:[5461-10922] (5462 slots) master
    # S: f5f98671523d33545a3a881fb0fb6352a1df4e03 139.139.139.139:6380
    # replicates aed34372434119e9ea5fd9332ac6b13776da04f5
    # S: d81b9ed97af9692bdcb2dc6b7245f53e7ffda0e5 139.139.139.139:6381
    # replicates 9fcbba06ff95de4a5352591d81e0a57b52f7ebe2
    # M: 9fcbba06ff95de4a5352591d81e0a57b52f7ebe2 101.101.101.101:6379
    # slots:[10923-16383] (5461 slots) master
    # S: f0ff1d8c32698edc7685033f927c14a96553f721 101.101.101.101:6380
    # replicates aed34372434119e9ea5fd9332ac6b13776da04f5
    # S: e910bddff6df93ef9cabd6156015c3e4a2b9a8db 101.101.101.101:6381
    # replicates 24ace9daa23390120c6196f2bfd4a4fb012a25db
    # Can I set the above configuration? (type 'yes' to accept): yes
    # ... 
    # [OK] All nodes agree about slots configuration.
    # >>> Check for open slots...
    # >>> Check slots coverage...
    # [OK] All 16384 slots covered.
    
  7. 验证集群信息

    # 连接任意节点
    src/redis-cli -p 6379
    
    # 查看集群信息
    cluster info
    
    # cluster_state:ok
    # cluster_slots_assigned:16384
    # cluster_slots_ok:16384
    # cluster_slots_pfail:0
    # cluster_slots_fail:0
    # cluster_known_nodes:9
    # cluster_size:3
    # cluster_current_epoch:9
    # cluster_my_epoch:1
    # cluster_stats_messages_ping_sent:1282
    # cluster_stats_messages_pong_sent:1267
    # cluster_stats_messages_sent:2549
    # cluster_stats_messages_ping_received:1259
    # cluster_stats_messages_pong_received:1282
    # cluster_stats_messages_meet_received:8
    # cluster_stats_messages_received:2549
    # total_cluster_links_buffer_limit_exceeded:0
    
    # 查看集群节点
    cluster nodes
    
    # 24ace9daa23390120c6196f2bfd4a4fb012a25db 139.139.139.139:6379@16379 master - 0 1703232221593 4 connected 5461-10922
    # aed34372434119e9ea5fd9332ac6b13776da04f5 159.159.159.159:6379@16379 myself,master - 0 1703232221000 1 connected 0-5460
    # e910bddff6df93ef9cabd6156015c3e4a2b9a8db 101.101.101.101:6381@16381 slave 24ace9daa23390120c6196f2bfd4a4fb012a25db 0 1703232222000 4 connected
    # f0ff1d8c32698edc7685033f927c14a96553f721 101.101.101.101:6380@16380 slave aed34372434119e9ea5fd9332ac6b13776da04f5 0 1703232221612 1 connected
    # 2c309fc808e3c8effab01d9b751fc650cfebd721 159.159.159.159:6380@16380 slave 9fcbba06ff95de4a5352591d81e0a57b52f7ebe2 0 1703232221000 7 connected
    # 9fcbba06ff95de4a5352591d81e0a57b52f7ebe2 101.101.101.101:6379@16379 master - 0 1703232220606 7 connected 10923-16383
    # f5f98671523d33545a3a881fb0fb6352a1df4e03 139.139.139.139:6380@16380 slave aed34372434119e9ea5fd9332ac6b13776da04f5 0 1703232222597 1 connected
    # 136ddf18be30a78b91b3a2234c51b6c48f701c7a 159.159.159.159:6381@16381 slave 24ace9daa23390120c6196f2bfd4a4fb012a25db 0 1703232221789 4 connected
    # d81b9ed97af9692bdcb2dc6b7245f53e7ffda0e5 139.139.139.139:6381@16381 slave 9fcbba06ff95de4a5352591d81e0a57b52f7ebe2 0 1703232221000 7 connected
    

自此,Redis集群结构搭建完成。

上次编辑于:
贡献者: yanggl