24.Redis2.8主从集群sentinel
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了24.Redis2.8主从集群sentinel相关的知识,希望对你有一定的参考价值。
0.集群架构(此处只说两种;本文2种,避免sentinel成为单节点)
第一种:
第二种:
1.下载redis2.8.x版本,2.8.x都是稳定版
redis-2.8.24.tar.gz
2.解压,安装
tar -zxvf redis-2.8.24.tar.gz -C /opt
make
make install
#编译的2进制文件在解压目录src下,为了方便把编译的二进制文件和配置文件复制到一起,不用每次找了
#搭建1 Master 2个Slave的分片集群(一台机器上)
mkdir -p /opt/redis-share1/{redis-6379,redis-6380,redis-6381} #创建多级目录
cd /opt/redis-2.8.24
cp runtest /opt/redis-share1/redis-6379
cp runtest-sentinel /opt/redis-share1/redis-6379
cp redis.conf /opt/redis-share1/redis-6379
cp sentinel.conf /opt/redis-share1/redis-6379
cd /opt/redis-2.8.24/src
cp redis-benchmark /opt/redis-share1/redis-6379
cp redis-check-aof /opt/redis-share1/redis-6379
cp redis-check-dump /opt/redis-share1/redis-6379
cp redis-cli /opt/redis-share1/redis-6379
cp redis-sentinel /opt/redis-share1/redis-6379
cp redis-server /opt/redis-share1/redis-6379
cd /opt/redis-share1/redis-6379/
cp * /opt/redis-share1/redis-6380
cp * /opt/redis-share1/redis-6381
3.修改配置文件
搜索slaveof,添加一项
slaveof 127.0.0.1 6379 (master不需要此项,slave指定主节点)
masterauth 123456 (不想设置密码不用)
requirepass 123456 (不想设置密码不用)
daemonize yes 修改为yes让其可以后台启动,不至于关闭控制台redis就关了
pidfile /opt/redis-share1/redis-6381/redis.pid 因为在同一台机器我们必须制定每个redis实例pid单独的位置
port 6379 #若干3个实例在3台服务器可以不用改,我这里在一台机器,所以分别6379 6380 6381
4.启动Sentinel Master Slave(红色是不用sentinel)
先启动Master 6379
cd /opt/redis-share1/redis-6379
./redis-server redis.conf
./redis-server redis.conf --sentinel #第二种方式,这种方式需要先修改sentinel.conf配置文件
再启动Slave1
cd /opt/redis-share1/redis-6380
./redis-server redis.conf
./redis-cli slaveof 172.29.1.102 6379 #把从节点告知主节点
./redis-server redis.conf --sentinel #第二种方式,这种方式需要先修改sentinel.conf配置文件
启动slave2
cd /opt/redis-share1/redis-6381
./redis-server redis.conf
./redis-cli slaveof 172.29.1.102 6379 #把从节点告知主节点
./redis-server redis.conf --sentinel #第二种方式,这种方式需要先修改sentinel.conf配置文件
#查看副本信息
./redis-cli -h 127.0.0.1 -p 6379 -a 123456 info replication
./redis-cli -h 127.0.0.1 -p 6380 -a 123456 info replication
./redis-cli -h 127.0.0.1 -p 6381 -a 123456 info replication
如果是第一种方式:
再启动Sentinel服务器:
cd /opt/redis-Sentinel/redis
./redis-server sentinel.conf --sentinel &
#不带sentinel启动方式查看验证主从复制
./redis-server redis.conf
#查看节点信息带密码
主:
./redis-cli -h 127.0.0.1 -p 6379 -a 123456 info replication
从:
./redis-cli -h 127.0.0.1 -p 6380 -a 123456 info replication
./redis-cli -h 127.0.0.1 -p 6381 -a 123456 info replication
#的是主从复制
./redis-cli -h 127.0.0.1 -p 6379 -a 123456
>set aa 123
>get aa
./redis-cli -h 127.0.0.1 -p 6380 -a 123456
>get aa
./redis-cli -h 127.0.0.1 -p 6381 -a 123456
>get aa
#Sentinel mode:
kill -9 $(ps -ef|grep -E \'redis\'|grep -v grep|awk \'{print $2}\') #杀掉所有redis,已sentinel启动
#分别进入3个redis
./redis-server sentinel.conf --sentinel
./redis-server sentinel.conf --sentinel
./redis-server sentinel.conf --sentinel
#测试Master挂了以后是否slave升级为Master
./redis-sentinel sentinel.conf #监控redis节点情况
#Master和Slave配置
#master
port 26379
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 900000
sentinel can-failover mymaster yes
sentinel parallel-syncs mymaster 1
#slaeve
port 26479
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 900000
sentinel can-failover mymaster yes
sentinel parallel-syncs mymaster 1
#slave
port 26579
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel failover-timeout mymaster 900000
sentinel can-failover mymaster yes
sentinel parallel-syncs mymaster 1
参考:http://blog.csdn.net/luyee2010/article/details/9385155
参考:http://www.linuxidc.com/Linux/2013-11/92287.htm
以上是关于24.Redis2.8主从集群sentinel的主要内容,如果未能解决你的问题,请参考以下文章