redis主从+sentinel主从自动切换

redis主从+sentinel主从自动切换

版本:redis-2.8.19.tar.gz
说明:redis3.x默认不设置密码验证会启动保护模式

架构:2个节点

节点1: 192.168.12.101/24
节点2: 192.168.12.102/24

节点1上部署redis实例,角色master,部署sentinel实例,监控redis-master节点
节点2上部署redis实例,角色slave,部署sentinel实例,监控redis-master节点

+------+           +------+
| [M1] |----//-----| [S1] |
| <S1> |           | <S2> |
+------+           +------+

[M1]  redis master
[S1]  redis slave
<S1>  sentinel 1
<S2>  sentinel 2

一.配置redis主从

节点1:redis-master配置
1.编译安装redis
tar zxvf redis-2.8.19.tar.gz
cd redis-2.8.19
make
2.更改redis.conf配置文件
daemonize no 改为 yes #后台运行

节点2:redis-slave配置
1.步骤同节点1,不同点:更改redis.conf配置文件
slaveof 192.168.12.101 6379 #当配置sentinel后,slave->master时,这行代码会被注释

启动redis-master
# src/redis-server -p 6380 redis.conf -h 192.168.12.101

启动redis-slave
# src/redis-server -p 6380 redis.conf -h 192.168.12.102


查看redis运行信息,端口
# src/redis-cli  -h 192.168.12.101 -p 6379 info replication
# src/redis-cli  -h 192.168.12.102 -p 6379 info replication

测试主从复制
 主redis
# src/redis-cli -p 6380 -h 192.168.12.101
192.168.12.101:6379> set name 123
OK
192.168.12.101:6379> get name
"123"
 从redis
# src/redis-cli -p 6380 -h 192.168.12.102
192.168.12.102:6379> get name
"123"

二.配置redis+sentinel主从自从切换




1.配置sentinel.conf配置文件,配置sentinel

2个sentinel实例配置相同mastername master1
[root@localhost redis-2.8.19]# cat sentinel.conf
port 26379

#master1
sentinel monitor master1 192.168.12.101 6379 1
sentinel down-after-milliseconds master1 5000
sentinel failover-timeout master1 15000
sentinel parallel-syncs master1 1

启动redis-master上的sentinel
# src/redis-sentinel sentinel.conf -h 192.168.12.101

启动redis-slave上的sentinel
# src/redis-sentinel sentinel.conf -h 192.168.12.102

--说明:sentinel启动后会对redis.conf和sentinel.conf文件更改

查看sentinel运行信息,端口不可省略
# src/redis-cli  -h 192.168.12.101 -p 26379 info sentinel
# src/redis-cli  -h 192.168.12.102 -p 26379 info sentinel

验证sentinel切换主从
# src/redis-cli  -h 192.168.12.101 shutdown
# src/redis-cli  -h 192.168.12.101 -p 26379 info
# src/redis-cli  -h 192.168.12.102 -p 26379 info

Jedis客户端测试


使用sentinel做HA,Jedis版本必须2.2.2及以上,所有访问Redis实例的连接都必须从连接池中获取

=====================  redis密码验证配置  ==================================

 1.命令启动,临时,不需要重启服务
 redis-master
# src/redis-cli -p 6380 -h 127.0.0.1
127.0.0.1:6379> CONFIG set requirepass mypass
OK
127.0.0.1:6379> auth mypass
OK
 redis-slave
# src/redis-cli -p 6380 -h 127.0.0.1
127.0.0.1:6379> CONFIG set masterauth mypass
OK
127.0.0.1:6379> auth mypass
OK
CONFIG SET masterauth mypass


sentinel密码验证配置
cat sentinel.conf
sentinel auth-pass master1 mypass
 2.更改配置文件,永久,重启服务
redis-master
cat redis.conf
requirepass "123456"
masterauth "123456"
redis-slave
cat redis.conf
requirepass "123456"
masterauth "123456"


sentinel密码验证配置
cat sentinel.conf
sentinel auth-pass master1 mypass

=======================  redis其他操作  ================================

# src/redis-cli -p 6380 -h 127.0.0.1
127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> save
OK

猜你喜欢

转载自blog.csdn.net/u010181847/article/details/51489278