首页 > 代码库 > Redis3.2.8+sentinel构建HA架构文档

Redis3.2.8+sentinel构建HA架构文档


一、环境准备:
HA架构至少需要一主一从
1.1、主机规划:

技术分享
master端:192.168.56.11
slave端:192.168.56.12
View Code

1.2、安装redis环境
两台机器分别安装redis3.2.8
安装步骤如下:

技术分享
cd /usr/local/
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar zxvf redis-3.2.8.tar.gz
cd redis-3.2.8
make
make install
mkdir -p /usr/local/redis/{bin,etc,logs}
cd /usr/local/redis/bin/
cp /usr/local/bin/redis-* /usr/local/redis/bin/
cd /usr/local/redis/etc/
View Code

编辑redis主配置文件

技术分享
cat > redis.conf << EOF
daemonize yes
pidfile "/var/run/redis.pid"
port 6379
bind 192.168.56.11   ##bind解析为服务器地址,连接时需要加 -h参数
timeout 0
tcp-backlog 35000
tcp-keepalive 0
loglevel notice
logfile "/usr/local/redis/logs/redis.log"
databases 16
stop-writes-on-bgsave-error yes
dbfilename "dump.rdb"
dir "/usr/local/redis"
Save “”
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxmemory 13824mb
appendonly no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
requirepass "123456"  ### redis登陆密码认证
masterauth "123456"   ### redis主从连接密码认证,若主库无密码,则该两项可省略
maxmemory-policy volatile-lru

EOF
View Code

修改内核参数

技术分享
cat >> /etc/sysctl.conf <<EOF
vm.overcommit_memory = 1
net.core.somaxconn = 65535
EOF
sysctl -p
sysctl vm.overcommit_memory=1
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/enabled >> /etc/rc.local
echo "65535" > /proc/sys/net/core/somaxconn
View Code

至此,两台redis环境安装完成!

 

1.3、部署主从环境

主库redis.conf文件如上所示保持不变

从库在redis.conf文件中改动如下所示:

技术分享
bind 192.168.56.12 ##从节点自身的IP地址
slaveof 192.168.56.11 6379 ##主库地址和端口
View Code

到此,主从配置文件完成。

接着,分别启动两台redis服务
Redis启停方式:
启动:
 /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf 

停止:

技术分享
 /usr/local/redis/bin/redis-cli shutdown ###无密码停止

 /usr/local/redis/bin/redis-cli -a 123456 -h 192.168.56.11 -p 6379 shutdown ###有密码停止 
View Code

验证主从同步:

Redis连接方式:
 /usr/local/redis/bin/redis-cli -h 192.168.56.11 -a 123456 -p 6379 

在master端登陆redis:

技术分享
/usr/local/redis/bin/redis-cli -h 192.168.56.11 -a 123456 -p 6379
192.168.56.11:6379> info replication #输出此命令查看master端状态
Replication
role:master
connected_slaves:1 ##获得连接
slave0:ip=192.168.56.12,port=6379,state=online,offset=3309,lag=1 ###获得slave端信息。证明slave连接成功
master_repl_offset:3450
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:3449
View Code

若出现如上信息,则证明主从同步成功。

 

1.4、配置sentinel哨兵模式
两台服务器中均部署sentinel,两台环境均按一下步骤操作

将redis解压文件夹中的sentinel.conf文件拷贝至redis配置文件夹中:
 cp -a /usr/local/redis-3.2.8/sentinel.conf /usr/local/redis/etc 

修改内容如下:

技术分享
port 26379 #sentinel监听端口
daemonize yes #以daemon形式运行在后台
logfile /usr/local/redis/logs/sentinel.log #日志文件
dir /usr/local/redis
sentinel monitor mymaster 192.168.56.11 6379 2 #监控mymaster组,master地址,端口,quorum次数
sentinel down-after-milliseconds mymaster 5000 #5000毫秒即5秒连续不能连通master,认为master挂掉
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 60000 #故障切换超时时间
sentinel auth-pass mymaster 123456 #密码认证
protected-mode no #默认情况下sentinel只监听环回地址,这样就导致sentinel之间不能通信,可以改为监听网卡或者关闭protected-mode
View Code

两台服务器均启动sentinel服务
/usr/local/redis/bin/redis-sentinel /usr/local/redis/etc/sentinel.conf

需要注意的地方,一旦启动sentinel服务后,服务会自动修改sentinel配置文件/usr/local/redis/etc/sentinel.conf :

技术分享
protected-mode no
port 26379
daemonize yes
logfile "/usr/local/redis/logs/sentinel.log"
dir "/usr/local/redis"
sentinel myid a7e5eaf6078540f6b3aee720ed8d3feecf49ee19
sentinel monitor mymaster 192.168.56.11 6379 2
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 2
sentinel leader-epoch mymaster 2
sentinel known-slave mymaster 192.168.56.12 6379
sentinel known-sentinel mymaster 192.168.56.12 26379 5133b8f7ec8b90b5ae29bf9d9bf47389c14a44fc
sentinel current-epoch 2
View Code


至此,sentinel部署完成。

接着,分别启动两台redis服务

Redis启停方式:

启动:

 /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf 

停止:

技术分享
/usr/local/redis/bin/redis-cli shutdown ###无密码停止


/usr/local/redis/bin/redis-cli -a 123456 -h 192.168.56.11 -p 6379  shutdown ###有密码停止
View Code

验证主从同步:

Redis连接方式:

/usr/local/redis/bin/redis-cli -h 192.168.56.11 -a 123456 -p 6379

master端登陆redis

技术分享
/usr/local/redis/bin/redis-cli -h 192.168.56.11  -a 123456 -p 6379
192.168.56.11:6379> info replication  #输出此命令查看master端状态
# Replication
role:master
connected_slaves:1  ##获得连接
slave0:ip=192.168.56.12,port=6379,state=online,offset=3309,lag=1  ###获得slave端信息。证明slave连接成功
master_repl_offset:3450
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:3449
View Code

若出现如上信息,则证明主从同步成功。

 

1.4、配置sentinel哨兵模式

两台服务器中均部署sentinel,两台环境均按一下步骤操作

redis解压文件夹中的sentinel.conf文件拷贝至redis配置文件夹中:

 cp -a /usr/local/redis-3.2.8/sentinel.conf /usr/local/redis/etc 

修改内容如下:

技术分享
port 26379                                      #sentinel监听端口
daemonize yes                                   #以daemon形式运行在后台
logfile /usr/local/redis/logs/sentinel.log                   #日志文件
dir /usr/local/redis
sentinel monitor mymaster 192.168.56.11 6379 2 #监控mymaster组,master地址,端口,quorum次数
sentinel down-after-milliseconds mymaster 5000  #5000毫秒即5秒连续不能连通master,认为master挂掉
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 60000        #故障切换超时时间
sentinel auth-pass mymaster 123456                #密码认证
protected-mode no                               #默认情况下sentinel只监听环回地址,这样就导致sentinel之间不能通信,可以改为监听网卡或者关闭protected-mode
View Code

两台服务器均启动sentinel服务

 /usr/local/redis/bin/redis-sentinel /usr/local/redis/etc/sentinel.conf 

#需要注意的地方,一旦启动sentinel服务后,服务会自动修改sentinel配置文件/usr/local/redis/etc/sentinel.conf 如下所示:

技术分享
protected-mode no
port 26379
daemonize yes
logfile "/usr/local/redis/logs/sentinel.log"
dir "/usr/local/redis"
sentinel myid a7e5eaf6078540f6b3aee720ed8d3feecf49ee19
sentinel monitor mymaster 192.168.56.11 6379 2
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 2
sentinel leader-epoch mymaster 2
sentinel known-slave mymaster 192.168.56.12 6379
sentinel known-sentinel mymaster 192.168.56.12 26379 5133b8f7ec8b90b5ae29bf9d9bf47389c14a44fc
sentinel current-epoch 2

 
View Code

具体用途请自行查看。

至此,sentinel部署完成。

Redis3.2.8+sentinel构建HA架构文档