Redis(主从复制、哨兵模式、集群)部署
一、Redis主从复制部署
1. 实验环境
主机 | 操作系统 | 主机IP | 软件包 |
---|---|---|---|
Master | CentOS7 | 192.168.117.10 | redis-5.0.7.tar.gz |
Slave1 | CentOS7 | 192.168.117.20 | redis-5.0.7.tar.gz |
Slave2 | CentOS7 | 192.168.117.30 | redis-5.0.7.tar.gz |
2. 安装Redis
一键部署安装Redis
cd /opt #将软件包拖入
vim redis.sh
#!/bin/bash
#关闭防火墙
systemctl stop firewalld
setenforce 0
yum install -y gcc gcc-c++ make
yum -y install expect
cd /opt
tar zxvf redis-5.0.7.tar.gz -C /opt/
cd /opt/redis-5.0.7/
make
make PREFIX=/usr/local/redis install
cd /opt/redis-5.0.7/utils
/usr/bin/expect <<EOF
spawn ./install_server.sh
expect "instance" {send "\r"}
expect "config" {send "\r"}
expect "log" {send "\r"}
expect "data" {send "\r"}
expect "executable" {send "/usr/local/redis/bin/redis-server\r"}
expect "abort" {send "\r"}
expect eof
EOF
ln -s /usr/local/redis/bin/* /usr/local/bin/
/etc/init.d/redis_6379 restart
sed -i '/bind 127.0.0.1/c bind 0.0.0.0' /etc/redis/6379.conf
sed -i 's/appendonly no/appendonly yes/' /etc/redis/6379.conf
/etc/init.d/redis_6379 restart
/etc/init.d/redis_6379 status
netstat -natp | grep redis
sh redis.sh
注:由于我自己虚拟机全都事先配置好本地yum源以及自动挂载,故脚本没有yum源仓库的配置
3. 修改Redis配置文件
Master:192.168.117.10
vim /etc/redis/6379.conf
bind 0.0.0.0 #70行,修改bind 项,0.0.0.0监听所有网段
daemonize yes #137行,开启守护进程
logfile /var/log/redis_6379.log #172行,指定日志文件目录
dir /var/lib/redis/6379 #264行,指定工作目录
appendonly yes #700行,开启AOF持久化功能
/etc/init.d/redis_6379 restart
Slave1:192.168.117.20
Slave2:192.168.117.30
vim /etc/redis/6379.conf
bind 0.0.0.0 #70行,修改bind 项,0.0.0.0监听所有网卡
daemonize yes #137行,开启守护进程
logfile /var/log/redis_6379.log #172行,指定日志文件目录
dir /var/lib/redis/6379 #264行,指定工作目录
replicaof 192.168.117.10 6379 #288行,指定要同步的Master节点IP和端口
appendonly yes #700行,开启AOF持久化功能
/etc/init.d/redis_6379 restart
4. 验证
Master:192.168.117.10
- 查看日志
tail -f /var/log/redis_6379.log
- 验证从节点
redis-cli info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.184.20,port=6379,state=online,offset=555,lag=0
slave1:ip=192.168.184.30,port=6379,state=online,offset=555,lag=0
二、Redis哨兵模式部署
1. 实验环境
基于主从复制已完成
主机 | 操作系统 | 主机IP | 软件包 |
---|---|---|---|
Master | CentOS7 | 192.168.117.10 | redis-5.0.7.tar.gz |
Slave1 | CentOS7 | 192.168.117.20 | redis-5.0.7.tar.gz |
Slave2 | CentOS7 | 192.168.117.30 | redis-5.0.7.tar.gz |
2. 修改Reids配置文件
Master:192.168.117.10
Slave1:192.168.117.20
Slave2:192.168.117.30
systemctl stop firewalld
setenforce 0
vim /opt/redis-5.0.7/sentinel.conf
protected-mode no #17行,关闭保护模式
port 26379 #21行,Redis哨兵默认的监听端口
daemonize yes #26行,指定sentinel为后台启动
logfile "/var/log/sentinel.log" #36行,指定日志存放路径
dir "/var/lib/redis/6379" #65行,指定数据库存放路径
sentinel monitor mymaster 192.168.184.10 6379 2 #84行,修改 指定该哨兵节点监控192.168.184.10:6379这个主节点,该主节点的名称是mymaster,最后的2的含义与主节点的故障判定有关:至少需要2个哨兵节点同意,才能判定主节点故障并进行故障转移
sentinel down-after-milliseconds mymaster 30000 #113行,判定服务器down掉的时间周期,默认30000毫秒(30秒)
sentinel failover-timeout mymaster 180000 #146行,故障节点的最大超时时间为180000(180秒)
3. 启动哨兵模式
cd /opt/redis-5.0.7/
redis-sentinel sentinel.conf &
#先启动主服务器,再启动从服务器
4. 故障模拟
查看Master节点redis-server进程号
ps aux | grep redis
root 46817 0.1 0.6 159476 12280 ? Ssl 14:15 0:04 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root 47150 0.3 0.3 153844 7904 ? Ssl 14:44 0:02 redis-sentinel *:26379 [sentinel]
root 47269 0.0 0.0 112676 980 pts/0 S+ 14:56 0:00 grep --color=auto redis
杀死Master节点上redis-server的进程号
kill -9 46817 #Master节点上redis-server的进程号
5. 验证
tail -f /var/log/sentinel.log #动态查询master上的哨兵模式日志
redis-cli -p 26379 INFO Sentinel
三、Redis集群部署
1. 实验环境
主机 | 操作系统 | IP:端口 | 软件包 |
---|---|---|---|
Master1 | CentOS7 | 192.168.117.10:8001 | redis-5.0.7.tar.gz |
Slave1 | CentOS7 | 192.168.117.20:8002 | redis-5.0.7.tar.gz |
Master2 | CentOS7 | 192.168.117.30:8003 | redis-5.0.7.tar.gz |
Slave2 | CentOS7 | 192.168.117.40:8004 | redis-5.0.7.tar.gz |
Master3 | CentOS7 | 192.168.117.50:8005 | redis-5.0.7.tar.gz |
Slave3 | CentOS7 | 192.168.117.60:8006 | redis-5.0.7.tar.gz |
2. 所有节点
cd /etc/redis/
mkdir -p redis-cluster/redis6379
cp /opt/redis-5.0.7/redis.conf /etc/redis/redis-cluster/redis6379/
cp /opt/redis-5.0.7/src/redis-cli /opt/redis-5.0.7/src/redis-server /etc/redis/redis-cluster/redis6379/
3. Master1节点
#其他5个文件夹的配置文件以此类推修改,注意6个端口都要不一样。
cd /etc/redis/redis-cluster/redis6379
vim redis.conf
bind 192.168.117.10 #69行,修改bind项,监听自己的IP
protected-mode no #88行,修改,关闭保护模式
port 8001 #92行,修改,redis监听端口,
daemonize yes #136行,以独立进程启动
cluster-enabled yes #832行,取消注释,开启群集功能
cluster-config-file nodes-6379.conf #840行,取消注释,群集名称文件设置,无需修改
cluster-node-timeout 15000 #846行,取消注释群集超时时间设置
appendonly yes #699行,修改,开启AOF持久化
scp /etc/redis/redis-cluster/redis6379/redis.conf root@192.168.117.20:/etc/redis/redis-cluster/redis6379/redis.conf
scp /etc/redis/redis-cluster/redis6379/redis.conf root@192.168.117.30:/etc/redis/redis-cluster/redis6379/redis.conf
scp /etc/redis/redis-cluster/redis6379/redis.conf root@192.168.117.40:/etc/redis/redis-cluster/redis6379/redis.conf
scp /etc/redis/redis-cluster/redis6379/redis.conf root@192.168.117.50:/etc/redis/redis-cluster/redis6379/redis.conf
scp /etc/redis/redis-cluster/redis6379/redis.conf root@192.168.117.60:/etc/redis/redis-cluster/redis6379/redis.conf
4. 其它节点
vim /etc/redis/redis-cluster/redis6379/redis.conf
5. 所有节点
cd /etc/redis/redis-cluster/redis6379/
redis-server redis.conf
redis-cli --cluster create 192.168.117.10:8001 192.168.117.30:8003 192.168.117.50:8005 192.168.117.60:8006 192.168.117.40:8004 192.168.117.20:8002 --cluster-replicas 1
redis-cli -h 192.168.117.10 -p 8001 -c #加-c参数,节点之间就可以互相跳转
cluster slots #查看节点的哈希槽编号范围
set class a
cluster keyslot class #查看name键的槽编号