1.概要
- 主从复制高可用?
- 架构说明
- 安装配置
- 客户端连接
- 实现原理
- 常见开发运维问题
2.主从复制高可用?
3.Redis Sentinel架构说明
4.Redis Sentinel安装与配置
- 配置开启主从节点
[root@master ~]# sed "s/7000/7001/g" redis-7000.conf > redis-7001.conf [root@master ~]# sed "s/7000/7002/g" redis-7000.conf > redis-7002.conf [root@master ~]# ll 总用量 68 -rw-------. 1 root root 1290 5月 1 17:12 anaconda-ks.cfg -rw-r--r--. 1 root root 92 9月 7 20:48 dump.rdb -rw-r--r--. 1 root root 6140 11月 12 2015 mysql-community-release-el7-5.noarch.rpm -rw-r--r--. 1 root root 6140 11月 12 2015 mysql-community-release-el7-5.noarch.rpm.1 -rw-r--r--. 1 root root 109 9月 7 20:53 redis-7000.conf -rw-r--r--. 1 root root 109 9月 7 20:54 redis-7001.conf -rw-r--r--. 1 root root 109 9月 7 20:54 redis-7002.conf -rw-r--r--. 1 root root 29508 8月 1 23:28 zookeeper.out [root@master ~]# echo "replicaof 127.0.0.1 7000" >> redis-7001.conf [root@master ~]# echo "replicaof 127.0.0.1 7000" >> redis-7002.conf [root@master ~]# cat redis-7001.conf port 7001 daemonize yes pidfile /var/run/redis-7001.pid logfile "7001.log" dir "/opt/soft/redis/redis/data/" replicaof 127.0.0.1 7000 [root@master ~]# redis-server redis-7000.conf [root@master ~]# redis-server redis-7001.conf [root@master ~]# redis-server redis-7002.conf [root@master ~]# redis-cli -p 7000 info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=7001,state=online,offset=42,lag=0 slave1:ip=127.0.0.1,port=7002,state=online,offset=42,lag=0 master_replid:ae8f9676aad83a6515e489cc0ed80eb402b75113 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:42 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:42
- 配置开启sentinel监控主节点。(sentinel是特殊的redis)
[root@master ~]# cat sentinel.conf | grep -v "#" | grep -v "^$" > redis-sentinel-26379.conf [root@master ~]# ll 总用量 84 -rw-r--r--. 1 root root 0 9月 7 20:58 7000.log -rw-r--r--. 1 root root 0 9月 7 20:58 7001.log -rw-r--r--. 1 root root 0 9月 7 20:58 7002.log -rw-------. 1 root root 1290 5月 1 17:12 anaconda-ks.cfg -rw-r--r--. 1 root root 92 9月 7 20:48 dump.rdb -rw-r--r--. 1 root root 6140 11月 12 2015 mysql-community-release-el7-5.noarch.rpm -rw-r--r--. 1 root root 6140 11月 12 2015 mysql-community-release-el7-5.noarch.rpm.1 -rw-r--r--. 1 root root 109 9月 7 20:53 redis-7000.conf -rw-r--r--. 1 root root 134 9月 7 20:56 redis-7001.conf -rw-r--r--. 1 root root 134 9月 7 20:56 redis-7002.conf -rw-r--r--. 1 root root 283 9月 7 21:04 redis-sentinel-26379.conf -rw-r--r--. 1 root root 9710 9月 7 21:03 sentinel.conf -rw-r--r--. 1 root root 29508 8月 1 23:28 zookeeper.out [root@master ~]# vim redis-sentinel-26379.conf [root@master ~]# cat redis-sentinel-26379.conf port 26379 daemonize yes pidfile "/var/run/redis-sentinel-26379.pid" logfile "26379.log" dir "/opt/soft/redis/redis/data" sentinel deny-scripts-reconfig yes sentinel monitor mymaster 127.0.0.1 7000 2 sentinel config-epoch mymaster 0 sentinel leader-epoch mymaster 0 # Generated by CONFIG REWRITE protected-mode no sentinel known-replica mymaster 127.0.0.1 7001 sentinel known-replica mymaster 127.0.0.1 7002 sentinel current-epoch 0 [root@master ~]# sed "s/26379/26380/g" redis-sentinel-26379.conf > redis-sentinel-26380.conf [root@master ~]# sed "s/26379/26381/g" redis-sentinel-26379.conf > redis-sentinel-26381.conf [root@master ~]# redis-sentinel redis-sentinel-26379.conf [root@master ~]# redis-sentinel redis-sentinel-26380.conf [root@master ~]# redis-sentinel redis-sentinel-26381.conf [root@master ~]# 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=127.0.0.1:7000,slaves=2,sentinels=3 [root@master ~]# ps -ef | grep redis-server root 1962 1 0 21:34 ? 00:00:00 redis-server *:7000 root 1969 1 0 21:34 ? 00:00:00 redis-server *:7001 root 1978 1 0 21:34 ? 00:00:00 redis-server *:7002 root 2091 1669 0 21:36 pts/0 00:00:00 grep --color=auto redis-server [root@master ~]# kill 1962 [root@master ~]# 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=127.0.0.1:7002,slaves=2,sentinels=3
- 实际应该多机器
- 详细配置节点
5.Java客户端连接sentinel
6.Python连接sentinel
7.故障转移演练
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class RedisSentinelTest {
private static Logger log = LoggerFactory.getLogger(RedisSentinelTest.class);
public static void main(String[] args) {
String masterName = "mymaster";
Set<String> sentinels = new HashSet<String>(){{
add("127.0.0.1:26379");
add("127.0.0.1:26380");
add("127.0.0.1:26381");
}};
JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, sentinels);
int counter = 0;
while(true){
counter++;
Jedis jedis = null;
try{
jedis = jedisSentinelPool.getResource();
int index = new Random().nextInt(100000);
String key = "k-" + index;
String value = "v-" + index;
jedis.set(key, value);
if (counter % 100 == 0){
log.info("{} value is {}", key, jedis.get(key));
}
TimeUnit.MILLISECONDS.sleep(10);
}catch (Exception e){
log.error(e.getMessage(), e);
}finally {
if(jedis != null){
jedis.close();
}
}
}
}
}
8.三个定时任务
9.主观下线与客观下线
10.领导者选举
11.故障转移(sentinel领导者节点完成)
12.Redis Sentinel开发运维问题
- 节点运维
-
节点下线和上线
-
主节点
从节点
-
Sentinel节点
-
机器下线:例如过保等情况
机器性能不足:例如CPU、内存、硬盘、网络等
节点自身故障:例如服务不稳定等
-
- 高可用读写分离