redis一主二从加哨兵

Posted 叮伱格斐呃

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis一主二从加哨兵相关的知识,希望对你有一定的参考价值。

redis版本:redis-3.0.6.tar.gz

 

master:192.168.3.180

slave:192.168.3.184 (机器原因,两从都在这上面)

 

一.redis安装

cd /root/tools/
tar -zxvf redis-3.0.6.tar.gz
cd redis-3.0.6
make install PREFIX=/usr/local/redis 
ln -s /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli
cp utils/redis_init_script /etc/init.d/redis
mkdir /etc/redis
cp redis.conf /etc/redis/6379.conf
cp -rp sentinel.conf /etc/redis/sentinel_26379.conf

 

二.配置

主服务器上redis

vim 6379.conf
daemonize yes
pidfile /var/run/redis_6379.pid
port 6379
bind 0.0.0.0  (很重要,否则主从同步会失败)
logfile "/var/log/redis_6379.log"
dbfilename "dump_6379.rdb"
dir "/opt/redis/6379"
requirepass 123456 (设置redis密码) 

 

哨兵sentinel (本文只有一个哨兵,哨兵也可以部署多台机器,形成哨兵集群,避免单点的问题,保证系统的高可用。)

#修改或添加以下几项
vim sentinel_26379.conf
port 26379
daemonize yes
logfile "/tmp/sentinel.log"
dir "/opt/redis/redis_sentinel"
sentinel monitor mymaster 192.168.3.180 6379 1  
(最后的数字1指明当有多少个sentinel认为一个master失效时,master才算真正失效)
sentinel parallel-syncs mymaster 2 
(最后的数字2表示有多少个slave)

 

 

从服务器上

#从服务器配置可复制主服务器的,只需要修改相应端口和修改添加以下一行即可
slaveof 192.168.3.180 6379  (指定主redis)

 

 

测试:

分别启动master和两个slave

master上:

[[email protected] tmp]# redis-cli -p 6379
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.3.184,port=6391,state=online,offset=190442,lag=0
slave1:ip=192.168.3.184,port=6390,state=online,offset=190442,lag=0
master_repl_offset:190442
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:190441

 

slave上:

[[email protected] redis]# redis-cli -p 6390
127.0.0.1:6390> info replication
# Replication
role:slave
master_host:192.168.3.180
master_port:6379
master_link_status:up


[[email protected] redis]# redis-cli -p 6391
127.0.0.1:6391> info replication
# Replication
role:slave
master_host:192.168.3.180
master_port:6379
master_link_status:up

以上可看到已经完成主从同步,也可以去看看slave的启动日志,查看同步实时信息

[[email protected] redis]# tailf /var/log/redis_6390.log 
4706:S 28 Jan 13:05:29.985 * Connecting to MASTER 192.168.3.180:6390
4706:S 28 Jan 13:05:29.985 * MASTER <-> SLAVE sync started
4706:S 28 Jan 13:05:29.986 * Non blocking connect for SYNC fired the event.
4706:S 28 Jan 13:05:29.987 * Master replied to PING, replication can continue...
4706:S 28 Jan 13:05:29.987 * Partial resynchronization not possible (no cached master)
4706:S 28 Jan 13:05:29.989 * Full resync from master: 4da2c58d50928717d9a45216ced5c36a45a3b78c:29
4706:S 28 Jan 13:05:30.055 * MASTER <-> SLAVE sync: receiving 18 bytes from master
4706:S 28 Jan 13:05:30.055 * MASTER <-> SLAVE sync: Flushing old data
4706:S 28 Jan 13:05:30.055 * MASTER <-> SLAVE sync: Loading DB in memory
4706:S 28 Jan 13:05:30.055 * MASTER <-> SLAVE sync: Finished with success

 

 

三.哨兵启用

进入redis的安装目录可以看到

[[email protected] bin]# ls
dump.rdb  redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis-sentinel  redis-server
[[email protected] bin]# pwd
/usr/local/redis/bin

启动哨兵

./redis-sentinel /etc/redis/sentinel_26379.conf

 

以上是关于redis一主二从加哨兵的主要内容,如果未能解决你的问题,请参考以下文章

Redis哨兵模式(一主二从三哨兵)

安装redis-5.0.9版本,一主二从三哨兵集群

安装redis-5.0.9版本,一主二从三哨兵集群

Redis安装部署(一主二从三哨兵)

redis 的一主二从三哨兵模式

redis一主二从+三哨兵配置