运维相关之redis搭建

一、简介:REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统
集群模式:

    1) 主从复制
    2) 哨兵模式
    3) 分布式存储

    【redis主从】:
    是备份关系, 我们操作主库,数据也会同步到从库。 如果主库机器坏了,从库可以上。就好比你 D盘的片丢了,但是你移动硬盘里边备份有。
    
    【redis哨兵】:
    哨兵保证的是HA,保证特殊情况故障自动切换,哨兵盯着你的“redis主从集群”,如果主库死了,它会告诉你新的老大是谁。
    
    【redis集群】:
    集群保证的是高并发,因为多了一些兄弟帮忙一起扛。同时集群会导致数据的分散,整个redis集群会分成一堆数据槽,即不同的key会放到不不同的槽中
    
二、单节点安装步骤:
    1. 配置编译环境
    sudo yum install gcc-c++
    
    2. 下载源码
    cd /usr/local/src/
    wget http://download.redis.io/releases/redis-3.2.8.tar.gz
    tar -zxvf redis-3.2.8.tar.gz
    cd redis-3.2.8
    
    3. 执行make编译Redis
    make MALLOC=libc
    make install
    
    4. 配置Redis能随系统启动
    ./utils/install_server.sh
    #无需修改的话一直回车即可
    Selected config:
    Port           : 6379
    Config file    : /etc/redis/6379.conf
    Log file       : /var/log/redis_6379.log
    Data dir       : /var/lib/redis/6379
    Executable     : /usr/local/bin/redis-server
    Cli Executable : /usr/local/bin/redis-cli
    
    5. Redis服务查看、开启、关闭
    a.通过ps -ef|grep redis命令查看Redis进程
    b.开启Redis服务操作通过/etc/init.d/redis_6379 start命令
    c.关闭Redis服务操作通过/etc/init.d/redis_6379 stop命令
    #登录redis
    redis-cli -h 127.0.0.1 -p 6379 -a zdvictory666
    
    6. 配置文件修改
    # 设定密码,去掉 requirepass 的注释
    requirepass zdvictory666
    logfile /var/log/redis_6379.log
    
三、分布式存储安装步骤:
    1. 每个节点启动两个redis进程,共三台服务器
    2. 安装编译环境:yum install gcc-c++
    3. 下载安装包,并编译安装
    4. 每台服务器,配两个配置文件
    5. 启动三台服务器,六个节点
    6. redis-trib.rb环境准备(安装Ruby、安装rubygem redis依赖、检查redis-trib.rb的可用性)
    7. 一条命令创建集群(./redis-4.0.8/src/redis-trib.rb create  --replicas 1 192.168.1.21:7001 192.168.1.22:7002)
    8. 检查集群完整性(redis-4.0.8/src/redis-trib.rb check 192.168.1.22:7002)
    9. 如果某个节点挂掉,步骤:
        1) 启动挂掉的节点
        2) 修复节点(./redis-trib.rb fix 192.168.1.21:7001)
        3) 加入节点(./redis-trib.rb add-node 192.168.1.21:7001 192.168.1.21:7002) #前面是需要被加入的节点,后面是其中一个主节点
        4) 手动指定从节点到某个主节点(cluster replicas 主节点ID)
        5) 再检查一下集群状态

四、哨兵模式搭建:
    1. 规划,单机搭建伪集群
    服务类型    是否主服务器    IP地址        端口
    Redis        是                127.0.0.1    6379
    Redis        否                127.0.0.1    6380
    Redis        否                127.0.0.1    6381
    sentinel    (哨兵)             127.0.0.1    26279
    sentinel    (哨兵)            127.0.0.1    26280
    sentinel    (哨兵)            127.0.0.1    26281
    
    2. 安装依赖
    yum install -y wget gcc
    
    3. 安装redis
    mkdir -p /usr/local/redis/config/sentinel-{26379,26380,26381}
    mkdir -p /usr/local/redis/config/redis-{6379,6380,6381}
    cd /usr/local/redis 
    wget http://download.redis.io/releases/redis-5.0.7.tar.gz
    tar -xzf redis-5.0.7.tar.gz
    cd redis-5.0.7
    make && make install
    
    4. 配置redis文件
    cp /usr/local/redis/redis-5.0.7/redis.conf /usr/local/redis/config/redis-6379/redis-6379.conf
    cp /usr/local/redis/redis-5.0.7/redis.conf /usr/local/redis/config/redis-6380/redis-6380.conf
    cp /usr/local/redis/redis-5.0.7/redis.conf /usr/local/redis/config/redis-6381/redis-6381.conf
    #修改主配置
    vi /usr/local/redis/config/redis-6379/redis-6379.conf

    bind 0.0.0.0        #网络互通
    port 6379
    protected-mode no    #关闭保护模式
    daemonize yes        #后台启动
    requirepass 123456  #配置认证密码
    dir /usr/local/redis/config/redis-6379  #rdb文件目录
    pidfile /usr/local/redis/config/redis-6379/redis_6379.pid  
    logfile "/usr/local/redis/logs/redis-6379.log"
    masterauth "123456"    #指定主节点密码,如果不加密码,当主节点挂掉重启的时候会连不上主节点


    
    #修改两个从配置
    vi /usr/local/redis/config/redis-6380/redis-6380.conf

    bind 0.0.0.0
    port 6380
    protected-mode no
    daemonize yes
    requirepass 123456
    dir /usr/local/redis/config/redis-6380 
    pidfile /usr/local/redis/config/redis-6380/redis_6380.pid
    logfile "/usr/local/redis/config/redis-6380/redis-6380.log"    
    replicaof 127.0.0.1 6379    #指定主节点IP和端口
    masterauth "123456"            #指定主节点密码


    

    #修改两个从配置
    vi /usr/local/redis/config/redis-6381/redis-6381.conf

    bind 0.0.0.0
    port 6381
    protected-mode no
    daemonize yes
    requirepass 123456
    dir /usr/local/redis/config/redis-6381
    pidfile /usr/local/redis/config/redis-6381/redis_6381.pid
    logfile "/usr/local/redis/config/redis-6381/redis-6381.log"        
    replicaof 127.0.0.1 6379    #指定主节点IP和端口
    masterauth "123456"            #指定主节点密码


    
    
    5. 启动主从redis
    redis-server /usr/local/redis/config/redis-6379/redis-6379.conf
    redis-server /usr/local/redis/config/redis-6380/redis-6380.conf
    redis-server /usr/local/redis/config/redis-6381/redis-6381.conf
    
    redis-cli -h 127.0.0.1 -p 6379 -a 123456
    127.0.0.1:6380> info replication
    # Replication
    role:master
    connected_slaves:2
    slave0:ip=127.0.0.1,port=6380,state=online,offset=210,lag=0
    slave1:ip=127.0.0.1,port=6381,state=online,offset=210,lag=1
    master_replid:b9ebb7007a63570077018be9c0761439ae264de2
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:210
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:210
    
    6. 修改哨兵配置文件
    cp /usr/local/redis/redis-5.0.7/sentinel.conf /usr/local/redis/config/sentinel-26379/sentinel-26379.conf
    cp /usr/local/redis/redis-5.0.7/sentinel.conf /usr/local/redis/config/sentinel-26380/sentinel-26380.conf
    cp /usr/local/redis/redis-5.0.7/sentinel.conf /usr/local/redis/config/sentinel-26381/sentinel-26381.conf
    
    #修改sentinel-26379.conf配置文件

    port 26379
    daemonize yes
    pidfile /usr/local/redis/config/sentinel-26379/redis-sentinel-26379.pid
    logfile "/usr/local/redis/config/sentinel-26379/redis-sentinel-26379.log"
    ## 修改3:取消注释,增加主服务信息和需要进行选举的法定个数(两台从服务器)
    sentinel monitor mymaster 127.0.0.1 6379 2
    #定义主密码
    sentinel auth-pass mymaster 123456
    # 3s内mymaster无响应,则认为mymaster宕机了
    sentinel down-after-milliseconds mymaster 3000
    #指定在故障切换准许的毫秒数,当超过这个毫秒数的时候,就认为切换故障失败,默认三分钟
    sentinel failover-timeout mymaster 10000


    
    #修改sentinel-26380.conf配置文件

    port 26380
    daemonize yes
    pidfile /usr/local/redis/config/sentinel-26380/redis-sentinel-26380.pid
    logfile "/usr/local/redis/config/sentinel-26380/redis-sentinel-26380.log"
    ## 修改3:取消注释,增加主服务信息和需要进行选举的法定个数(两台从服务器)
    sentinel monitor mymaster 127.0.0.1 6379 2
    #定义主密码
    sentinel auth-pass mymaster 123456
    # 3s内mymaster无响应,则认为mymaster宕机了
    sentinel down-after-milliseconds mymaster 3000
    #指定在故障切换准许的毫秒数,当超过这个毫秒数的时候,就认为切换故障失败,默认三分钟
    sentinel failover-timeout mymaster 10000 

   
    
    
    #修改sentinel-26381.conf配置文件

    port 26381
    daemonize yes
    pidfile /usr/local/redis/config/sentinel-26381/redis-sentinel-26381.pid
    logfile "/usr/local/redis/config/sentinel-26381/redis-sentinel-26381.log"
    ## 修改3:取消注释,增加主服务信息和需要进行选举的法定个数(两台从服务器)
    sentinel monitor mymaster 127.0.0.1 6379 2
    #定义主密码
    sentinel auth-pass mymaster 123456
    # 3s内mymaster无响应,则认为mymaster宕机了
    sentinel down-after-milliseconds mymaster 3000
    #指定在故障切换准许的毫秒数,当超过这个毫秒数的时候,就认为切换故障失败,默认三分钟
    sentinel failover-timeout mymaster 10000  

 
    
    7. 启动哨兵redis
    redis-sentinel /usr/local/redis/config/sentinel-26379/sentinel-26379.conf
    redis-sentinel /usr/local/redis/config/sentinel-26380/sentinel-26380.conf
    redis-sentinel /usr/local/redis/config/sentinel-26381/sentinel-26381.conf
    
    #查看启动日志
    cat /usr/local/redis/config/sentinel-26381/redis-sentinel-26381.log
    105800:X 01 Feb 2021 17:49:32.074 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    105800:X 01 Feb 2021 17:49:32.074 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=105800, just started
    105800:X 01 Feb 2021 17:49:32.074 # Configuration loaded
    105801:X 01 Feb 2021 17:49:32.077 * Running mode=sentinel, port=26279.
    105801:X 01 Feb 2021 17:49:32.078 # Sentinel ID is 81c3169ea8b38ed896c43f4a69813721b9eeb0da
    105801:X 01 Feb 2021 17:49:32.078 # +monitor master mymaster 127.0.0.1 6379 quorum 2
    105801:X 01 Feb 2021 17:49:32.079 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
    105801:X 01 Feb 2021 17:49:32.080 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
    105801:X 01 Feb 2021 17:50:17.418 * +sentinel sentinel 954ba8335e63b557f37945a315118fa49d8dc08a 127.0.0.1 26280 @ mymaster 127.0.0.1 6379
    105801:X 01 Feb 2021 17:50:23.207 * +sentinel sentinel d62673a744a61a64d8296936218ab362eb04031d 127.0.0.1 26281 @ mymaster 127.0.0.1 6379
    
    #查看sentinel状态
    redis-cli -p 26279
    127.0.0.1:26279> info sentinel
    # Sentinel
    sentinel_masters:1
    sentinel_tilt:0
    sentinel_running_scripts:0
    sentinel_scripts_queue_length:0
    sentinel_simulate_failure_flags:0
    master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值