安装哨兵模式redis集群

环境说明

三台服务器 10.x.x.1 redis1,10.x.x.2 redis2, 10.x.x.3 redis3 ;
安装时设置redis1 服务器为主,redis2,redis3为从,配置好sentinel之后,主从关系将交给sentinel系统自动控制。

前置1:先安装gcc

yum install gcc-c++

前置2:安装jemalloc

  1. 下载jemalloc-5.0.1.tar.bz2, url地址https://github.com/jemalloc/jemalloc/releases
  2. 上传到家目录
  3. tar xvf jemalloc-5.0.1.tar.bz2
  4. cd jemalloc-5.0.1
  5. ./configure -prefix=/usr/local/jemalloc
  6. make -j8 && make install
  7. echo /usr/local/jemalloc/lib >> /etc/ld.so.conf
  8. ldconfig

前置3:增大系统核心参数somaxconn

vim /etc/sysctl.conf 增加一行

net.core.somaxconn=1024

然后执行命令
sysctl -p

正式开始安装redis

每台服务器上都安装如下步骤执行:
按照redis4.0.6

  1. 下载或上传 redis-4.0.6.tar.gz 到linux用户的家目录。

  2. 进入家目录,解压: tar xzf redis-4.0.6.tar.gz

  3. 运行命令: cd redis-4.0.6

  4. 运行命令: make MALLOC=jemalloc && make install,完成安装
    此步骤里切记要添加参数MALLOC=jemalloc,安装jemalloc方式的内存管理,防止内存碎片比例过高。
    make install安装完成后,会在/usr/local/bin目录下生成下面几个可执行文件,它们的作用分别是:
    redis-server:Redis服务器端启动程序
    redis-cli:Redis客户端操作工具。也可以用telnet根据其纯文本协议来操作
    redis-benchmark:Redis性能测试工具
    redis-check-aof:数据修复工具

  5. (配置sentinel模式则跳过本步)安装到系统服务 ./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
  1. 编辑redis配置文件
    mkdir /etc/redis
    mkdir -p /data/redis/log
    cp ~/redis-4.0.6/redis.conf /etc/redis/
    vim /etc/redis/redis.conf
内存最大8G字节 maxmemory 8589934592
绑定所有网口	bind 0.0.0.0
关闭保护模式  protected-mode no
开启守护模式  daemonize yes  
指定数据存储目录  dir /data/redis  
每秒一次aof写  appendfsync everysec  
打开aof持久化  appendonly yes  
指定日志文件  logfile "/data/redis/log/redis.log"

在2台从服务器的redis.conf里最后添加下面一行

slaveof 10.x.x.1 6379
  1. 编辑哨兵配置文件,全部内容如下
    vim /etc/redis/sentinel.conf
port 26379
dir "/tmp"
daemonize yes
protected-mode no
logfile "/data/redis/log/sentinel.log"
sentinel monitor mymaster 10.x.x.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 60000
  1. 分别启动redis:
    redis-server /etc/redis/redis.conf &

  2. 启动哨兵:
    方式一:redis-sentinel /etc/redis/sentinel.conf &(推荐,这种方式启动和redis实例没有任何关系)
    方式二:redis-server /etc/redis/sentinel.conf --sentinel &

如果发现哨兵模式不正常,master不能发现slave。则检查slave节点的/etc/redis/redis.conf,最后一行有没有 slaveof 6379

  1. 查看状态命令 redis-cli
127.0.0.1:6379> info replication
\# Replication
role:master
connected_slaves:2
slave0:ip=10.x.x.2,port=6379,state=online,offset=434,lag=1
slave1:ip=10.x.x.1,port=6379,state=online,offset=434,lag=1
master_replid:4219d672b92cb2ea4e9920eb36f3d20c55ee5cac
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:434
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:434
  1. 停止redis命令: redis-cli shutdown

  2. 停止sentinel : ps -ef|grep “[sentinel]”|grep -v grep|cut -c 9-15|xargs kill

  3. 连接sentinel : redis-cli -p 26379 ,命令 info sentinel、 sentinel master mymaster。

  4. 使用时: 10.x.x.1:26379,10.x.x.2:26379,10.x.x.3:26379

  5. 重启redis:
    redis-cli shutdown
    redis-server /etc/redis/redis.conf &

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值