Redis服务搭建及使用Keepalived实现主从双机热备

时间:2022-12-19 10:37:59

Redis服务搭建及使用Keepalived实现主从双机热备

1.  简述

本文档根据互联网上博客参考,并在测试环境搭建成功,记录测试环境中Redis缓存服务器的搭建过程及实现主从双机热备架构的过程。

2.  测试环境

系统:Centos6.5

软件:redis-2.8.24.tar.gz  Keepalived v1.2.13

主IP: 192.168.3.3

从IP:192.168.3.4

VIP:   192.168.3.5

3.  Redis单机安装及配置

3.1     编译安装

A.准备好redis tar gz 放到/tmp目录

在线下载 wget http://download.redis.io/releases/redis-2.8.24.tar.gz

B.执行以下命令:

cd /tmp

tar -zxvf redis-2.8.24.tar.gz

cd redis-2.8.24

makeMALLOC=libc

make PREFIX=/usr/local/redis install

3.2     配置启动脚本redis和配置文件redis.conf

3.2.1   启动脚本redis

touch/etc/init.d/redis

 

#!/bin/sh

#chkconfig:2345 80 90

#

# Simple Redisinit.d script conceived to work on Linux systems

# as it doesuse of the /proc filesystem.

 

REDISPORT=6379

EXEC=/usr/local/redis/bin/redis-server 

CLIEXEC=/usr/local/redis/bin/redis-cli

 

PIDFILE=/var/run/redis.pid

CONF="/etc/redis/redis.conf"

 

REDISPASSWORD=123456

 

 

case"$1" in

    start)

        if [ -f $PIDFILE ]

        then

                echo "$PIDFILE exists,process is already running or crashed"

        else

                echo "Starting Redisserver..."

                $EXEC $CONF &

        fi

        ;;

    stop)

        if [ ! -f $PIDFILE ]

        then

                echo "$PIDFILE does notexist, process is not running"

        else

                PID=$(cat $PIDFILE)

                echo "Stopping ..."

                $CLIEXEC -p $REDISPORT -a$REDISPASSWORD shutdown

                while [ -x /proc/${PID} ]

                do

                    echo "Waiting forRedis to shutdown ..."

                    sleep 1

                done

                echo "Redis stopped"

        fi

        ;;

    *)

        echo "Please use start or stop asfirst argument"

        ;;

esac

3.2.2   配置文件redis.conf

       mkdir/etc/redis/

       cp/tmp/redis-2.8.24/redis.conf /etc/redis/

      

       修改内容:

       daemonizeyes

       port6379

       bind0.0.0.0

       requirepass123456

      

3.2.3 开机启动及防火墙

添加开机启动

chkconfig --add redis

chkconfig redis on

chkconfig --list | grep redis

 

添加防火墙

/sbin/iptables -I INPUT -p tcp --dport 3306-j ACCEPT

service iptables restart

/etc/rc.d/init.d/iptables save

more /etc/sysconfig/iptables

 

将Redis的命令所在目录添加到系统参数PATH中

并将此添加到.bashrc

#vi /etc/profile

exportPATH="$PATH:/usr/local/redis/bin"

 

启动服务

service redis start

 

查看服务启动情况:

[root@redis01 ~]# ps -ef | grep redis

root    18478 18465  0 06:28 pts/1    00:00:00 grep --color=auto redis

root    26404     1  0 Jan05 ?       00:04:40/usr/local/redis/bin/redis-server 0.0.0.0:6379

 

测试命令:

redis-cli -a 123456

 

127.0.0.1:6379> ping

PONG

证明安装配置完成。

4.  主从双机热备的实现

将192.168.3.3作为master将192.168.3.4作为slave 并统一访问地址为192.168.3.5;应用通过访问192.168.3.5:6379来访问redis缓存数据库。

 

实现主从双机热备,首先需要实现Redis的主从复制功能,然后使用Keepalived来实现系统的容灾,创建出VIP,在master故障时能够自动切换到slave机器上,并实现双机热备的能力。

 

4.1     Redis主从复制

 

Master的redis.conf 

slave-read-only no #因同时写入的只会有一个,此选项打开是为了防止master -slave数据不同步问题,我们此架构不存在master slave同时写入过程。所以我们将此选项关闭,为了当从master切换到slave后,应用也可以写入,不影响应用的正常使用。

 

Slave的redis.conf

slaveof 192.168.3.3 6379

masterauth 123456

slave-serve-stale-data yes

slave-read-only no

 

设置完成后,重启两台机器的redis服务。

 

验证主从复制是否正常:

终端登陆:

redis-cli -a 123456

 

info 输出:

master

[root@youlook-redis01 ~]# redis-cli -a123456

127.0.0.1:6379> info

# Server

redis_version:2.8.24

redis_git_sha1:00000000

redis_git_dirty:0

redis_build_id:24d3bdda0cc05ee1

redis_mode:standalone

os:Linux 2.6.32-642.6.2.el6.x86_64 x86_64

arch_bits:64

multiplexing_api:epoll

gcc_version:4.4.7

process_id:23070

run_id:f0d358edd0d6aad82171e4c16fae2c522ba62bd0

tcp_port:6379

uptime_in_seconds:3815852

uptime_in_days:44

hz:10

lru_clock:7551606

config_file:/etc/redis/redis.conf

 

# Clients

connected_clients:13

client_longest_output_list:0

client_biggest_input_buf:0

blocked_clients:0

 

# Memory

used_memory:29171896

used_memory_human:27.82M

used_memory_rss:116658176

used_memory_peak:518246264

used_memory_peak_human:494.24M

used_memory_lua:36864

mem_fragmentation_ratio:4.00

mem_allocator:libc

 

# Persistence

loading:0

rdb_changes_since_last_save:6

rdb_bgsave_in_progress:0

rdb_last_save_time:1483946348

rdb_last_bgsave_status:ok

rdb_last_bgsave_time_sec:0

rdb_current_bgsave_time_sec:-1

aof_enabled:0

aof_rewrite_in_progress:0

aof_rewrite_scheduled:0

aof_last_rewrite_time_sec:-1

aof_current_rewrite_time_sec:-1

aof_last_bgrewrite_status:ok

aof_last_write_status:ok

 

# Stats

total_connections_received:5481499

total_commands_processed:85348127

instantaneous_ops_per_sec:3

total_net_input_bytes:7877840209

total_net_output_bytes:28361548248

instantaneous_input_kbps:0.09

instantaneous_output_kbps:0.02

rejected_connections:2

sync_full:2

sync_partial_ok:0

sync_partial_err:1

expired_keys:93876

evicted_keys:0

keyspace_hits:33685304

keyspace_misses:2473872

pubsub_channels:0

pubsub_patterns:0

latest_fork_usec:3264

 

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.3.4,port=6379,state=online,offset=4237822823,lag=1

master_repl_offset:4237822823

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:4236774248

repl_backlog_histlen:1048576

 

# CPU

used_cpu_sys:5370.02

used_cpu_user:3799.63

used_cpu_sys_children:320.30

used_cpu_user_children:1659.35

 

# Keyspace

db0:keys=7124,expires=47,avg_ttl=128544654

 

slave端的info信息:

[root@youlook-redis02 ~]# redis-cli -a123456

127.0.0.1:6379> info

# Server

redis_version:2.8.24

redis_git_sha1:00000000

redis_git_dirty:0

redis_build_id:aa10e3525eecea17

redis_mode:standalone

os:Linux 2.6.32-642.6.2.el6.x86_64 x86_64

arch_bits:64

multiplexing_api:epoll

gcc_version:4.4.7

process_id:22720

run_id:5e61741d52bb82def68c30cc2df322f34135798b

tcp_port:6379

uptime_in_seconds:3816078

uptime_in_days:44

hz:10

lru_clock:7551784

config_file:/etc/redis/redis.conf

 

# Clients

connected_clients:3

client_longest_output_list:0

client_biggest_input_buf:0

blocked_clients:0

 

# Memory

used_memory:27903728

used_memory_human:26.61M

used_memory_rss:58122240

used_memory_peak:41421576

used_memory_peak_human:39.50M

used_memory_lua:36864

mem_fragmentation_ratio:2.08

mem_allocator:libc

 

# Persistence

loading:0

rdb_changes_since_last_save:1689

rdb_bgsave_in_progress:0

rdb_last_save_time:1483946706

rdb_last_bgsave_status:ok

rdb_last_bgsave_time_sec:0

rdb_current_bgsave_time_sec:-1

aof_enabled:0

aof_rewrite_in_progress:0

aof_rewrite_scheduled:0

aof_last_rewrite_time_sec:-1

aof_current_rewrite_time_sec:-1

aof_last_bgrewrite_status:ok

aof_last_write_status:ok

 

# Stats

total_connections_received:1907598

total_commands_processed:21038297

instantaneous_ops_per_sec:1

total_net_input_bytes:4343963223

total_net_output_bytes:188755136

instantaneous_input_kbps:0.02

instantaneous_output_kbps:0.06

rejected_connections:0

sync_full:0

sync_partial_ok:0

sync_partial_err:0

expired_keys:0

evicted_keys:0

keyspace_hits:90412

keyspace_misses:11

pubsub_channels:0

pubsub_patterns:0

latest_fork_usec:438

 

# Replication

role:slave

master_host:192.168.3.3

master_port:6379

master_link_status:up

master_last_io_seconds_ago:8

master_sync_in_progress:0

slave_repl_offset:4238304449

slave_priority:100

slave_read_only:0

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

 

# CPU

used_cpu_sys:3004.34

used_cpu_user:1546.73

used_cpu_sys_children:276.15

used_cpu_user_children:1587.06

 

# Keyspace

db0:keys=7124,expires=47,avg_ttl=184336271

 

4.2     KeepAlived实现双机热备

使用Keepalived实现VIP,并且通过notify_notify_master、notify_backup、notify_fault、notify_stop来实现容灾。

4.2.1 安装Keepalived

yum -y install keepalived

4.2.2   配置Keepalived.conf

在此贴出master 和slave的Keepalived.conf 配置文件以及相关的依赖脚本。

Master
/etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

  notification_email {

    acassen@firewall.loc

    failover@firewall.loc

    sysadmin@firewall.loc

   }

  notification_email_from Alexandre.Cassen@firewall.loc

  smtp_server 192.168.200.1

  smtp_connect_timeout 30

  router_id redis01

}

 

vrrp_script chk_redis {

   script "/etc/keepalived/scripts/redis_check.sh"     ###监控脚本  

   interval 2                                          ###监控时间

}

 

 

vrrp_instance VI_1 {

   state MASTER

   interface eth0

   virtual_router_id 51

   priority 100

   advert_int 1

   authentication {

       auth_type PASS

       auth_pass 1111

    }

   track_script {

       chk_redis             ###执行上面定义的chk_redis 

    }

   virtual_ipaddress {

       192.168.3.5

    }

   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

}

 

进入/etc/keepalived/scripts/查看依赖脚本

redis_backup.sh  redis_check.sh  redis_fault.sh  redis_master.sh  redis_stop.sh

 

redis_master.sh

#!/bin/bash

 

REDISCLI="/usr/local/redis/bin/redis-cli-a 123456"

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 192.168.3.4 6379 >>$LOGFILE  2>&1

sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态 

 

echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILE

$REDISCLI SLAVEOF NO ONE >> $LOGFILE2>&1

 

redis_backup.sh

#!/bin/bash 

REDISCLI="/usr/local/redis/bin/redis-cli-a 123456"

LOGFILE="/var/log/keepalived-redis-state.log"

 

echo "[backup]" >> $LOGFILE

date >> $LOGFILE

echo "Being slave...." >>$LOGFILE 2>&1

 

sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色 

echo "Run SLAVEOF cmd ...">> $LOGFILE

$REDISCLI SLAVEOF 192.168.3.4 6379 >>$LOGFILE  2>&1

 

redis_fault.sh

#!/bin/bash 

LOGFILE=/var/log/keepalived-redis-state.log

echo "[fault]" >> $LOGFILE

date >> $LOGFILE

redis_stop.sh

#!/bin/bash 

LOGFILE=/var/log/keepalived-redis-state.log

echo "[stop]" >> $LOGFILE

date >> $LOGFILE

redis_check.sh

#!/bin/bash 

ALIVE=`/usr/local/redis/bin/redis-cli -a123456 PING` 

if [ "$ALIVE" == "PONG"]; then

echo $ALIVE 

exit 0 

else

echo $ALIVE 

exit 1 

fi

Slave
/etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

  notification_email {

    acassen@firewall.loc

    failover@firewall.loc

    sysadmin@firewall.loc

   }

  notification_email_from Alexandre.Cassen@firewall.loc

  smtp_server 192.168.200.1

  smtp_connect_timeout 30

  router_id redis02

}

 

vrrp_script chk_redis {

   script "/etc/keepalived/scripts/redis_check.sh"     ###监控脚本 

   interval 2                   ###监控时间

}

vrrp_instance VI_1 {

   state BACKUP

   interface eth0

   virtual_router_id 51

   priority 10

   advert_int 1

   authentication {

       auth_type PASS

       auth_pass 1111

    }

   track_script {

       chk_redis        ###执行上面定义的chk_redis 

    }

   virtual_ipaddress {

       192.168.3.5

    }

   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

 

}

redis_master.sh

#!/bin/bash 

REDISCLI="/usr/local/redis/bin/redis-cli-a 123456"

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 192.168.3.3 6379 >>$LOGFILE  2>&1

sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态 

 

echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILE

$REDISCLI SLAVEOF NO ONE >> $LOGFILE2>&1

redis_backup.sh

#!/bin/bash 

REDISCLI="/usr/local/redis/bin/redis-cli-a 123456"

LOGFILE="/var/log/keepalived-redis-state.log"

 

echo "[backup]" >> $LOGFILE

date >> $LOGFILE

echo "Being slave...." >>$LOGFILE 2>&1

 

sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色 

echo "Run SLAVEOF cmd ...">> $LOGFILE

$REDISCLI SLAVEOF 192.168.3.3 6379 >>$LOGFILE  2>&1

redis_fault.sh

#!/bin/bash 

LOGFILE=/var/log/keepalived-redis-state.log

echo "[fault]" >> $LOGFILE

date >> $LOGFILE

redis_stop.sh

#!/bin/bash 

LOGFILE=/var/log/keepalived-redis-state.log

echo "[stop]" >> $LOGFILE

date >> $LOGFILE

redis_check.sh

#!/bin/bash 

ALIVE=`/usr/local/redis/bin/redis-cli -a123456 PING` 

if [ "$ALIVE" == "PONG"]; then

echo $ALIVE 

exit 0 

else

echo $ALIVE 

exit 1 

fi

 

4.2.3 开机启动及防火墙

添加开机启动

chkconfig keepalived on

 

添加防火墙

编辑/etc/sysconfig/iptables

添加:

-A INPUT -s 192.168.3.0/24 -p vrrp -jACCEPT

或者

-A INPUT -p vrrp -j ACCEPT

 

service iptables restart

/etc/rc.d/init.d/iptables save

more /etc/sysconfig/iptables

 

启动服务

service keepalived start

 

测试服务是否正常

从另外服务器ping 192.168.3.5 是否联通

测试redis是否正常

redis-cli -h 192.168.3.5 -p 6379 -a 123456 info

至此Redis服务器搭建及主从双机热备架构实现。
参考博客: http://hao360.blog.51cto.com/5820068/1435297