工具之《redis部署哨兵(sentinel)模式》

一、redis哨兵模式
1、哨兵是redis提供的独立进程,用以监控主服务器是否正常。如果检测到主服务器故障,哨兵会从从服务器中投票选举出新的主服务器,实现自动的故障切换

2、采用一主两从三哨兵模式
系统为CentOS7
master 192.168.100.1
slave1 192.168.100.2
slave2 192.168.100.3
sentinel 192.168.100.1
sentinel 192.168.100.2
sentinel 192.168.100.3

二、安装
1、安装依赖

yum install -y make gcc gcc-c++ tcl

2、建立目录&编译安装

mkdir -p /appserver/software
cd /appserver/software
wget http://download.redis.io/releases/redis-stable.tar.gz
tar -zxvf redis-stable.tar.gz
cd redis-stable
make
make install

3、确认安装文件
默认安装路径为:/usr/local/bin
说明
redis-benchmark:redis自带的压力测试工具
redis-check-aof:aof持久化文件恢复程序
redis-check-rdb:rdb持久化文件恢复程序
redis-cli:redis命令行客户端
redis-sentinel -> redis-server:redis软链接指向redis-server
redis-server:redis服务器

4、验证redis服务
1)以缺省配置启动redis-server

[root@master bin]# ./redis-server
12644:C 31 Jan 2020 13:27:34.635 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12644:C 31 Jan 2020 13:27:34.635 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=12644, just started
12644:C 31 Jan 2020 13:27:34.635 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
12644:M 31 Jan 2020 13:27:34.636 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 12644
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

12644:M 31 Jan 2020 13:27:34.647 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
12644:M 31 Jan 2020 13:27:34.647 # Server initialized
12644:M 31 Jan 2020 13:27:34.648 * Ready to accept connections

2)新开一个终端窗口运行redis-cli

[root@master bin]# ./redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> incr mycounter
(integer) 1
127.0.0.1:6379> incr mycounter
(integer) 2
127.0.0.1:6379> exit

5、处理警告信息

echo "net.core.somaxconn = 1024" >> /usr/lib/sysctl.d/50-default.conf

使设置生效:

sysctl -p

6、停止redis命令

redis-cli shutdown

 三、配置主从服务
1、创建目录
存放配置文件、日志、数据

mkdir -p /appserver/redis/conf
mkdir -p /appserver/redis/log
mkdir -p /appserver/redis/data

2、配置master
1)添加配置文件

cp /appserver/software/redis-stable/redis.conf /appserver/redis/conf/redis_10079.conf

2)修改绑定IP

bind 192.168.100.1

3)修改端口

port 10079   #redis默认端口必须改掉,否则会有安全隐患

4)修改以守护进程方式启动

daemonize yes

5)设置pid文件路径

pidfile /appserver/redis/conf/redis_10079.pid

6)设置log文件路径

logfile /appserver/redis/log/redis_10079.log

7)本地数据库文件名

dbfilename dump_10079.rdb

8)本地数据库存放目录

dir /appserver/redis/data/

9)slave连接master的密码

masterauth 123_12345678^_00

10)redis连接密码

requirepass 123_12345678^_00

11)redis使用最大内存数

maxmemory 128m

12)达到最大内存后的淘汰策略

maxmemory-policy allkeys-lru   #从内存数据集中挑选最近最少使用的数据淘汰

13)每次更新操作后是否进行日志记录

appendonly yes

14)更新日志文件名

appendfilename "appendonly_10079.aof"

2、配置slave1
复制master的配置文件redis_10079.conf放在/appserver/redis/conf/路径下
1)修改绑定IP

bind 192.168.100.2

2)修改复制master的IP和PORT

replicaof 192.168.100.1 10079

3、配置slave2
复制master的配置文件redis_10079.conf放在/appserver/redis/conf/路径下
1)修改绑定IP

bind 192.168.100.3

2)修改复制master的IP和PORT

replicaof 192.168.100.1 10079

4、启动服务,切记先主后从

cd /usr/local/bin
./redis-server /appserver/redis/conf/redis_10079.conf

5、验证服务
1)master服务器上查看信息

redis-cli -h 192.168.100.1 -p 10079 -a 123_12345678^_00
info

找到:

# Replication
role:master
connected_slaves:2
slave0:ip=192.168.100.2,port=10079,state=online,offset=448,lag=1
slave1:ip=192.168.100.3,port=10079,state=online,offset=448,lag=1
master_replid:ca27305624ce1b656134410064c27dde7e8c8073
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:448
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:448

2)slave服务器上查看信息
slave1执行:

redis-cli -h 192.168.100.2 -p 10079 -a 123_12345678^_00
info

找到:

# Replication
role:slave
master_host:192.168.100.1
master_port:10079
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:742
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:ca27305624ce1b656134410064c27dde7e8c8073
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:742
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:742

slave2执行:

redis-cli -h 192.168.100.3 -p 10079 -a 123_12345678^_00
info

找到:

# Replication
role:slave
master_host:192.168.100.1
master_port:10079
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:924
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:ca27305624ce1b656134410064c27dde7e8c8073
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:924
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:29
repl_backlog_histlen:896

6、验证数据同步
1)master服务器上执行

192.168.100.1:10079> set test111 "sample111"
OK
192.168.100.1:10079> set test222 "sample222"
OK
192.168.100.1:10079> keys *
1) "test222"
2) "test111"
192.168.100.1:10079>

2)slave服务器上执行

192.168.100.2:10079> keys *
1) "test222"
2) "test111"
192.168.100.2:10079> set test333 "sample333"
(error) READONLY You can't write against a read only replica.
192.168.100.2:10079>

四、Sentinel配置
1、配置master服务器的sentinel
1)添加配置文件

cp /appserver/software/redis-stable/sentinel.conf /appserver/redis/conf/sentinel_20079.conf

2)修改绑定IP

bind 192.168.100.1

3)设置保护模式

protected-mode yes

4)修改端口

port 20079

5)修改以守护进程方式启动

daemonize yes

6)设置pid文件路径

pidfile /appserver/redis/conf/redis-sentinel_20079.pid

7)设置log文件路径

logfile /appserver/redis/log/redis-sentinel_20079.log

8)设置工作目录

dir /appserver/redis

9)设置sentinel去监听master信息

sentinel monitor 001_master 192.168.100.1 10079 2

10)设置连接master的密码

sentinel auth-pass 001_master 123_12345678^_00

11)master存活超时时间

sentinel down-after-milliseconds 001_master 30000

12)设置主备切换时的方式
当主节点发生变化的时候,其余的从节点是如何复制主节点的数据,1是一个个来复制,0是同时进行复制,一个个来会减少主库的压力

sentinel parallel-syncs 001_master 1

13)failover超时时间
当failover开始后,在此时间内仍然没有触发任何failover操作,当前sentinel将会认为此次failoer失败

sentinel failover-timeout 001_master 30000

2、配置slave1服务器的sentinel
复制master服务器的配置文件sentinel_20079.conf放在/appserver/redis/conf/路径下
1)修改绑定IP

bind 192.168.100.2

3、配置slave2服务器的sentinel
复制master服务器的配置文件sentinel_20079.conf放在/appserver/redis/conf/路径下
1)修改绑定IP

bind 192.168.100.3

4、启动服务

cd /usr/local/bin
./redis-sentinel /appserver/redis/conf/sentinel_20079.conf

5、查看启动日志
1)Sentinel1(192.168.100.1)

13307:X 01 Feb 2020 06:48:46.659 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
13307:X 01 Feb 2020 06:48:46.659 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=13307, just started
13307:X 01 Feb 2020 06:48:46.659 # Configuration loaded
13308:X 01 Feb 2020 06:48:46.661 * Increased maximum number of open files to 10032 (it was originally set to 1024).
13308:X 01 Feb 2020 06:48:46.785 * Running mode=sentinel, port=20079.
13308:X 01 Feb 2020 06:48:46.786 # Sentinel ID is baee1ee2dee67b30793e3de9fb0cc902c73b9a75
13308:X 01 Feb 2020 06:48:46.786 # +monitor master 001_master 192.168.100.1 10079 quorum 2
13308:X 01 Feb 2020 06:48:46.787 * +slave slave 192.168.100.2:10079 192.168.100.2 10079 @ 001_master 192.168.100.1 10079
13308:X 01 Feb 2020 06:48:46.787 * +slave slave 192.168.100.3:10079 192.168.100.3 10079 @ 001_master 192.168.100.1 10079
13308:X 01 Feb 2020 06:48:49.765 * +sentinel sentinel f460e106f22d8f29f3d258f1ae4c054742ff745d 192.168.100.2 20079 @ 001_master 192.168.100.1 10079
13308:X 01 Feb 2020 06:48:56.249 * +sentinel sentinel 62592c23dcbff3a3c14a4e9921eee8400411db1d 192.168.100.3 20079 @ 001_master 192.168.100.1 10079

2)Sentinel2(192.168.100.2)

12206:X 01 Feb 2020 06:48:47.716 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12206:X 01 Feb 2020 06:48:47.716 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=12206, just started
12206:X 01 Feb 2020 06:48:47.716 # Configuration loaded
12207:X 01 Feb 2020 06:48:47.719 * Increased maximum number of open files to 10032 (it was originally set to 1024).
12207:X 01 Feb 2020 06:48:47.784 * Running mode=sentinel, port=20079.
12207:X 01 Feb 2020 06:48:47.787 # Sentinel ID is f460e106f22d8f29f3d258f1ae4c054742ff745d
12207:X 01 Feb 2020 06:48:47.787 # +monitor master 001_master 192.168.100.1 10079 quorum 2
12207:X 01 Feb 2020 06:48:47.789 * +slave slave 192.168.100.2:10079 192.168.100.2 10079 @ 001_master 192.168.100.1 10079
12207:X 01 Feb 2020 06:48:47.790 * +slave slave 192.168.100.3:10079 192.168.100.3 10079 @ 001_master 192.168.100.1 10079
12207:X 01 Feb 2020 06:48:48.734 * +sentinel sentinel baee1ee2dee67b30793e3de9fb0cc902c73b9a75 192.168.100.1 20079 @ 001_master 192.168.100.1 10079
12207:X 01 Feb 2020 06:48:56.250 * +sentinel sentinel 62592c23dcbff3a3c14a4e9921eee8400411db1d 192.168.100.3 20079 @ 001_master 192.168.100.1 10079

3)Sentinel3(192.168.100.3)

12387:X 01 Feb 2020 06:48:53.616 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12387:X 01 Feb 2020 06:48:53.616 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=12387, just started
12387:X 01 Feb 2020 06:48:53.616 # Configuration loaded
12388:X 01 Feb 2020 06:48:53.619 * Increased maximum number of open files to 10032 (it was originally set to 1024).
12388:X 01 Feb 2020 06:48:54.336 * Running mode=sentinel, port=20079.
12388:X 01 Feb 2020 06:48:56.247 # Sentinel ID is 62592c23dcbff3a3c14a4e9921eee8400411db1d
12388:X 01 Feb 2020 06:48:56.247 # +monitor master 001_master 192.168.100.1 10079 quorum 2
12388:X 01 Feb 2020 06:48:56.247 # +tilt #tilt mode entered
12388:X 01 Feb 2020 06:48:56.249 * +slave slave 192.168.100.2:10079 192.168.100.2 10079 @ 001_master 192.168.100.1 10079
12388:X 01 Feb 2020 06:48:57.534 * +slave slave 192.168.100.3:10079 192.168.100.3 10079 @ 001_master 192.168.100.1 10079
12388:X 01 Feb 2020 06:48:58.208 * +sentinel sentinel baee1ee2dee67b30793e3de9fb0cc902c73b9a75 192.168.100.1 20079 @ 001_master 192.168.100.1 10079
12388:X 01 Feb 2020 06:48:58.773 * +sentinel sentinel f460e106f22d8f29f3d258f1ae4c054742ff745d 192.168.100.2 20079 @ 001_master 192.168.100.1 10079

6、验证配置

[root@master log]# redis-cli -h 192.168.100.1 -p 20079 -a 123_12345678^_00
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.100.1:20079> sentinel get-master-addr-by-name 001_master
1) "192.168.100.1"
2) "10079"
192.168.100.1:20079> sentinel slaves 001_master
1)  1) "name"
    2) "192.168.100.2:10079"
    3) "ip"
    4) "192.168.100.2"
    5) "port"
    6) "10079"
    7) "runid"
    8) "860f0bd57d582d1d0e7f509001d87db7ce459970"
    9) "flags"
   10) "slave"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "139"
   19) "last-ping-reply"
   20) "139"
   21) "down-after-milliseconds"
   22) "30000"
   23) "info-refresh"
   24) "8802"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "1506924"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "192.168.100.1"
   35) "master-port"
   36) "10079"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "397604"
2)  1) "name"
    2) "192.168.100.3:10079"
    3) "ip"
    4) "192.168.100.3"
    5) "port"
    6) "10079"
    7) "runid"
    8) "d8af8f78204e2ba7daed5a06c8f0ed50fc81a4f4"
    9) "flags"
   10) "slave"
   11) "link-pending-commands"
   12) "0"
   13) "link-refcount"
   14) "1"
   15) "last-ping-sent"
   16) "0"
   17) "last-ok-ping-reply"
   18) "139"
   19) "last-ping-reply"
   20) "139"
   21) "down-after-milliseconds"
   22) "30000"
   23) "info-refresh"
   24) "1022"
   25) "role-reported"
   26) "slave"
   27) "role-reported-time"
   28) "1506924"
   29) "master-link-down-time"
   30) "0"
   31) "master-link-status"
   32) "ok"
   33) "master-host"
   34) "192.168.100.1"
   35) "master-port"
   36) "10079"
   37) "slave-priority"
   38) "100"
   39) "slave-repl-offset"
   40) "399231"
192.168.100.1:20079>

五、停止命令
1、停止redis服务(填入IP、端口、密码)

redis-cli -h 192.168.100.1 -p 10079 -a 123_12345678^_00 shutdown

2、停止sentinel(命令类似,端口不同)

redis-cli -h 192.168.100.1 -p 20079 -a 123_12345678^_00 shutdown

六、故障切换
1、故障切换时,redis会自动在配置文件中加入修改,可以搜索“Generated by CONFIG REWRITE”

参考资料:
Redis哨兵(Sentinel)模式
https://www.jianshu.com/p/06ab9daf921d
Redis 持久化之RDB和AOF详解
https://www.cnblogs.com/wangjian941118/p/11294762.html
Redis及其Sentinel配置项详细说明
https://blog.csdn.net/a1282379904/article/details/52335051
Redis使用
https://www.cnblogs.com/qingfengzhuimeng/p/7712778.html
深入剖析Redis系列(二) - Redis哨兵模式与高可用集群
https://www.jianshu.com/p/0f58475c6918
Redis 哨兵模式实现主从故障互切换
https://www.cnblogs.com/chenmh/p/5578376.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值