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台)
yum install gcc* -y
2.下载redis安装(3台)
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
3.解压编译(3台)
tar zxvf redis-5.0.5.tar.gz -C /usr/local/ cd /usr/local/redis-5.0.5 make
4.创建redis 数据目录 日志目录(3台)
mkdir -p /opt/redis/{data,conf,redis-log,sentinel-log}
5.移动配置文件(3台)
cd /usr/local/redis-5.0.5/
cat redis.conf | grep -v ^#|grep -v ^$ >/opt/redis/conf/redis.conf
6.修改配置文件redis.conf(主)
#修改配置 bind 192.168.1.115 dir "/opt/redis/data" daemonize yes logfile "/opt/redis/redis-log/redis.log" #增加配置 requirepass "123.456.789" masterauth 123.456.789
修改配置文件redis.conf(从)
#修改配置 bind 192.168.1.114 dir "/opt/redis/data" daemonize yes logfile "/opt/redis/redis-log/redis.log" #增加配置 requirepass "123.456.789" masterauth 123.456.789 slaveof 192.168.1.115 6379
#修改配置 bind 192.168.1.116 dir "/opt/redis/data" daemonize yes logfile "/opt/redis/redis-log/redis.log" #增加配置 requirepass "123.456.789" masterauth 123.456.789 slaveof 192.168.1.115 6379
7.把启动文件放在/usr/local/bin方便启动(3台)
[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台)
redis-server /opt/redis/conf/redis.conf
9.测试
#查看集群是否正常
[root@redis-node1 ~]# redis-cli -h 192.168.1.115 -p 6379 -a 123.456.789 info Replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.114,port=6379,state=online,offset=906,lag=0
slave1:ip=192.168.1.116,port=6379,state=online,offset=906,lag=0
master_replid:4ac62473efb8c3767bbd2cd0e1754c4e8d2980d1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:906
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:906
[root@redis-node1 ~]# redis-cli -h 192.168.1.115 -p 6379 -a 123.456.789 Warning: 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.789 OK 192.168.1.115:6379> set k1 v1 OK 192.168.1.115:6379> exit [root@redis-node1 ~]# redis-cli -h 192.168.1.114 -p 6379 -a 123.456.789 Warning: 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 password 192.168.1.114:6379> AUTH 123.456.789 OK 192.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.789 Warning: 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.789 OK 192.168.1.116:6379> get k1 "v1"
四 安装哨兵 Sentinel
1.复制配置文件 拷贝哨兵启动文件到/usr/local/bin(3台)
cd /usr/local/redis-5.0.5/ cat sentinel.conf | grep -v ^#|grep -v ^$ >/opt/redis/conf/sentinel.conf cd src cp redis-sentinel /usr/local/bin
2.修改配置文件/opt/redis/conf/sentinel.conf(3台一致)
port 26379 daemonize yes pidfile /var/run/redis-sentinel.pid logfile "/opt/redis/sentinel-log/sentinel.log" dir /tmp sentinel monitor mymaster 192.168.1.115 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes sentinel auth-pass mymaster 123.456.789
3.启动 哨兵
redis-sentinel /opt/redis/conf/sentinel.conf
4.测试
[root@redis-node1 conf]# redis-cli -h 192.168.1.116 -p 6379 -a 123.456.789 info Replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:2 slave0:ip=192.168.1.115,port=6379,state=online,offset=30335,lag=1 slave1:ip=192.168.1.114,port=6379,state=online,offset=30335,lag=1 master_replid:db7b7b2d05b7bc2e770f5597e31bf9b7274b6add master_replid2:d8095aad044052ca59d4ecfc54a89911eb17e966 master_repl_offset:30617 second_repl_offset:11834 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:30617 kill 116上的redis服务 顺利切换到115上 [root@redis-node1 conf]# redis-cli -h 192.168.1.115 -p 6379 -a 123.456.789 info Replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:1 slave0:ip=192.168.1.114,port=6379,state=online,offset=0,lag=0 master_replid:68c6b9e0b0ca4f51a887adb82ecd6e1b36a2276e master_replid2:db7b7b2d05b7bc2e770f5597e31bf9b7274b6add master_repl_offset:42413 second_repl_offset:41827 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1448 repl_backlog_histlen:40966