Redis哨兵部署安装

三个实验主机

db01:10.0.0.51
db02:10.0.0.52
db03:10.0.0.53

 

1.快速安装3个redis节点

rsync -avz 10.0.0.51:/opt/* /opt/
mkdir /data/redis_6379 -p
cd /opt/redis 
make install 
sed -i 's#51#53#g' /opt/redis_6379/conf/redis_6379.conf
rm -rf /data/redis_6379/* 
redis-server /opt/redis_6379/conf/redis_6379.conf
redis-cli

 

2.配置3个redis配置文件

db01的单节点配置:

cat >/opt/redis_6379/conf/redis_6379.conf <<EOF
daemonize yes
bind 10.0.0.51 127.0.0.1
port 6379
pidfile /opt/redis_6379/pid/redis_6379.pid
logfile /opt/redis_6379/logs/redis_6379.log
databases 16
dir /data/redis_6379
dbfilename redis_6379.rdb
requirepass "123456"
masterauth "123456"
EOF

 

db02的单节点配置:

cat >/opt/redis_6379/conf/redis_6379.conf <<EOF
daemonize yes
bind 10.0.0.52 127.0.0.1
port 6379
pidfile /opt/redis_6379/pid/redis_6379.pid
logfile /opt/redis_6379/logs/redis_6379.log
databases 16
dir /data/redis_6379
dbfilename redis_6379.rdb
requirepass "123456"
masterauth "123456"
EOF

 

db03的单节点配置:

cat >/opt/redis_6379/conf/redis_6379.conf <<EOF
daemonize yes
bind 10.0.0.53 127.0.0.1
port 6379
pidfile /opt/redis_6379/pid/redis_6379.pid
logfile /opt/redis_6379/logs/redis_6379.log
databases 16
dir /data/redis_6379
dbfilename redis_6379.rdb
requirepass "123456"
masterauth "123456"
EOF

 

2.安装部署3个哨兵节点

===========================
!!!!注意!!!!
三个节点的bind IP修改为自己的IP地址
===========================
mkdir -p /data/redis_26379
mkdir -p /opt/redis_26379/{conf,pid,logs}

 

3.配置哨兵的配置文件

db01的配置:

cat >/opt/redis_26379/conf/redis_26379.conf << EOF
bind 10.0.0.51
port 26379
daemonize yes
logfile /opt/redis_26379/logs/redis_26379.log
dir /data/redis_26379
sentinel monitor mymaster 10.0.0.51 6379 2 
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 18000
sentinel auth-pass mymaster 123456
EOF

 

db02的配置:

cat >/opt/redis_26379/conf/redis_26379.conf << EOF
bind 10.0.0.52
port 26379
daemonize yes
logfile /opt/redis_26379/logs/redis_26379.log
dir /data/redis_26379
sentinel monitor mymaster 10.0.0.51 6379 2 
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 18000
sentinel auth-pass mymaster 123456
EOF

 

db03的配置:

cat >/opt/redis_26379/conf/redis_26379.conf << EOF
bind 10.0.0.53
port 26379
daemonize yes
logfile /opt/redis_26379/logs/redis_26379.log
dir /data/redis_26379
sentinel monitor mymaster 10.0.0.51 6379 2 
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 18000
sentinel auth-pass mymaster 123456
EOF

 

4.重启所有的单节点(db01,db02,db03)

pkill redis
redis-server /opt/redis_6379/conf/redis_6379.conf

 

5.配置主从复制(db01)

redis-cli -h db02 slaveof 10.0.0.51 6379
redis-cli -h db03 slaveof 10.0.0.51 6379

 

6.启动哨兵(db01,db02,db03)

redis-sentinel /opt/redis_26379/conf/redis_26379.conf

 

7.验证主节点(db01)

redis-cli -h db01 -p 26379 Sentinel get-master-addr-by-name mymaster
redis-cli -h db02 -p 26379 Sentinel get-master-addr-by-name mymaster
redis-cli -h db03 -p 26379 Sentinel get-master-addr-by-name mymaster

出现如下图表示主节点开启成功

Redis哨兵部署安装

 

8.模拟故障转移

– 关闭主节点服务上的所有redis进程

Redis哨兵部署安装

 

– 观察其他2个节点会不会发生选举

Redis哨兵部署安装
– 查看配置文件里会不会自动更新

Redis哨兵部署安装
– 查看新的主节点能不能写入

Redis哨兵部署安装
– 查看从节点能否正常同步

Redis哨兵部署安装

 

9.模拟故障修复上线

– 启动db01单节点并启动db01哨兵

Redis哨兵部署安装

10.模拟权重选举

– 设置其他节点的权重为0

[root@db01 ~]#redis-cli -h db02 -p 6379 CONFIG SET slave-priority 0
OK
[root@db01 ~]#redis-cli -h db03 -p 6379 CONFIG SET slave-priority 0
OK

– 手动发起重新选举并且验证

重新选举(主动切换)
[root@db01 ~]#redis-cli -h db01 -p 26379 sentinel failover mymaster
OK
[root@db01 ~]#

验证
[root@db01 ~]#redis-cli -h db01 -p 26379 Sentinel get-master-addr-by-name mymaster
1) "10.0.0.51"
2) "6379"

– 观察所有节点消息是否同步

在主节点db01插入一个新的键值
[root@db01 ~]#redis-cli
127.0.0.1:6379> set k2 ceshi
OK
127.0.0.1:6379> 

在从节点db02查看能否获取
[root@db02 /opt/redis_6379/conf]#redis-cli
127.0.0.1:6379> get k2
"ceshi"
127.0.0.1:6379>

 

– 还原db02,db03从库的权重

[root@db01 ~]#redis-cli -h db02 -p 6379 CONFIG SET slave-priority 100
OK
[root@db01 ~]#redis-cli -h db03 -p 6379 CONFIG SET slave-priority 100
OK
[root@db01 ~]#

 

命令解释:

1.查询节点权重命令:CONFIG GET slave-priority
2.设置节点权重命令:CONFIG SET slave-priority 0
3.主动切换为主节点:sentinel failover mymaster

 

操作命令:

redis-cli -h db02 -p 6379 CONFIG SET slave-priority 0
redis-cli -h db03 -p 6379 CONFIG SET slave-priority 0
redis-cli -h db01 -p 26379 sentinel failover mymaster

 

赞赏

微信赞赏支付宝赞赏

Redis

Redis设置用户认证

2019-12-2 7:35:11

Redis

Redis主从复制

2019-12-2 7:43:31

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索