Redis with HA

1. 概述

Redis(http://redis.io/)一个高性能的key-value数据库,可用于实时的计算和统计等。
Redis有多种HA方案,比如Master-Slave,Redis Cluster等方案。
本文采用Redis Sentinel来管理1 Master 2 Slave的3台Centos 6.3 VM实例的方式,每台VM实例有一个Redis进程和一个Sentinel进程。


对于小型的应用来说,此方案已经足够。

2. 部署配置

假设

redis master10.64.70.10
redis slave 110.64.70.20
redis slave 210.64.70.30



创建以下目录结构

/data/redis/config存放redis.conf sentinel.conf
/data/redis/backup/ 存放定期持久化的backup db
/date/redis/log/存放redis.log sentinel.log
/data/redis/logrotate/ 存放redis logrotate config:redis.rotate




在3台VM上安装redis并拷贝配置文件

$ wget http://download.redis.io/releases/redis-3.0.5.tar.gz
$ tar xzf redis-3.0.5.tar.gz
$ cd redis-3.0.5
$ make
$ make install

拷贝redis-3.0.5.tar.gz包中原始的redis.conf sentinel.conf 到/data/redis/config/ 并修改3台VM上的配置文件

Redis master redis.conf

# Master redis.conf

daemonize yes
pidfile /var/run/redis.pid
port 6379
bind 10.64.70.10
timeout 0
loglevel notice
logfile /data/redis/log/redis.log
databases 16
#save 900 1
#save 300 10
#save 60 10000
rdbcompression yes
dbfilename redis_dump.db
dir /data/redis/backup
#slaveof
appendonly no
slave-serve-stale-data yes
slave-read-only yes

Redis salve1 redis.conf

# Slave1 redis.conf

daemonize yes
pidfile /var/run/redis.pid
port 6379
bind 10.64.70.20
timeout 0
loglevel notice
logfile /data/redis/log/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename redis_dump.db
dir /data/redis/backup
slaveof 10.64.70.10 6379
appendonly yes
slave-serve-stale-data yes
slave-read-only yes

Redis slave 2 redis.conf

# Slave2 redis.conf

daemonize yes
pidfile /var/run/redis.pid
port 6379
bind 10.64.70.30
timeout 0
loglevel notice
logfile /data/redis/log/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename redis_dump.db
dir /data/redis/backup
slaveof 10.64.70.10 6379
appendonly yes
slave-serve-stale-data yes
slave-read-only yes

Every sentinel.conf

# all Sentinel sentinel.conf

daemonize yes
loglevel notice
logfile /data/redis/log/sentinel.log
port 26379
sentinel monitor redis1 10.64.70.10 6379 2
sentinel monitor redis2 10.64.70.20 6379 2
sentinel monitor redis3 10.64.70.30 6379 2
sentinel down-after-milliseconds redis1 5000
sentinel down-after-milliseconds redis2 5000
sentinel down-after-milliseconds redis3 5000
sentinel parallel-syncs redis1 1
sentinel parallel-syncs redis2 1
sentinel parallel-syncs redis3 1
sentinel failover-timeout redis1 900000
sentinel failover-timeout redis2 900000
sentinel failover-timeout redis3 900000

Logrotate: /data/redis/logrotate/redis.rotate

/data/redis/log/redis.log
/data/redis/log/sentinel.log
{
daily
rotate 30
copytruncate
notifempty
missingok
dateext
dateformat -%Y%m%d
}
Create cron job “00 00 * * * /usr/sbin/logrotate /data/redis/logrotate/redis.rotate -f” to execute the rotate.

3. 启动关闭

启动,在3台上执行
redis-server /data/redis/config/redis.conf
redis-server /data/redis/config/sentinel.conf --sentinel
关闭redis,分别执行
redis-cli -h 10.64.70.10 shutdown
redis-cli -h 10.64.70.20 shutdown
redis-cli -h 10.64.70.30 shutdown
关闭sentinel,在3台上执行
redis-cli -p 26379 shutdown

4. 监控

简单的监控方式是判断每台上的redis和sentinel进程是否存在,不存在则发送alert出来。
判断存在的脚本的例子,可以由zenoss定期执行根据返回结果向设定好的收件人发alert。
#!/bin/sh

LOCAL_IP=$(/sbin/ifconfig eth0 | /bin/awk '/inet addr/{print substr($2,6)}')
REDIS_PROCESS_NUM=$(/usr/bin/pgrep -l -f redis-server | /usr/bin/wc -l)

if [ "$REDIS_PROCESS_NUM" == "2" ]; then
    echo "OK: [$LOCAL_IP]Redis running"
    exit 0
else
    echo "ERROR: [$LOCAL_IP]Redis stopped"
    exit 1        
fi

实时监控方案1: zenoss + Redis Monitor ZenPack
http://wiki.zenoss.org/ZenPack:Redis_Monitor

实时监控方案2: Nagios + plugin check_redis.pl
http://exchange.nagios.org/directory/Plugins/Databases/check_redis-2Epl/details
https://github.com/willixix/WL-NagiosPlugins


### 配置基于 Nginx 和 Redis 的集群环境 在部署和配置基于 **Nginx** 和 **Redis** 的集群环境时,需要结合负载均衡、Session 共享以及集群管理等关键点。以下是一个完整的部署流程和技术细节说明。 --- #### 1. 环境准备 - 操作系统:CentOS 7.9 - 安装包: - `nginx-1.12.2.tar.gz` - `pcre-8.37.tar.gz`(用于正则表达式支持) - `apache-tomcat-7.0.78.tar.gz` - `jdk-8u161-linux-x64.tar.gz` - `redis-6.x` 确保所有依赖库已安装,包括 GCC 编译工具链、make 工具等。 --- #### 2. 安装与配置 Tomcat 节点 每个 Tomcat 节点需独立安装,并且启用 FarmWarDeployer 实现 WAR 包自动同步: ```bash tar -zxvf apache-tomcat-7.0.78.tar.gz -C /opt/tomcat1 tar -zxvf jdk-8u161-linux-x64.tar.gz -C /usr/local/ ``` 编辑 `/opt/tomcat1/conf/server.xml` 文件以启用集群功能: ```xml <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> <Valve className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="true"/> ``` 将应用 WAR 包放入任意节点的 `webapps` 目录即可触发自动同步到其他节点[^2]。 --- #### 3. 部署 Redis 集群 Redis 集群用于实现 Session 共享,部署多个 Redis 实例并配置为集群模式: 创建目录结构: ```bash mkdir -p /root/fhsdv2/redis-cluster/{7001,7002,7003,7004,7005,7006} ``` 编写每个实例的配置文件,如 `7001/redis.conf`: ```conf port 7001 cluster-enabled yes cluster-config-file nodes-7001.conf cluster-node-timeout 5000 appendonly yes dir /root/fhsdv2/redis-cluster/7001 ``` 启动所有 Redis 实例后,使用命令创建集群: ```bash redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 \ 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 \ --cluster-replicas 1 ``` 确保客户端连接使用支持 Redis 集群的驱动,例如 Lettuce 或 Jedis。 --- #### 4. 安装与配置 Nginx Nginx 用于负载均衡,将请求分发到多个 Tomcat 节点。 安装步骤: ```bash tar -zxvf pcre-8.37.tar.gz cd pcre-8.37 && ./configure && make && make install tar -zxvf nginx-1.12.2.tar.gz cd nginx-1.12.2 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-pcre=../pcre-8.37 make && make install ``` 配置负载均衡: 编辑 `/usr/local/nginx/conf/nginx.conf`: ```nginx http { upstream tomcat_cluster { least_conn; server 192.168.1.10:8080 weight=3; server 192.168.1.11:8080; server 192.168.1.12:8080 backup; keepalive 32; } server { listen 80; location / { proxy_pass http://tomcat_cluster; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } } ``` 重启 Nginx: ```bash /usr/local/nginx/sbin/nginx -s reload ``` --- #### 5. 配置 Session 共享 Tomcat 使用 Redisson 或 Spring Session 实现与 Redis 的集成: Maven 依赖示例: ```xml <dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-session</artifactId> <version>3.16.6</version> </dependency> ``` Spring Boot 应用中配置 Redis: ```yaml spring: redis: host: 127.0.0.1 port: 7001 lettuce: pool: max-active: 8 max-idle: 4 ``` 确保所有 Tomcat 节点指向相同的 Redis 集群地址。 --- #### 6. 自动化部署方案 可以借助脚本或 Ansible 实现一键自动化部署: - 自动生成配置文件参数。 - 自动解决依赖问题。 - 自动部署反向代理配置。 - 自动同步 Session 到 Redis。 - 支持一键升级和回滚。 --- #### 7. 日志与监控 - Redis 日志路径建议统一设置为 `/usr/local/redis/var/`。 - Nginx 日志路径默认为 `/usr/local/nginx/logs/`。 - 可使用 ELK 套件进行日志集中管理。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值