redis5.0 哨兵
redis5.0 一主二从三哨兵
一 redis:是完全开源免费的,遵守BSD协议,是一个高可用的key-value数据库
有3个特点
1.支持数据持久化,可以将内存中的数据保存在磁盘中,重启后可以再次加载
2.不仅仅支持简单的key-value类型的数据,同时还提供list set zset hash等数据结构的存储
3.支持数据库备份,即master-slave模式的数据库备份。
二 环境介绍 CentOS Linux release 7.6.1810 (Core)
192.168.1.115 主+Sentinel redis端口:6379 Sentinel端口:26379
192.168.1.114 从+Sentinel redis端口:6379 Sentinel端口:26379
192.168.1.116 从+Sentinel redis端口:6379 Sentinel端口:26379
三 安装redis服务
1.安装依赖(3台)
|
1
|
yum install gcc* -y |
2.下载redis安装(3台)
|
1
|
wget http://download.redis.io/releases/redis-5.0.5.tar.gz |
3.解压编译(3台)
|
1
2
3
|
tar zxvf redis-5.0.5.tar.gz -C /usr/local/cd /usr/local/redis-5.0.5make |
4.创建redis 数据目录 日志目录(3台)
|
1
|
mkdir -p /opt/redis/{data,conf,redis-log,sentinel-log} |
5.移动配置文件(3台)
|
1
|
cd /usr/local/redis-5.0.5/<br>cat redis.conf | grep -v ^#|grep -v ^$ >/opt/redis/conf/redis.conf |
6.修改配置文件redis.conf(主)
|
1
2
3
4
5
6
7
8
|
#修改配置bind 192.168.1.115dir "/opt/redis/data"daemonize yeslogfile "/opt/redis/redis-log/redis.log"#增加配置requirepass "123.456.789"masterauth 123.456.789 |
修改配置文件redis.conf(从)
|
1
2
3
4
5
6
7
8
9
|
#修改配置bind 192.168.1.114dir "/opt/redis/data"daemonize yeslogfile "/opt/redis/redis-log/redis.log"#增加配置requirepass "123.456.789"masterauth 123.456.789slaveof 192.168.1.115 6379 |
|
1
2
3
4
5
6
7
8
9
|
#修改配置bind 192.168.1.116dir "/opt/redis/data"daemonize yeslogfile "/opt/redis/redis-log/redis.log"#增加配置requirepass "123.456.789"masterauth 123.456.789slaveof 192.168.1.115 6379 |
7.把启动文件放在/usr/local/bin方便启动(3台)
|
1
2
|
[root@redis-node3 opt]# cp /usr/local/redis-5.0.5/src/redis-server /usr/local/bin/[root@redis-node3 opt]# cp /usr/local/redis-5.0.5/src/redis-cli /usr/local/bin/ |
8.启动(3台)
|
1
|
redis-server /opt/redis/conf/redis.conf |
9.测试
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#查看集群是否正常<br>[root@redis-node1 ~]# redis-cli -h 192.168.1.115 -p 6379 -a 123.456.789 info Replication<br>Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.<br># Replication<br>role:master<br>connected_slaves:2<br>slave0:ip=192.168.1.114,port=6379,state=online,offset=906,lag=0<br>slave1:ip=192.168.1.116,port=6379,state=online,offset=906,lag=0<br>master_replid:4ac62473efb8c3767bbd2cd0e1754c4e8d2980d1<br>master_replid2:0000000000000000000000000000000000000000<br>master_repl_offset:906<br>second_repl_offset:-1<br>repl_backlog_active:1<br>repl_backlog_size:1048576<br>repl_backlog_first_byte_offset:1<br>repl_backlog_histlen:906<br><br>[root@redis-node1 ~]# redis-cli -h 192.168.1.115 -p 6379 -a 123.456.789Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.1.115:6379> AUTH 123.456.789OK192.168.1.115:6379> set k1 v1OK192.168.1.115:6379> exit[root@redis-node1 ~]# redis-cli -h 192.168.1.114 -p 6379 -a 123.456.789Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.1.114:6379> AUTH 123,456,789(error) ERR invalid password192.168.1.114:6379> AUTH 123.456.789OK192.168.1.114:6379> get k1"v1"192.168.1.114:6379> exit[root@redis-node1 ~]# redis-cli -h 192.168.1.116 -p 6379 -a 123.456.789Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.1.116:6379> AUTH 123.456.789OK192.168.1.116:6379> get k1"v1" |
四 安装哨兵 Sentinel
1.复制配置文件 拷贝哨兵启动文件到/usr/local/bin(3台)
|
1
2
3
4
|
cd /usr/local/redis-5.0.5/cat sentinel.conf | grep -v ^#|grep -v ^$ >/opt/redis/conf/sentinel.confcd srccp redis-sentinel /usr/local/bin |
2.修改配置文件/opt/redis/conf/sentinel.conf(3台一致)
|
1
2
3
4
5
6
7
8
9
10
11
|
port 26379daemonize yespidfile /var/run/redis-sentinel.pidlogfile "/opt/redis/sentinel-log/sentinel.log"dir /tmpsentinel monitor mymaster 192.168.1.115 6379 2sentinel down-after-milliseconds mymaster 30000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 180000sentinel deny-scripts-reconfig yessentinel auth-pass mymaster 123.456.789 |
3.启动 哨兵
|
1
|
redis-sentinel /opt/redis/conf/sentinel.conf |
4.测试
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
[root@redis-node1 conf]# redis-cli -h 192.168.1.116 -p 6379 -a 123.456.789 info ReplicationWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# Replicationrole:masterconnected_slaves:2slave0:ip=192.168.1.115,port=6379,state=online,offset=30335,lag=1slave1:ip=192.168.1.114,port=6379,state=online,offset=30335,lag=1master_replid:db7b7b2d05b7bc2e770f5597e31bf9b7274b6addmaster_replid2:d8095aad044052ca59d4ecfc54a89911eb17e966master_repl_offset:30617second_repl_offset:11834repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:30617kill 116上的redis服务 顺利切换到115上[root@redis-node1 conf]# redis-cli -h 192.168.1.115 -p 6379 -a 123.456.789 info ReplicationWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# Replicationrole:masterconnected_slaves:1slave0:ip=192.168.1.114,port=6379,state=online,offset=0,lag=0master_replid:68c6b9e0b0ca4f51a887adb82ecd6e1b36a2276emaster_replid2:db7b7b2d05b7bc2e770f5597e31bf9b7274b6addmaster_repl_offset:42413second_repl_offset:41827repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1448repl_backlog_histlen:40966 |


浙公网安备 33010602011771号