redis主从配置+sentinel哨兵模式
redis版本:redis-2.8.19.tar.gz
架构:2个节点
节点1: 10.8.207.25
节点2: 10.8.207.26
节点1部署redis实例,角色master,部署sentinel实例,监控redis-master节点
节点2部署redis实例,角色slave ,部署sentinel实例,监控redis-master节点
一.配置redis主从
节点1:redis-master配置
1.编译安装redis
Copy安装文件到/usr目录下:
tar zxvf redis-2.8.19.tar.gz cd redis-2.8.19 make
2.更改redis.conf配置文件
daemonize no 改为 yes #后台运行
节点2:redis-slave配置
1.编译安装redis
Copy安装文件到/usr目录下:
tar zxvf redis-2.8.19.tar.gz cd redis-2.8.19 make
2.更改redis.conf配置文件
daemonize no 改为 yes #后台运行 slaveof 10.8.207.25 6379 #指向master服务器IP、端口
启动redis-master
# src/redis-server -p 6379 redis.conf -h 10.8.207.25
启动redis-slave
# src/redis-server -p 6379 redis.conf -h 10.8.207.26
查看redis-master运行信息
# src/redis-cli -h 10.8.207.25 -p 6379 info replication # Replication role:master connected_slaves:1 slave0:ip=10.8.207.26,port=6379,state=online,offset=69298006,lag=0 master_repl_offset:69298143 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:68249568 repl_backlog_histlen:1048576
查看redis-slave运行信息
# src/redis-cli -h 10.8.207.26 -p 6379 info replication # Replication role:slave master_host:10.8.207.25 master_port:6379 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:69302158 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
测试主从复制
redis-master
# src/redis-cli -p 6379 -h 10.8.207.25 10.8.207.25:6379> set name 123 OK 10.8.207.25:6379> get name "123"
Redis-slave # src/redis-cli -p 6379 -h 10.8.207.26 10.8.207.26:6379> get name "123"
二.配置sentinel哨兵模式
1.配置sentinel.conf配置文件
2个sentinel实例配置相同mastername mymaster
port 26379 sentinel monitor mymaster 10.8.207.25 6379 1
启动redis-master上的sentinel
src/redis-sentinel sentinel.conf -h 10.8.207.25 &
启动redis-slave上的sentinel
src/redis-sentinel sentinel.conf -h 10.8.207.26 &
查看redis-master上的sentinel运行信息,端口不可省略
# src/redis-cli -h 10.8.207.25 -p 26379 info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 master0:name=mymaster,status=ok,address=10.8.207.25:6379,slaves=1,sentinels=2
查看redis-slave上的sentinel运行信息,端口不可省略
# src/redis-cli -h 10.8.207.26 -p 26379 info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 master0:name=mymaster,status=ok,address=10.8.207.25:6379,slaves=1,sentinels=2
2.验证sentinel切换主从
# src/redis-cli -h 10.8.207.25 shutdown
查看redis-master上的sentinel运行信息
# src/redis-cli -h 10.8.207.25 -p 26379 info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 master0:name=mymaster,status=ok,address=10.8.207.26:6379,slaves=1,sentinels=2
查看redis-slave上的sentinel运行信息
# # src/redis-cli -h 10.8.207.26 -p 26379 info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 master0:name=mymaster,status=ok,address=10.8.207.26:6379,slaves=1,sentinels=2
可以看到10.8.207.26已变成master.