CentOS下Redis高可用安装笔记

(WJW)Redis高可用安装笔记

[x] 安装环境介绍:

Master: T1
Slave: T2
VIP: 192.168.68.45


[x] 安装Redis(Master,Slave)

注意: 安装redis前flushall的修改

查找src/redis.c文件,把

`{"flushdb",flushdbCommand,1,"w",0,NULL,0,0,0,0,0},`
 `{"flushall",flushallCommand,1,"w",0,NULL,0,0,0,0,0},`

修改成:

`{"xflushdb",flushdbCommand,1,"w",0,NULL,0,0,0,0,0},`
 `{"xflushall",flushallCommand,1,"w",0,NULL,0,0,0,0,0},`
vi /etc/sysctl.conf
添加一行: `vm.overcommit_memory=1`

mkdir /opt/redis
mkdir /opt/redis/log
mkdir /opt/redis/db

tar zxvf ./redis-2.8.17.tar.gz
cd redis-2.8.17
make PREFIX=/opt/redis install

[x] Redis启动脚本(Master,Slave):/opt/redis/bin/startRedis.sh

#!/bin/bash

basedir=`dirname $0`
echo "Redis BASE DIR:$basedir"
cd $basedir

nohup ./redis-server ./redis.conf > /dev/null 2>&1 &

[x] Redis停止脚本(Master,Slave):/opt/redis/bin/StopRedis.sh

#!/bin/sh

basedir=`dirname $0`
echo "Redis BASE DIR:$basedir"
cd $basedir

./redis-cli -h localhost -a 123456 shutdown

[x] Redis配置文件(Master,Slave):/opt/redis/bin/redis.conf

#requirepass 123456

pidfile /opt/redis/bin/redis.pid
logfile /opt/redis/log/redis.log
dir /opt/redis/db/

daemonize yes
port 6379
timeout 300
loglevel warning
databases 16
maxmemory 1g

#不要快照
#save 900 1
#save 300 10
#save 60 10000
#rdbcompression yes
#dbfilename dump.rdb

#使用AOF
appendonly yes
appendfsync everysec

[x] 安装Keepalived(Master,Slave):

#wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
tar zxvf keepalived-1.2.13.tar.gz
cd ./keepalived-1.2.13
./configure
make
make install

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/sbin/keepalived /usr/sbin/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
chkconfig --add keepalived
#chkconfig --level 35 keepalived on
#service keepalived start

[x] notify_*解释

Keepalived在转换状态时会依照状态来呼叫:
当进入Master状态时会呼叫notify_master
当进入Backup状态时会呼叫notify_backup
当发现异常情况时(track_script,track_interface失败)进入Fault状态呼叫notify_fault
当Keepalived程序终止时则呼叫notify_stop

[x] 在Master上创建如下配置文件:

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server localhost
   smtp_connect_timeout 30
   router_id LVS_REDIS
}

vrrp_script chk_redis {
  script "/etc/keepalived/scripts/redis_check.sh"   ###监控脚本
  interval 2                                        ###监控时间
}

vrrp_instance VI_1 {
    nopreempt ###不抢占,防止脑裂
    state MASTER             #备的是BACKUP
    interface br0
    virtual_router_id 51
    priority 100            #备的是90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass redis
    }
    virtual_ipaddress {
        192.168.68.45
    }

    track_script {
      chk_redis                       ###执行上面定义的chk_redis
    }

    notify_master /etc/keepalived/scripts/redis_master.sh
    notify_backup /etc/keepalived/scripts/redis_backup.sh
    notify_fault  /etc/keepalived/scripts/redis_fault.sh
    notify_stop   /etc/keepalived/scripts/redis_stop.sh
}

[x] 在Slave上创建如下配置文件:

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server localhost
   smtp_connect_timeout 30
   router_id LVS_REDIS
}

vrrp_script chk_redis {
  script "/etc/keepalived/scripts/redis_check.sh"   ###监控脚本
  interval 2                                        ###监控时间
}

vrrp_instance VI_1 {
    nopreempt ###不抢占,防止脑裂
    state BACKUP             #备的是BACKUP
    interface br0
    virtual_router_id 51
    priority 90            #备的是90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass redis
    }
    virtual_ipaddress {
        192.168.68.45
    }

    track_script {
      chk_redis                       ###执行上面定义的chk_redis
    }

    notify_master /etc/keepalived/scripts/redis_master.sh
    notify_backup /etc/keepalived/scripts/redis_backup.sh
    notify_fault  /etc/keepalived/scripts/redis_fault.sh
    notify_stop   /etc/keepalived/scripts/redis_stop.sh
}

[x] 在Master和Slave上创建监控Redis的脚本:

mkdir /etc/keepalived/scripts/
vi /etc/keepalived/scripts/redis_check.sh

#!/bin/bash

C_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`
ALIVE=`/opt/redis/bin/redis-cli PING`

if [ "$ALIVE" == "PONG" ]; then
  echo "${C_DATE} $ALIVE"
  exit 0
else
  echo "${C_DATE} $ALIVE"
  exit 1
fi

[x] 在Master与Slave创建如下脚本notify_faultnotify_stop

vim /etc/keepalived/scripts/redis_fault.sh

#!/bin/bash

C_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`
LOGFILE=/var/log/keepalived-redis-state.log

echo "${C_DATE} [fault]" >> $LOGFILE

vim /etc/keepalived/scripts/redis_stop.sh

#!/bin/bash

C_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`
LOGFILE=/var/log/keepalived-redis-state.log

echo "${C_DATE} [stop]" >> $LOGFILE

[x] 在Master上创建notity_masternotify_backup脚本:

vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bash

C_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`
REDISCLI="/opt/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"

echo "${C_DATE} [master]" >> $LOGFILE

#当keepalived配置为"抢占式"时,打开下面注释
#echo "Being master...." >> $LOGFILE 2>&1
#echo "Run SLAVEOF cmd ..." >> $LOGFILE
#$REDISCLI SLAVEOF T2 6379 >> $LOGFILE  2>&1
#sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态

echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

vim /etc/keepalived/scripts/redis_backup.sh

#!/bin/bash

C_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`
REDISCLI="/opt/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"

echo "${C_DATE} [backup]" >> $LOGFILE

#当keepalived配置为"抢占式"时,打开下面注释
#echo "Being slave...." >> $LOGFILE 2>&1
#sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色

echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF T2 6379 >> $LOGFILE  2>&1

[x] 在Slave上创建notity_masternotify_backup脚本:

vim /etc/keepalived/scripts/redis_master.sh

#!/bin/bash

C_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`
REDISCLI="/opt/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"

echo "${C_DATE} [master]" >> $LOGFILE

#当keepalived配置为"抢占式"时,打开下面注释
#echo "Being master...." >> $LOGFILE 2>&1
#echo "Run SLAVEOF cmd ..." >> $LOGFILE
#$REDISCLI SLAVEOF T1 6379 >> $LOGFILE  2>&1
#sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态

echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

vim /etc/keepalived/scripts/redis_backup.sh

#!/bin/bash

C_DATE=`date +"[%Y-%m-%d %H:%M:%S]"`
REDISCLI="/opt/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"

echo "${C_DATE} [backup]" >> $LOGFILE

#当keepalived配置为"抢占式"时,打开下面注释
#echo "Being slave...." >> $LOGFILE 2>&1
#sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色

echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF T1 6379 >> $LOGFILE  2>&1

[x] 在Master和Slave上,给监控脚本都加上可执行权限:

chmod +x /etc/keepalived/scripts/*.sh

[x] 启动步骤:

  1. 启动Master上的Redis
    /opt/redis/bin/startRedis.sh
  2. 启动Slave上的Redis
    /opt/redis/bin/startRedis.sh
  3. 启动Master上的Keepalived
    service keepalived start
  4. 启动Slave上的Keepalived
    service keepalived start
时间: 2024-11-11 12:26:34

CentOS下Redis高可用安装笔记的相关文章

kubernetes实战(二十七):CentOS 8 二进制 高可用 安装 k8s 1.16.x

1. 基本说明 本文章将演示CentOS 8二进制方式安装高可用k8s 1.16.x,相对于其他版本,二进制安装方式并无太大区别.CentOS 8相对于CentOS 7操作更加方便,比如一些服务的关闭,无需修改配置文件即可永久生效,CentOS 8默认安装的内核版本是4.18,所以在安装k8s的过程中也无需在进行内核升级,系统环境也可按需升级,如果下载的是最新版的CentOS 8,系统升级也可省略. 2. 基本环境配置 主机信息 192.168.1.19 k8s-master01 192.168

CentOS 8 二进制 高可用 安装 k8s 1.16.x

基本说明 本文章将演示CentOS 8二进制方式安装高可用k8s 1.16.x,相对于其他版本,二进制安装方式并无太大区别.CentOS 8相对于CentOS 7操作更加方便,比如一些服务的关闭,无需修改配置文件即可永久生效,CentOS 8默认安装的内核版本是4.18,所以在安装k8s的过程中也无需在进行内核升级,系统环境也可按需升级,如果下载的是最新版的CentOS 8,系统升级也可省略. 基本环境配置 主机信息 192.168.1.19 k8s-master01 192.168.1.18

kubernetes实战(三十):CentOS 8 二进制 高可用 安装 k8s 1.17.x

1. 基本说明 本文章将演示CentOS 8二进制方式安装高可用k8s 1.17.x,相对于其他版本,二进制安装方式并无太大区别. 2. 基本环境配置 主机信息 192.168.1.19 k8s-master01 192.168.1.18 k8s-master02 192.168.1.20 k8s-master03 192.168.1.88 k8s-master-lb 192.168.1.21 k8s-node01 192.168.1.22 k8s-node02 系统环境 [[email pro

centos下redis集群安装

Redis集群部署文档(centos6系统) (要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下) 127.0.0.1:7000127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 1:下载redis.官网下载3.0.0版本,之前2.几的版本不支持集群模式 下载地址:https://github.com

CentOS 下 redis 安装与配置

CentOS 下 redis 安装与配置 1.到官网上找到合适版本下载解压安装 Xml代码   [[email protected] src]# wget -c http://redis.googlecode.com/files/redis-2.4.7.tar.gz [[email protected] src]# tar -zxv -f redis-2.4.7.tar.gz [[email protected] src]# cd /usr/local/src/redis-2.4.7 [[ema

mac与centos下redis的安装与配置

前言 最近在用redis,下面简单写一下mac和centos下redis的安装与配置方法. 安装 mac下面 安装命令:brew intall redis 运行命令:brew services start redis centos centos在su用户下运行,或者加sudo 安装命令:yum install redis 运行命令:service redis start 配置 mac 在mac下,其配置文件路径是/usr/local/etc/redis.conf centos 在centos下,

CentOS下Redis的安装

CentOS下Redis的安装 [TOC] 前言 安装Redis需要知道自己需要哪个版本,有针对性的安装,比如如果需要redis GEO这个地理集合的特性,那么redis版本就不能低于3.2版本,由于这个特性是3.2版本才有的.另外需要注意的是,Redis约定次版本号(即第一个小数点后的数字)为偶数的版本是稳定版(如2.8版.3.0版),奇数版本是非稳定版(如2.7版.2.9版),生产环境下一般需要使用稳定版本. 下载安装包 wget http://download.redis.io/relea

利用lvs keepalived配置redis高可用及负载均衡

需求 我们用ES做日志服务,架构是 上游数据来源=>redis=>logstash=>ES redis目前还是单点, 没有做高可用, 现在数据量越来越多, 如果下游消费不出问题还好, redis里面数据来了就走,但是下游一旦出问题, 分给redis的内存半小时就撑满了. 看到redis3.0 beta版本已经提供了集群功能, 但是需要client以集群模式接入, 我们这么多上游用户, 不太可能统一要求他们改造. 公司也有硬件的LB, 同事在E公司的时候就是用的硬件LB. 但接入还要申请,

搭建一个redis高可用系统

一.单个实例 当系统中只有一台redis运行时,一旦该redis挂了,会导致整个系统无法运行. 单个实例 二.备份 由于单台redis出现单点故障,就会导致整个系统不可用,所以想到的办法自然就是备份(一般工业界认为比较安全的备份数应该是3份).当一台redis出现问题了,另一台redis可以继续提供服务. 备份 三.自动故障转移 虽然上面redis做了备份,看上去很完美.但由于redis目前只支持主从复制备份(不支持主主复制),当主redis挂了,从redis只能提供读服务,无法提供写服务.所以