CentOS 7 下 Redis 哨兵模式配置

Redis 5.0.x 安装

安装包准备
[root@localhost ~]# ll | grep redis
-rw-r--r--. 1 root root 1986574 Dec 17 15:29 redis-5.0.9.tar.gz
解压 && 分发
[root@localhost ~]# tar -zxf redis-5.0.9.tar.gz
[root@localhost ~]# mv redis-5.0.9 /usr/local/redis
安装
[root@localhost ~]# cd /usr/local/redis
[root@localhost redis]# make && make install

配置文件配置

相关目录创建
[root@localhost ~]# mkdir -p /usr/local/redis/conf/700{1,2,3}
[root@localhost ~]# mkdir -p /usr/local/redis/conf/2700{1,2,3}
配置文件配置
  • 主节点 – redis-7001.conf
[root@localhost ~]# cat > /usr/local/redis/conf/7001/redis-7001.conf << EEOOFF
bind 0.0.0.0                    # 绑定 IP
protected-mode no               # 被外部网络访问 IP,默认 yes
port 7001                       # 对应节点开放端口
daemonize yes                   # 设置 redis 通过后台启动
pidfile /usr/local/redis/conf/7001/redis_7001.pid       # pid 文件位置
loglevel notice                                         # log 级别
logfile /usr/local/redis/conf/7001/redis_7001.log       # log 文件位置
EEOOFF
  • 从节点 – redis-7002.conf
[root@localhost ~]# cat > /usr/local/redis/conf/7002/redis-7002.conf << EEOOFF
bind 0.0.0.0
protected-mode no
port 7002
daemonize yes
slaveof 192.168.8.145 7001          # 主节点
pidfile /usr/local/redis/conf/7002/redis_7002.pid
loglevel notice
logfile /usr/local/redis/conf/7002/redis_7002.log
EEOOFF
  • 从节点 – redis-7003.conf
[root@localhost ~]# cat > /usr/local/redis/conf/7003/redis-7003.conf << EEOOFF
bind 0.0.0.0
protected-mode no
port 7003
daemonize yes
slaveof 192.168.8.145 7001
pidfile /usr/local/redis/conf/7003/redis_7003.pid
loglevel notice
logfile /usr/local/redis/conf/7003/redis_7003.log
EEOOFF
  • 哨兵节点 – redis-27001.conf
[root@localhost ~]# cat > /usr/local/redis/conf/27001/redis-27001.conf << EEOOFF
port 27001
daemonize yes
sentinel monitor redis-master 192.168.8.145 7001 2
sentinel down-after-milliseconds redis-master 5000
sentinel failover-timeout redis-master 900000
sentinel parallel-syncs redis-master 1
# sentinel auth-pass redis-master 123456
logfile /usr/local/redis/conf/27001/redis_27001.log
EEOOFF
  • 哨兵节点 – redis-27002.conf
[root@localhost ~]# cat > /usr/local/redis/conf/27002/redis-27002.conf << EEOOFF
port 27002
daemonize yes
sentinel monitor redis-master 192.168.8.145 7001 2
sentinel down-after-milliseconds redis-master 5000
sentinel failover-timeout redis-master 900000
sentinel parallel-syncs redis-master 1
# sentinel auth-pass redis-master 123456
logfile /usr/local/redis/conf/27002/redis_27002.log
EEOOFF
  • 哨兵节点 – redis-27003.conf
[root@localhost ~]# cat > /usr/local/redis/conf/27001/redis-27001.conf << EEOOFF
port 27003
daemonize yes
sentinel monitor redis-master 192.168.8.145 7001 2
sentinel down-after-milliseconds redis-master 5000
sentinel failover-timeout redis-master 900000
sentinel parallel-syncs redis-master 1
# sentinel auth-pass redis-master 123456
logfile /usr/local/redis/conf/27003/redis_27003.log
EEOOFF

启动服务

redis 主节点
[root@localhost ~]# /usr/local/redis/src/redis-server /usr/local/redis/conf/7001/redis-7001.conf
redis 从节点
[root@localhost ~]# /usr/local/redis/src/redis-server /usr/local/redis/conf/7002/redis-7002.conf
[root@localhost ~]# /usr/local/redis/src/redis-server /usr/local/redis/conf/7003/redis-7003.conf
redis 哨兵节点
[root@localhost ~]# /usr/local/redis/src/redis-sentinel /usr/local/redis/conf/27001/sentinel-27001.conf
[root@localhost ~]# /usr/local/redis/src/redis-sentinel /usr/local/redis/conf/27002/sentinel-27002.conf
[root@localhost ~]# /usr/local/redis/src/redis-sentinel /usr/local/redis/conf/27003/sentinel-27003.conf

Redis 可用性验证

redis-cli 客户端验证
[root@localhost ~]# /usr/local/redis/src/redis-cli -h 192.168.8.145 -p 7001
192.168.8.145:7001> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.8.145,port=7002,state=online,offset=6078987,lag=0
slave1:ip=192.168.8.145,port=7003,state=online,offset=6078987,lag=0
master_replid:c0f9f5b1d368875069db097b0e606e80e9ce0aee
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:6079132
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:5030557
repl_backlog_histlen:1048576
Python 程序连接
  • 测试代码
[root@localhost ~]# cat connectRedis.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-


import redis
from redis.sentinel import Sentinel

# 连接哨兵服务器(主机名也可以用域名)
sentinel = Sentinel( [ ('192.168.8.145', 27001), ('192.168.8.145', 27002), ('192.168.8.145', 27003) ], socket_timeout=0.5)

master = sentinel.discover_master('redis-master')
print(master)

slave = sentinel.discover_slaves('redis-master')
print(slave)

master = sentinel.master_for('redis-master', socket_timeout=0.5, db=0)
w_ret = master.set('foo', 'bar')

slave = sentinel.slave_for('redis-master', socket_timeout=0.5, db=0)
r_ret = slave.get('foo')
print(r_ret)
  • 执行测试
[root@localhost ~]# python3 connectRedis.py
('192.168.8.145', 7001)
[('192.168.8.145', 7003), ('192.168.8.145', 7002)]
b'bar'

参考

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页