一.案例分析
- sentinel模式是基于master-slave模式发展的,但是master-slave不够故障自动切换。
- 所以redis官方自3.0的版本加入sentinel架构,已解决故障自动切换问题
二.环境部署
角色 | IP地址 | 软件包 |
redis-master、redis-sentinel | 192.168.43.101/24 | redis-4.0.14.tar.gz |
redis-slave、redis-sentinel | 192.168.43.102/24 | |
redis-slave、redis-sentinel | 192.168.43.103/24 |
- 为了节省资源,这里把sentinel和redis-server安装在一起,但是生产环境下,sentinel是单独存在的
- snetinul是redis系统下的一个的独立进程,所以不需要单独的数据包
- 由于sentinel是基于master-slave模式之上的,所以我们先搭建master-slave
三.集群搭建
在三台虚拟机上都安装redis,步骤相同
- 下载redis安装包
[root@localhost ~]# wget http://download.redis.io/releases/ redis-4.0.14.tar.gz
-
yum编译环境工具
[root@localhost ~]# yum install gcc gcc-c++ make -y
-
手工编译且安装
##解压redis软件包
tar xzvf redis-4.0.14.tar.gz -C /opt
##编译且安装
cd /opt/redis-4.0.14/
make
make PREFIX=/usr/local/redis install
-
创建软链接,便于服务控制
ln -s /usr/local/redis/bin/* /usr/local/bin/
- 关闭防火墙和selinux增强性功能
systemctl stop firewalld
ssetenforce 0
在master上面设置redis的配置文件
- 将redis的配置文件模板复制到/usr/local/redis目录下
[root@192 ~]# cd redis-4.0.14/
[root@192 redis-4.0.14]# ls
00-RELEASENOTES deps README.md runtest-sentinel utils
BUGS INSTALL redis.conf sentinel.conf
CONTRIBUTING Makefile runtest src
COPYING MANIFESTO runtest-cluster tests
[root@192 redis-4.0.14]# cp redis.conf /usr/local/redis/
- 配置redis.conf文件
cd /usr/local/redis
[root@192 redis]# vi redis.conf
bind 192.168.43.101 //表示监听本机地址
protected-mode no //关闭安全保护
port 8000 //开启端口8000
daemonize yes //以独立进程启动
appendonly yes //开启aof持久化功能
requirepass 123456 //设置密码为123456
pidfile /var/run/redis-8000.pid //修改pid文件名
logfile /var/log/redis/redis-8000.log //定义日志文件,需要自己创建
[root@192 redis]#mkdir /var/log/redis/
- 开启redis-master服务器
[root@192 ~]# cd /usr/local/redis/
[root@192 redis]# ./bin/redis-server ./redis.conf
[root@192 redis]# netstat -natp | grep 8000
tcp 0 0 192.168.43.101:8000 0.0.0.0:* LISTEN 1404/redis-server 1
[root@192 redis]#
- 查看master的日志文件
[root@192 ~]# cat /var/log/redis/redis-8000.log
1403:C 22 Mar 23:44:22.054 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=1403, just started
1403:C 22 Mar 23:44:22.054 # Configuration loaded
1404:M 22 Mar 23:44:22.056 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.14 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 8000
| `-._ `._ / _.-' | PID: 1404
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1404:M 22 Mar 23:44:22.059 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1404:M 22 Mar 23:44:22.059 # Server initialized
1404:M 22 Mar 23:44:22.059 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1404:M 22 Mar 23:44:22.059 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1404:M 22 Mar 23:44:22.059 * DB loaded from disk: 0.000 seconds
1404:M 22 Mar 23:44:22.059 * Ready to accept connections
[root@192 ~]#
注意日志信息是否匹配
在两台slave上设置redis的配置文件(两台salve除了bind地址不同其他步骤相同)
- 复制redisc.conf配置文件模板
cp /opt/redis-4.0.14/redis.conf /usr/local/redis
cd /usr/local/redis
- 设置配置文件
[root@192 redis]# vi redis.conf
bind 192.168.43.102 #192.168.43.103 //表示监听本机地址
protected-mode no //关闭安全保护 -------------导致不能进行自动故障转移
port 8000 //开启端口8000
daemonize yes //以独立进程启动
appendonly yes //开启aof持久化功能
requirepass 123456 //设置密码为123456
masterauth 123456 //设置主服务器的密码
pidfile /var/run/redis-8000.pid //修改pid文件名
logfile /var/log/redis/redis-8000.log //定义日志文件,需要自己创建
slaveof 192.168.43.101 8000 //定义主服务器的地址和端口
[root@192 redis]# mkdir /var/log/redis/
- 开启redis服务
[root@192 ~]# cd /usr/local/redis/
[root@192 redis]# ./bin/redis-server ./redis.conf
进入master查看slave信息
[root@localhost ~]# redis-cli -h 192.168.43.101 -p 8000
192.168.43.101:8000> auth 123456
OK
192.168.43.101:8000> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.43.102,port=8000,state=online,offset=840,lag=0
slave1:ip=192.168.43.103,port=8000,state=online,offset=840,lag=0
master_replid:7cc6dca338440e5b139272577d142e79203ca619
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:840
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:840
192.168.43.101:8000>
至此,主从模式搭建完成。
搭建redis-sentinel系统(在三台虚拟机上操作一样)
- 复制sentinel配置文件模板
cp /opt/redis-4.0.14/sentinel.conf /usr/local/redis
-
编辑sentinel配置文件
vi /usr/local/redis/sentinel.conf
protected-mode no //关闭守护进程模式
daemonize yes //以独立进程启动
port 6000 //绑定端口号
##dir "/usr/local/redis/sentinel"
sentinel monitor mymaster 192.168.43.101 8000 1
sentinel auth-pass mymaster 123456
#5秒内master6800没有响应,就认为SDOWN
sentinel down-after-milliseconds mymaster 5000 //ms单位
sentinel failover-timeout mymaster 15000
logfile /var/log/redis/sentinel.log
pidfile /var/run/sentinel.pid
-
开启redis-sentinel
[root@192 ~]# cd /usr/local/redis/
[root@192 redis]# ./bin/redis-sentinel ./sentinel.conf
[root@192 redis]# netstat -natp | grep 6000
tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN 35886/./bin/redis-s
tcp6 0 0 :::6000 :::* LISTEN 35886/./bin/redis-s
-
查看sentinel信息
[root@192 redis]# redis-cli -h 192.168.43.101 -p 6000 -a 123456 info Sentinel
Warning: Using a password with '-a' option on the command line interface may not be safe.
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.43.101:8000,slaves=2,sentinels=3
四.测试集群功能
验证主从复制,读写分离
- 在master上
[root@192 redis]# redis-cli -h 192.168.43.101 -p 8000
192.168.43.101:8000> info replication
NOAUTH Authentication required.
192.168.43.101:8000> auth 123456
OK
192.168.43.101:8000> set stu bob
OK
192.168.43.101:8000> get stu
"bob"
192.168.43.101:8000>
- 在slave上
[root@192 ~]# redis-cli -h 192.168.43.103 -p 8000
192.168.43.103:8000> auth 123456
OK
192.168.43.103:8000> get stu
"bob"
192.168.43.103:8000> set stu aoa
(error) READONLY You can't write against a read only slave.
192.168.43.103:8000>
验证故障自动切换
- 在master上
[root@192 redis]# redis-cli -h 192.168.43.101 -p 8000
192.168.43.101:8000> auth 123456
OK
192.168.43.101:8000> SHUTDOWN
- 在salve上有
[root@slave ~]# redis-cli -h 192.168.43.102 -p 6379 -a 123456
192.168.43.102:6379> info replication
# Replication
role:slave
master_host:192.168.43.103
master_port:8000
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:11674
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:98bbdc73e26ee67626dbcfb44b7a099c4fd6fd4c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:11674
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:11674
192.168.43.102:6379>