Redis 主从搭建

252 阅读5分钟

前言

记录 Redis 主从搭建的步骤

环境

OS: centos6.5

STEP 1

下载 redis

wget http://download.redis.io/releases/redis-3.2.9.tar.gz

安装 redis

# 解压缩
tar -xzf redis-3.2.9.tar.gz 

# 进入Redis源码目录
cd redis-3.2.9

# 编译
make 
# 建议安装到某个目录下,使用会更容易一点
make 或者 make install PREFIX={$安装目录}

# 测试编译结果
make test

STEP 2

启动 redis

[root@MiWiFi-R3-srv redis_bin]# bin/redis-server redis.conf 
9693:M 27 Mar 22:00:42.510 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 9693
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

9693:M 27 Mar 22:00:42.512 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9693:M 27 Mar 22:00:42.512 # Server started, Redis version 3.2.9
9693:M 27 Mar 22:00:42.512 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
9693:M 27 Mar 22:00:42.513 * DB loaded from disk: 0.000 seconds
9693:M 27 Mar 22:00:42.513 * The server is now ready to accept connections on port 6379

STEP 3

启动主从集群

# 新建几个目录,以端口为标记
mkdir 7001 7002 7003

分别写配置

# 主机
bind 127.0.0.1
port 7001
# daemonize no 默认情况下,redis不是在后台运行的,如果需要在后台运行,把该项的值更改为yes
daemonize yes 
logfile "7001.log" # 日志
dbfilename "dump-7001.rdb" # rdb文件

# 从机
bind 127.0.0.1
port 7002
daemonize yes
logfile "7002.log"
dbfilename "dump-7002.rdb"
slaveof 127.0.0.1 7001 # 表示跟踪主机

分别启动主从

# bin/redis-server 7001/redis-7001.conf
# bin/redis-server 7002/redis-7002.conf
# bin/redis-server 7003/redis-7003.conf

测试主从

127.0.0.1:7001> set test3 testStr3
OK
127.0.0.1:7001> 

127.0.0.1:7002> keys *
1) "test3"
2) "test2"
3) "test"
127.0.0.1:7002> 
127.0.0.1:7002> set test4 test4Str
# 无法直接写从机
(error) READONLY You can't write against a read only slave.
127.0.0.1:7002> 

STEP 4 设置哨兵

构建3个哨兵目录

drwxr-xr-x. 2 root root   4096 3月  27 21:42 sentinel-26379
drwxr-xr-x. 2 root root   4096 3月  27 21:42 sentinel-26380
drwxr-xr-x. 2 root root   4096 3月  27 21:42 sentinel-26381

编辑配置文件(PS: 哨兵程序其实是 redis-server 的软连接,所以配置风格都很像,很多参数一样)

port 26379 # 端口
bind 127.0.0.1
daemonize yes
# 目录
dir "/home/www/redis_bin/sentinel-26379" 
# 日志
logfile "/home/www/redis_bin/sentinel-26379/sentinel.log"
sentinel myid 51861118567ca1c41138c9c7ec06c8d8d9e0266c

# sentinel监控的redis主节点的 ip port
# master-name 可以自己命名的主节点名字,只能由字母A-z、数字0-9 、这三个字符".-_"组成
# quorum 将这个主实例判断为失效至少需要quorum个Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel monitor mymaster 127.0.0.1 7001 2
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0

STEP 5 查看相关日志和配置

查看主节点

127.0.0.1:7001> info replication
# Replication
role:master # 角色主节点
connected_slaves:2 # 有两个从节点
slave0:ip=127.0.0.1,port=7002,state=online,offset=201209,lag=0
slave1:ip=127.0.0.1,port=7003,state=online,offset=201062,lag=0 # 两个节点的状态
master_repl_offset:201209
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:201208

查看从节点

127.0.0.1:7002> info replication
# Replication
role:slave # 从节点
master_host:127.0.0.1
master_port:7001
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:208545
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

查看哨兵

127.0.0.1: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:7001,slaves=2,sentinels=3 # 哨兵盯着点节点

测试主从下线

让主节点下线

bin/redis-cli -p 7001 shutdown

从节点 sync 失败了。

9557:S 27 Mar 22:40:27.052 * Connecting to MASTER 127.0.0.1:7001
9557:S 27 Mar 22:40:27.053 * MASTER <-> SLAVE sync started
9557:S 27 Mar 22:40:27.053 # Error condition on socket for SYNC: Connection refused
9557:S 27 Mar 22:40:28.055 * Connecting to MASTER 127.0.0.1:7001
9557:S 27 Mar 22:40:28.055 * MASTER <-> SLAVE sync started
9557:S 27 Mar 22:40:28.055 # Error condition on socket for SYNC: Connection refused

启动主节点

# 观察从节点
9557:S 27 Mar 22:46:40.203 # Error condition on socket for SYNC: Connection refused
9557:S 27 Mar 22:46:41.207 * Connecting to MASTER 127.0.0.1:7001
9557:S 27 Mar 22:46:41.207 * MASTER <-> SLAVE sync started
9557:S 27 Mar 22:46:41.207 # Error condition on socket for SYNC: Connection refused
9557:S 27 Mar 22:46:42.208 * Connecting to MASTER 127.0.0.1:7001
9557:S 27 Mar 22:46:42.208 * MASTER <-> SLAVE sync started
9557:S 27 Mar 22:46:42.209 * Non blocking connect for SYNC fired the event.
9557:S 27 Mar 22:46:42.209 * Master replied to PING, replication can continue...
9557:S 27 Mar 22:46:42.209 * Trying a partial resynchronization (request b1f882d995afbfa069c9b5fc39ae0497ed513a09:226214).
9557:S 27 Mar 22:46:42.211 * Full resync from master: 9628b0ea6e19af7fb5e4433771dd3289848261fe:1
9557:S 27 Mar 22:46:42.211 * Discarding previously cached master state.
9557:S 27 Mar 22:46:42.297 * MASTER <-> SLAVE sync: receiving 76 bytes from master
9557:S 27 Mar 22:46:42.297 * MASTER <-> SLAVE sync: Flushing old data
9557:S 27 Mar 22:46:42.297 * MASTER <-> SLAVE sync: Loading DB in memory
9557:S 27 Mar 22:46:42.297 * MASTER <-> SLAVE sync: Finished with success # 重新恢复和主节点的通信

Question

  • 三个主节点、三个哨兵,主节点下线,但没测试出有从节点升级为主节点?

疑问

  1. 假如一个 redis 的主节点假死了,哨兵选举了新的节点做主节点后,假死的节点网络恢复,那时候会怎么办?假死的节点应该怎么定位自己?

集群

集群待测试

Thanks

CentOS 7离线安装Redis