Redis主从复制
搭建Redis主从复制
安装Redis
systemctl stop firewalld
setenforce 0
yum install -y gcc gcc-c++ make
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
./install_server.sh
......
Please select the redis executable path [/usr/local/bin/redis-server] /usr/local/redis/bin/redis-server
ln -s /usr/local/redis/bin/* /usr/local/bin/
修改Redis配置文件(Master节点操作)
vim /etc/redis/6379.conf
bind 0.0.0.0 #70行,监听所有网段
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
修改Redis配置文件(Slave节点操作)
vim /etc/redis/6379.conf
bind 0.0.0.0 #70行,监听所有网段
daemonize yes #137行,开启守护进程
logfile /var/log/redis_6379.log #172行,指定日志文件目录
dir /var/lib/redis/6379 #264行,指定工作目录
replicaof 192.168.241.3 6379 #288行,指定要同步的Master节点IP和端口
appendonly yes #700行,开启AOF持久化功能
/etc/init.d/redis_6379 restart
验证主从效果 在Master节点上看日志
tail -f /var/log/redis_6379.log
6136:M 25 Feb 2022 14:24:36.246 * Background saving terminated with success
6136:M 25 Feb 2022 14:24:36.246 * Synchronization with replica 192.168.50.110:6379 succeeded
6136:M 25 Feb 2022 14:31:01.412 * Replica 192.168.50.20:6379 asks for synchronization
6136:M 25 Feb 2022 14:31:01.413 * Full resync requested by replica 192.168.50.20:6379
6136:M 25 Feb 2022 14:31:01.413 * Starting BGSAVE for SYNC with target: disk
6136:M 25 Feb 2022 14:31:01.413 * Background saving started by pid 6258
6258:C 25 Feb 2022 14:31:01.418 * DB saved on disk
6258:C 25 Feb 2022 14:31:01.418 * RDB: 4 MB of memory used by copy-on-write
6136:M 25 Feb 2022 14:31:01.434 * Background saving terminated with success
6136:M 25 Feb 2022 14:31:01.434 * Synchronization with replica 192.168.50.20:6379 succeeded
在Master节点上验证从节点
redis-cli info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.50.110,port=6379,state=online,offset=798,lag=0
slave1:ip=192.168.50.20,port=6379,state=online,offset=798,lag=0
Redis哨兵模式
实验 修改Redis哨兵模式的配置文件(所有节点操作)
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.50.105 6379 2
#84行,修改 指定该哨兵节点监控192.168.50.105:6379这个主节点,该主节点的名称是mymaster,最后的2的含义与主节点的故障判定有关:至少需要2个哨兵节点同意,才能判定主节点故障并进行故障转移
sentinel down-after-milliseconds mymaster 3000
#113行,判定服务器down掉的时间周期,默认30000毫秒(30秒)
sentinel failover-timeout mymaster 180000
#146行,故障节点的最大超时时间为180000(180秒)
启动哨兵模式 先启master,再启动slave
cd /opt/redis-5.0.7/
redis-sentinel sentinel.conf &
查看哨兵信息(每台服务器都可以查看)
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=192.168.50.105:6379,slaves=2,sentinels=3
故障模拟
查看redis-server进程号
ps -elf | grep redis
5 S root 6136 1 0 80 0 - 39101 ep_pol 14:21 ? 00:00:04 /usr/local/redis/bin/redis-server 0.0.0.0:6379
5 S root 6638 1 0 80 0 - 38461 ep_pol 15:07 ? 00:00:02 redis-sentinel *:26379 [sentinel]
0 S root 6943 1601 0 80 0 - 28170 pipe_w 15:35 pts/0 00:00:00 grep --color=auto redis
杀死Master节点上redis-server的进程号
kill -3 6136
#Master节点上redis-server的进程号
5 S root 6638 1 0 80 0 - 38461 ep_pol 15:07 ? 00:00:03 redis-sentinel *:26379 [sentinel]
0 S root 6963 1601 0 80 0 - 28169 pipe_w 15:37 pts/0 00:00:00 grep --color=auto redis
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=192.168.50.110:6379,slaves=2,sentinels=3
Redis 集群模式
搭建Redis群集模式
cd /etc/redis/
mkdir -p redis-cluster/redis600{1..6}
for i in {1..6}
do
cp /opt/redis-5.0.7/redis.conf /etc/redis/redis-cluster/redis600$i
cp /opt/redis-5.0.7/src/redis-cli /opt/redis-5.0.7/src/redis-server /etc/redis/redis-cluster/redis600$i
done
开启群集功能
cd /etc/redis/redis-cluster/redis6001
vim redis.conf
bind 0.0.0.0
#69行,监听所有网段
protected-mode no
#88行,修改,关闭保护模式
port 6001
#92行,修改,redis监听端口,
daemonize yes
#136行,以独立进程启动
cluster-enabled yes
#832行,取消注释,开启群集功能
cluster-config-file nodes-6001.conf
#840行,取消注释,群集名称文件设置
cluster-node-timeout 15000
#846行,取消注释群集超时时间设置
appendonly yes
#700行,修改,开启AOF持久化
启动redis节点
cd /etc/redis/redis-cluster/redis6001
redis-server redis.conf
for d in {1..6}
do
cd /etc/redis/redis-cluster/redis600$d
redis-server redis.conf
done
ps -ef | grep redis
启动群集
redis-cli --cluster create 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005 127.0.0.1:6006 --cluster-replicas 1
#六个实例分为三组,每组一主一从,前面的做主节点,后面的做从节点。下面交互的时候 需要输入 yes 才可以创建。
--replicas 1 表示每个主节点有1个从节点。
测试集群
redis-cli -p 6001 -c
127.0.0.1:6001> CLUSTER SLOTS
1) 1) (integer) 0
2) (integer) 5460
3) 1) "127.0.0.1"
2) (integer) 6001
3) "89528194315cde2b625002d93458116340aa9129"
4) 1) "127.0.0.1"
2) (integer) 6006
3) "2c87e6c25f5b0fa458e1443f6024d7b7fd2519a3"
2) 1) (integer) 10923
2) (integer) 16383
3) 1) "127.0.0.1"
2) (integer) 6003
3) "d48d6962b09691313ddd3ed899df941f35ffc156"
4) 1) "127.0.0.1"
2) (integer) 6005
3) "002042335e6f6df586e201cf7cf36d49fc2996d9"
3) 1) (integer) 5461
2) (integer) 10922
3) 1) "127.0.0.1"
2) (integer) 6002
3) "1467de28cc619a3f99639ea193497b68c81117eb"
4) 1) "127.0.0.1"
2) (integer) 6004
3) "3bc36e7a78d8a023d7934032c60a39577713434c"
127.0.0.1:6001> set name lisi
-> Redirected to slot [5798] located at 127.0.0.1:6002
OK
127.0.0.1:6002> CLUSTER KEYSLOT name
(integer) 5798
127.0.0.1:6002> keys *
1) "name"
127.0.0.1:6002> quit
redis-cli -p 6006 -c
127.0.0.1:6006> keys *
(empty list or set)
127.0.0.1:6006> quit
redis-cli -p 6004 -c
127.0.0.1:6004> keys *
1) "name"
127.0.0.1:6004>