阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

132次阅读
没有评论

共计 11627 个字符,预计需要花费 30 分钟才能阅读完成。

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 服务器搭建及主从双机热备架构实现。

下面关于 Redis 的文章您也可能喜欢,不妨参考下:

Ubuntu 14.04 下 Redis 安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis 主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm

Redis 集群明细文档 http://www.linuxidc.com/Linux/2013-09/90118.htm

Ubuntu 14.04 安装 Redis 与简单配置  http://www.linuxidc.com/Linux/2017-01/139075.htm

Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

CentOS 7.0 安装 Redis 3.2.1 详细过程和使用常见问题 http://www.linuxidc.com/Linux/2016-09/135071.htm

Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

Ubuntu 15.10 下 Redis 集群部署文档 http://www.linuxidc.com/Linux/2016-06/132340.htm

Redis 实战 中文 PDF http://www.linuxidc.com/Linux/2016-04/129932.htm

Redis 的详细介绍:请点这里
Redis 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-01/139793.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计11627字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中