部署Redis+Keepalived高可用环境---Elastic Stack之四

1.安装redis服务

服务器角色:

主机名

ip

服务

elk1

192.168.1.223

jdk1.8,es7.2,logstash,

elk2

192.168.1.224

jdk1.8,es7.2,elasticsearch-head,

kibana,cerebro

elk3

192.168.1.225

jdk1.8,es7.2,redis2+keepalived

elk4

192.168.1.226

jdk1.8,redis1+keepalived+vip,

filebeat,metricbeat,rabbitmq,

Packbeat;heartbeat

1.安装redis服务及主从配置elk4(192.168.1.226),elk3(192.168.1.225)节点上都要操作
#yum install make gcc gcc-c++ tcl -y 
#wget http://download.redis.io/releases/redis-3.2.1.tar.gz
#tar xf redis-3.2.1.tar.gz -C /usr/local/
#cd /usr/local/redis-3.2.1/
#make
#cd /usr/local/redis-3.2.1/src
#make test  (如果这一步有报错,也需要处理)

2.添加相关文件及命令
#mkdir -p /usr/local/redis/bin/
#cd /usr/local/redis-3.2.1/src/
#cp -r redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server redis-sentinel  /usr/local/redis/bin/
#cp -r  /usr/local/redis-3.2.1/redis.conf   /etc/

3.添加redis启动脚本
#vim /etc/init.d/redis
#!/bin/bash
#chkconfig: 2345 10 90
#description: Start and Stop redis
REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
REDIS_CLI=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/etc/redis.conf"

case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi

if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;

stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;

restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac

4.添加执行权限
#chmod +x  /etc/init.d/redis 

5.设置开机自启
# chkconfig --add redis
# chkconfig redis on

6.创建redis状态日志和数据目录
#mkdir /var/log/redis/
#touch /var/log/redis/redis.log
#mkdir -p /var/redis/redis

7.redis主从配置(redis-master主节点的配置)
#vim /etc/redis.conf
port 6379
daemonize yes      #这个修改为yes
bind 0.0.0.0       #绑定的主机地址。说明只能通过这个ip地址连接本机的redis。最好绑定0.0.0.0;注意这个不能配置成127.0.0.1,否则复制会失败!用0.0.0.0或者本机ip地址都可以
pidfile /var/run/redis.pid
logfile /var/log/redis/redis.log
dir /var/redis/redis    #redis数据目录
appendonly yes           #启用AOF持久化方式
appendfilename "appendonly.aof" #AOF文件的名称
appendfsync everysec  #每秒钟强制写入磁盘一次,在性能和持久化方面做了很好的折中,是受推荐的方式。
save 900 1          #启用RDB快照功能,启用的
save 300 10
save 60 10000      #即在多少秒的时间内,有多少key被改变的数据添加到.rdb文件里
slave-serve-stale-data yes  #默认就会开启
slave-read-only yes
dbfilename dump.rdb   #快照文件名称

注:另一个从节点redis-slave的redis.conf配置和上面基本差不多,只是多了下面一行配置:
slaveof 192.168.1.226 6379

说明: 通过简单的配置slave(master端无需配置),用户就能使用redis的主从复制,即只需在slave端的redis.conf文件中配置下面一行:slaveof <masterip> <masterport>表示该redis服务作为slave,masterip和masterport分别为master 的ip和port;

#192.168.1.226的redis.conf

#192.168.1.225的redis.conf

8.启动redis服务
# /etc/init.d/redis  start
# ps -ef | grep  redis

2.keepalived +redis配置

1.安装keepalived
#yum install -y  keepalived
#cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

2.redis-master主节点的高可用配置
#cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
  router_id  redis-master
}
vrrp_script  check_redis {
  script  "/etc/keepalived/scripts/redis_check.sh  127.0.0.1 6379"
  interval    2
  timeout   2
  fall         3
}

vrrp_instance redis {
    state MASTER
    interface ens33
    lvs_sync_daemon_interface ens33
    virtual_router_id 202
    priority 150
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.230
    }

    track_script {
    check_redis
    }
   notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.1.225 6379"
   notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.1.225 6379"
   notify_fault /etc/keepalived/scripts/redis_fault.sh
   notify_stop /etc/keepalived/scripts/redis_stop.sh
}

3.redis-slave主节点的高可用配置
#cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
  router_id  redis-slave
}

vrrp_script  check_redis {
  script  "/etc/keepalived/scripts/redis_check.sh  127.0.0.1 6379"
  interval  2
  timeout   2
  fall      3
}
vrrp_instance redis {
    state BACKUP
    interface ens33
    lvs_sync_daemon_interface ens33
    virtual_router_id 202
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.230
    }
    track_script {
    check_redis
    }

   notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.1.226 6379"
   notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.1.226 6379"
   notify_fault /etc/keepalived/scripts/redis_fault.sh
   notify_stop /etc/keepalived/scripts/redis_stop.sh
}


4.在redis-master和redis-slave两个节点机器上都要创建监控脚本(下面几个脚本,在两个节点上都要同样配置)首先配置监控脚本
#mkdir -pv /etc/keepalived/scripts

#cat /etc/keepalived/scripts/redis_check.sh
#!/bin/bash
ALIVE=`/usr/local/redis/bin/redis-cli -h $1 -p $2 PING`
LOGFILE="/var/log/keepalived-redis-check.log"
echo "[CHECK]" >> $LOGFILE
date >> $LOGFILE
if [ $ALIVE == "PONG" ]; then :
   echo "Success: redis-cli -h $1 -p $2 PING $ALIVE" >> $LOGFILE 2>&1
    exit 0
else
    echo "Failed:redis-cli -h $1 -p $2 PING $ALIVE " >> $LOGFILE 2>&1
    exit 1
fi

5.在redis-master主节点上创建notity_master与notify_backup脚本:
#vim /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/redis/bin/redis-cli -h $1 -p $3"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ... " >> $LOGFILE
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE 2>&1

#echo "SLAVEOF $2 cmd can't excute ... " >> $LOGFILE
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
REDISCLI="/usr/local/redis/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[BACKUP]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE
sleep 100      #延迟100秒以后待数据同步完成后再取消同步状态
exit(0)

# vim /etc/keepalived/scripts/redis_fault.sh
#!/bin/bash
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[fault]" >> $LOGFILE
date >> $LOGFILE

# vim /etc/keepalived/scripts/redis_stop.sh
#!/bin/bash
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[stop]" >> $LOGFILE
date >> $LOGFILE

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

6.将redis-master主节点上的上面5个脚本直接复制到redis-slave节点上即可。
# scp -r /etc/keepalived/scripts/*.sh  elk3:/etc/keepalived/scripts/

7.设置环境变量(两个节点上都要设置)
#echo "export PATH=$PATH:/usr/local/redis/bin" >>/etc/profile
#source /etc/profile

8.启动两个节点上的keepalived服务
# systemctl enable keepalived 
# systemctl start keepalived 
# systemctl status keepalived 
# ps -ef|grep keepalived

9.查看下redis-master主节点,发现虚拟ip已经有了
#ip addr

 

 3.redis+keepalived故障切换测试

1.分别启动redis-mastr和redis-slave两个节点的redis和keepalived服务(如上已启动)
尝试通过VIP连接Redis:
#redis-cli -h 192.168.1.230 INFO|grep role
#redis-cli -h 192.168.1.226 INFO|grep role
#redis-cli -h 192.168.1.225 INFO|grep role

 连接成功,Slave也连接上来了。

2.尝试插入一些数据:
# redis-cli -h 192.168.1.230 SET Hello Redis
#从vip中获取数据
# redis-cli -h 192.168.1.230 GET  Hello
#主节点获取数据
#redis-cli -h 192.168.1.224 GET  Hello
#从节点获取数据
#redis-cli -h 192.168.1.225 GET  Hello


 参考链接:

https://www.cnblogs.com/kevingrace/p/9001975.html

https://www.cnblogs.com/kevingrace/p/910

 

posted @ 2020-05-23 15:40  非然踏古忘今焉  阅读(291)  评论(0编辑  收藏  举报