一、redis集群之主从复制
搭建环境
节点类型 | IP地址 | 安装包 |
---|---|---|
master | 20.0.0.20 | redis-5.0.7.tar.gz |
slave1 | 20.0.0.30 | redis-5.0.7.tar.gz |
slave2 | 20.0.0.40 | redis-5.0.7.tar.gz |
slave3 | 20.0.0.50 | redis-5.0.7.tar.gz |
搭建过程
- 关闭防火墙和核心保护
systemctl stop firewalld.service
setenforce 0
1.所有节点安装redis
[root@localhost opt]# tar xzvf redis-5.0.7.tar.gz -C /opt/
[root@localhost opt]# cd /opt/redis-5.0.7/
[root@localhost redis-5.0.7]# make
[root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis install
#如果安装过程中,更改安装路径可以用make PRRFIX=安装路径
#设置Redis相关配置文件
[root@localhost redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/
[root@localhost redis-5.0.7]# cd utils/
[root@localhost utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] /usr/local/bin/redis-server
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
2、修改主、从节点的配置文件
master:
vi /etc//redis/6379.conf
70/bind 0.0.0.0 #在研发、测试环境可以考虑bind 0.0.0.0,线上生产环境建议绑定从节点服务器的IP地址
137行 daemonize yes #开启守护进程yes
172行 logfile "" 改成 logfile /var/log/redis_6379.log
264行 dir ./ 改成 dir /var/lib/redis/6379 #本地数据库的存放目录
700/appendonly yes #启用AOF持久化
从节点:
vi /etc//redis/6379.conf
67/bind 0.0.0.0 #在研发、测试环境可以考虑bind 0.0.0.0,线上生产环境建议绑定从节点服务器的IP地址
137行 daemonize yes #开启守护进程yes
172行 logfile "" 改成 logfile /var/log/redis_6379.log
264行 dir ./ 改成 dir /var/lib/redis/6379
700/appendonly no #启用AOF持久化
287/replicaof <masterip> <masterport> 改成 replicaof 20.0.0.20 6379
3、启动服务,先启动master redis,然后依次启动slave
#重启服务
/etc/init.d/redis_6379 restart
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
#检查端口
netstat -anpt | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 9923/redis-server 0
4、查看日志文件内容或登录数据库,查看节点角色
[root@master1 ~]# tail -f /var/log/redis_6379.log
或
[root@master1 ~]# redis-cli
127.0.0.1:6379> info replication
5、效果验证
master
redis-cli
127.0.0.1:6379> set name hyc
OK
127.0.0.1:6379> get name
"hyc"
slave
redis-cli
127.0.0.1:6379> get name
"hyc"
二、redis集群之哨兵模式
搭建过程
1、主从复制的基础上进行
2、设置哨兵模式的配置文件
master
vi /opt/redis-5.0.7/sentinel.conf
17/protected-mode no #关闭保护模式
21/port 26379 #无需修改,sentinel的端口号
26/daemonize yes #指定sentinel为后台启动
36/logfile /var/log/sentinel.log #日志存放路径
65/dir /tmp 改成 dir "/var/lib/redis/6379" #数据库存放路径
84/sentinel monitor mymaster 20.0.0.20 6379 2
113/sentinel down-after-milliseconds mymaster 3000 #原来是30000,为了尽快看到效果,将时间缩短
146/sentinel failover-timeout mymaster 120000 #原来是180000
slave
vi /opt/redis-5.0.7/sentinel.conf
17/protected-mode no #关闭保护模式
26/daemonize yes #指定sentinel为后台启动
36/logfile /var/log/sentinel.log #日志存放路径
65/dir /tmp 改成 dir "/var/lib/redis/6379" #数据库存放目录
84/sentinel monitor mymaster 20.0.0.20 6379 2 #填写主节点的地址
113/sentinel down-after-milliseconds mymaster 3000 #原来是30000,为了尽快看到效果,将时间缩短
146/sentinel failover-timeout mymaster 120000 #原来是180000
3、启动哨兵服务
- 依次启动哨兵sentinel,先启动master 然后slave
redis-sentinel /opt/redis-5.0.7/sentinel.conf &
查看哨兵信息,里面有master节点的IP地址
[root@master1 ~]# redis-cli -p 26379 INFO Sentinel
# 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=20.0.0.20:6379,slaves=3,sentinels=4
[1]+ 完成 redis-sentinel /opt/redis-5.0.7/sentinel.conf